9

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.

Mike Izbicki
  • 6,286
  • 1
  • 23
  • 53

3 Answers3

6

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.

kosmikus
  • 19,549
  • 3
  • 51
  • 66
5

I use the following style.

%if False

\begin{code}
<code>
\end{code}

%endif

Edit: I just found Andres Löh's slides, where I have probably taken this style from.

Jan Christiansen
  • 3,153
  • 1
  • 19
  • 16
4

From http://www.haskell.org/haskellwiki/Literate_programming...

If you want to hide some code, you can e.g. define:

\long\def\ignore#1{}

Auxiliary functions can be hidden as follows:

\ignore{
\begin{code}
help = putStr "Help me, what is this LiterateProgramming thing??"
\end{code}
}
mhwombat
  • 8,026
  • 28
  • 53
  • 2
    I actually discourage this solution. Of course it's possible to exclude parts of TeX documents with a macro like this. But `\ignore` as given makes it likely to introduce spurious spaces in your document (after the closing `}`), and its presence may still affect the surrounding document in subtle ways. Look at LaTeX `comment` environments and their documentation if you want to know about all the issues. Doing the hiding in the preprocessor completely bypasses these problems. – kosmikus Oct 25 '12 at 22:20