Search code examples
haskellliterate-programming

How can I hide code blocks in lhs2TeX?


I want to document my code using latex, but it's really annoying having all those modules and compiler extensions show up at the beginning of the latex document. Is there some flag I can pass to lhs2TeX to prevent it from displaying this section of code in the pdf, while still letting ghc see the code?

This seems like a pretty basic feature, but I can't find it in the manual.


Solution

  • The right way is indeed to use conditionals.

    The simple option is to exclude such code from lhs2TeX processing completely:

    %if False
    Everything you want LaTeX not to see. Can be
    
    > code
    
    as well as other stuff.
    %endif
    

    In a more advanced scenario, you might want to use lhs2TeX to preprocess both your sources for LaTeX and your code for Haskell. In such a setting, you can use

    %if style /= newcode
    Everything you want LaTeX not to see, as above.
    %else
    Everything you want LaTeX to see, but not Haskell.
    %endif
    

    Here's an example of how I use this in practice: assume I have two versions of a function; in the document I don't want to distinguish them, but in the Haskell code, they should get different names. On the other hand, the first version of the example is incomplete, so I have an ellipsis, but I still want it typechecked. So I can do the following:

    %if style /= newcode
    %format example1 = example
    %format example2 = example
    %format DOTS = "\dots "
    %else
    %format DOTS = "undefined"
    %endif
    
    Our first attempt:
    
    > example1 = 42 == DOTS
    
    Now we complete the example:
    
    > example2 = 42 == 6 * 9
    

    You can process this file in --newcode mode to extract preprocessed Haskell, and in --poly mode to get the LaTeX as usual.

    The manual describes conditionals in Section 10. Examples of uses of conditionals are provided in Sections 11.1 and 11.4.