Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 633
Martin Drautzburg Avatar asked Oct 03 '22 11:10

Martin Drautzburg


2 Answers

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.

like image 199
kosmikus Avatar answered Oct 07 '22 19:10

kosmikus


Apparently, lhs2tex will still preprocess anything following -- before rendering it as LaTeX, which is why -- % doesn't work, and as you point out in your question, --% isn't recognised as a comment by Haskell, because it could be an operator.

The easiest workaround for this is to make it a comment line for both Haskell and LaTeX. For example, if you had:

> main = do
>       options <- getOptions
>       setup <- fmap readSetup $ readFile "setup.dat"
>       configureWith options setup
>       putStrLn "Some message to the user"

If you wanted to temporarily miss out the configureWith line, you could do this:

> main = do
>       options <- getOptions
>       setup <- fmap readSetup $ readFile "setup.dat"

% >       configureWith options setup

>       putStrLn "Some message to the user"

The blank lines are necessary because in literate Haskell you can't have a comment line next to a code line. (This is to prevent simple errors over missing the initial >.)

like image 39
AndrewC Avatar answered Oct 07 '22 20:10

AndrewC