5

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.

Martin Drautzburg
  • 5,143
  • 1
  • 27
  • 39
  • 1
    Why on earth would LaTeX complain about `-- %`? You'd get an n-dash, or perhaps two adjacent hyphen-minus on that line, but the rest of the line should be considered a comment and ignored by LaTeX. – Daniel Fischer Jul 21 '13 at 13:06
  • 2
    Can't you just prefix the whole line with `%` (or maybe `% `)? This will make it a LaTeX comment, and since the line no longer starts with `>`, it will be considered a literate comment, too. I haven't tested it, though. – Daniel Wagner Jul 21 '13 at 18:55
  • @DanielWagner Yes, but remember you have to put a blank line in between lines that are code (starting with >), and non-blank non-code lines. – AndrewC Jul 21 '13 at 19:00

2 Answers2

2

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.

kosmikus
  • 19,549
  • 3
  • 51
  • 66
  • It would be helpful if whoever downvoted this reply could provide some feedback so that I can provide better answers in the future. – kosmikus Jul 22 '13 at 04:06
  • %if False is good to know. It behaves like you say it would. Meanwhile I found another possibility. With the comment package loaded I replace my \begin{code} with \begin{comment} and \end accordingly. This is a good way for commenting out entire blocks of texts. For single lines your sugestion answers my question. – Martin Drautzburg Jul 22 '13 at 20:05
2

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 >.)

AndrewC
  • 32,300
  • 7
  • 79
  • 115