kosmikus / lhs2tex

Preprocessor for typesetting Haskell sources with LaTeX
Other
99 stars 28 forks source link

Could one introduce an environment to customize the whole mass of the code snippets in a document? #59

Closed kindaro closed 10 months ago

kindaro commented 6 years ago

I'm not a big deal of a LaTeX hacker, but I set out today to modify the font size of the code snippets to small and this happens to not be doable due to the lack of an environment that I could have tweaked using one of the several tricks one finds on the Internet. Particularly, the nice \begin{code} ... \end{code} environment would be lost in translation before any macros could be expanded in its place, and no other environment is consistently introduced that one could target with a macro.

Could it be made so that the code blocks are each automagically enclosed within some environment that does nothing, just for the sake of it being redefined by the typesetter according to their special needs? Or is there another, recommended route to achieving the same effect? So far, I would be happy consistently changing as little as font size and margins of the code snippets.

kosmikus commented 6 years ago

Code blocks are by default translated into the hscode environment, and there are several ways to affect their styling.

This should work:

\documentclass{article}

%include polycode.fmt

\let\hscodestyle\small

\begin{document}

This is text:

> x = 2 -- this is code

\end{document}

In general, about everything in the output is configurable. You can completely change the way code is formatted by redefining the %subst code directive.

kindaro commented 6 years ago

I must be confused about something.

  1. I thought I should include lhs2TeX.fmt, not polycode.fmt. This is what I see in the guide to be found in this repo and this is what the error message said to me when I first tried to run lhs2TeX on a Literate Haskell file. With lhs2TeX.fmt, no hscode environment is present, no matter what style.

  2. Even if I include polycode.fmt, the code will be wrapped in hscode environment only for the poly style, not for math or tt. Consequently, for these styles there is no smallness to be observed.

So, the feature only works in 1 of 6 combinations of style and fmt file. This is what I meant when I wrote "no other environment is consistently introduced" in the opening post.

kosmikus commented 6 years ago

The correct documentation is https://github.com/kosmikus/lhs2tex/blob/master/doc/Guide2.lhs It recommends polycode.fmt (which in turn uses lhs2TeX.fmt and lhs2TeX.sty).

You are right that for math or tt, the hscode environment is not used. Personally, I consider all but poly and newcode modes to be deprecated. Everything you can do in these modes, you can do better usign poly mode, so I don't think it's a huge limitation that these are different.

You can still redefine %subst code for these modes if you need to use them, but I'd be interested to know what your use case is for these modes.

kindaro commented 6 years ago

I just checked out various styles and settled on math because it spaces out top-level definitions while poly produces a solid wall of code.

Should I submit a pull request that corrects the error message observed when one forgets to include lhs2TeX.fmt or polycode.fmt and removes deprecated modes from the output of lhs2TeX --help so that the next person discovering lhs2TeX doesn't get confused the way I was?

kosmikus commented 6 years ago

Personally, I never liked about math style that it introduces "arbitrary" spacing. If this is something you like, then in principle, you can simulate it in poly style as well.

I'm happy to accept a PR that

I'm planning to actually remove these modes at some point soon. Then they'll also disappear from the documentation and help messages completely.