Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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
    
You can try to use a special keyword formating for Prelude>. –  Kurt Sep 4 '12 at 22:01
    
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'm not sure to understand you quite right. I thaught to use morekeywords. See documentation (texdoc listings) page 20, chapter "3.2 Language definitions". –  Kurt Sep 5 '12 at 13:39
    
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
add comment

2 Answers

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.

share|improve this answer
    
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
add comment
up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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