When I write documents that contain source code examples and snippets the listings package is a great solution to include such things. Especially with the \lstinputlisting
command and firstline
/lastline
options you don't have to do copy/pasting and you are able to easily test your examples.
But what about the integration of command line sessions? For example a bash
session, a session in the Haskell interpreter hugs
or in the Python shell?
Ideally I would like to be able to write something like this in LaTeX:
\begin{hugs}[nostartbanner]
take 3 [1..]
reverse "hello world"
\end{hugs}
And get following output in my document:
Hugs> take 3 [1..] [1,2,3] Hugs> reverse "hello world" "dlrow olleh"
(perhaps plus some syntax highlighting)
That means such an environment should automagically start an external shell (e.g. hugs) and should paste the session output in the document. And for bonus points it should do some caching of the output.
Such an approach would help to reduce typos in anticipated output and copy-n-pasting orgies (which are of course error prone).
Also while writing documentation for a comment line program or a shell that is still in rapid development you don't have to manually check all your existing examples when the output is automatically integrated in the LaTeX document.
How can I solve this with LaTeX?
I found Listing language for “screen session”, but this question is just about the syntax highlighting part.
Best Answer
As @egreg recommended, the
bashful
package is specifically designed for this. Quoting from the user manual:Note that this requires
-shell-escape
option to be specified.Here I have used only basic
bash
commands. I suspect that you could do something similar with the Haskell interpreter.The first line of the bash script file begins with a
%
, so I started the listings form line 2, and left the first line blank. When you initiate the\bash
command, you provide a file name for the.sh
bash file and the output file. Then uselstinputlisting
to reincorporate the contents of that file back into the.tex
file.