I am writing a document that contains parts of an compiler interpreter session (GHCi in this case, but this is not relevant). I have a carefully setup style for Haskell code that I would like to use there. But the prompt and especially the output is not Haskell code and should be printed differently.

Here is an example:

Prelude> let fibs = 0:1:zipWith (+) fibs (tail fibs) in fibs !! 10

What is the most elegant way to apply different style to what follows the Prelude > and the rest?

  • I tried to capture the code with escapeinsinde and use escapebegin and escapeend to start a new listing environment, but unfortunately that just loops. – Joachim Breitner Aug 30 '12 at 12:54
  • Ok, that is one step. But what about the output, i.e. all lines not starting with Prelude>? – Joachim Breitner Sep 5 '12 at 7:11
  • I know about morekeywords, but I also want every line not starting with Prelude> to be style differently from the code following Prelude> – in the above example, the 55. – Joachim Breitner Sep 5 '12 at 13:45

One approach would be to use the moredelim option to apply a different formatting (and kill syntax highlighting) on certain parts:





@Prelude>@ let fibs = 0:1:zipWith (+) fibs (tail fibs) in fibs !! 10


enter image description here

A possible disadvantage is that you have to do this "by hand". For the Prelude> part you may use the morekeywords option to automatically apply its style, but for the lines not starting with Prelude> this will be more difficult.

  • Regarding automation: Maybe a simple sed script to apply the delimiter around all lines not starting with Prelude> would suffice? – Daniel Oct 5 '12 at 18:20
  • Thanks for the suggestion. I guess once I start modifying the highlighted code, I can just put the haskell stuff in lstinline and format the rest using \ttfamily. – Joachim Breitner Oct 6 '12 at 17:42

Here is a partial answer that is sufficient for my use case, e.g. formatting only the commands and leaving the rest verbatim:




\def > ##1^^M{\char`\>{} \lstinline!##1!\par}%
\def ^^M{\par}%


*Count> let s = "hello stackexchange"
*Count> let x = count 0 [0..100000000]
*Count> x
Just *** Exception: stack overflow

This produces the following output

enter image description here

It is still not entirely satisfying, as I cannot use lstlistings’s frame options, which I had to imitate using the nice adjustbox package. Also, it breaks if there is a > in the output somewhere, but that is something I can live with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.