2

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:

\begin{lstlisting}
Prelude> let fibs = 0:1:zipWith (+) fibs (tail fibs) in fibs !! 10
55
\end{lstlisting}

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

3
  • I tried to capture the code with escapeinsinde and use escapebegin and escapeend to start a new listing environment, but unfortunately that just loops. Commented Aug 30, 2012 at 12:54
  • Ok, that is one step. But what about the output, i.e. all lines not starting with Prelude>? Commented Sep 5, 2012 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. Commented Sep 5, 2012 at 13:45

2 Answers 2

2

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

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{listings}

\newcommand{\notCode}{\color[rgb]{0.0,0.35,0.6}}

\lstset{
  language=Haskell,
  basicstyle=\ttfamily,
  showstringspaces=false,
  breaklines=true,
  keywordstyle={\textit},
  moredelim=[is][\notCode]{@}{@},
}

\begin{document}

\begin{lstlisting}
@Prelude>@ let fibs = 0:1:zipWith (+) fibs (tail fibs) in fibs !! 10
@55@
\end{lstlisting}

\end{document}

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.

2
  • Regarding automation: Maybe a simple sed script to apply the delimiter around all lines not starting with Prelude> would suffice?
    – Daniel
    Commented Oct 5, 2012 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. Commented Oct 6, 2012 at 17:42
0

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

\documentclass{article}

\usepackage{listings}
\usepackage{framed}

\lstset{language=Haskell,basicstyle=\small\sffamily,stringstyle=\itshape}

\begingroup
\catcode`\>=\active%
\catcode`\^^M=\active%
\gdef\foo{%
\catcode`\>=\active%
\catcode`^^M=\active%
\def > ##1^^M{\char`\>{} \lstinline!##1!\par}%
\def ^^M{\par}%
}%
\endgroup

\newenvironment{ghci}{%
\begin{framed}%
\begingroup%
\small\ttfamily%
\foo%
}{%
\endgroup%
\end{framed}%
}

\begin{document}
\pagestyle{empty}
\begin{ghci}
*Count> let s = "hello stackexchange"
*Count> let x = count 0 [0..100000000]
*Count> x
Just *** Exception: stack overflow
\end{ghci}
\end{document}

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .