Search code examples
haskelllatexliterate-programming

How to comment out lines in literate haskell


I am having trouble commenting out lines of code in an lhs-style haskell program, so both haskell and Latex ignore the line.

When I use -- then my lh2tex will try to render the haskell code as a comment. But this often fails, because the code contains dollars and other stuff which is confusing for Latex.

When I use --%, then Latex is happy as it just ignores the comment, but haskell does not like --%. Only when I put a space after -- haskell is okay with it, but then Latex is complaining again.


Solution

  • If you're using lhs2TeX (which you seem to be), then you can hide code from LaTeX by using lhs2TeX conditionals:

    %if False
    
    > code seen by Haskell but not typeset
    > -- comment that is not typeset
    
    %endif
    
    > code seen by Haskell and typeset
    > -- comment that will be typeset
    

    As Daniel Wagner suggests in his comment, another option is to prefix complete lines with % to turn them into LaTeX comments.

    lhs2TeX will always treat comments as LaTeX text, but it will in addition perform preprocessing. So using a % on a line with a comment (as in -- %) is not going to work, because the % will be ending up in the middle of partially relevant code in the generated TeX file and trigger errors.