As @egreg recommended, the bashful
package is specifically designed for this. Quoting from the user manual:
...bashful provides a convenient interface to TEX’s primitive \write18
—the execution of shell com- mands from within your input files, also known as shell escape. Text between \bash
and \END
is executed by bash, a popular Unix command line interpreter. Various flags control whether the executed commands and their output show up in the printed document, and whether they are saved to files.
Although provisions are made for using shells other than bash, this package may not operate without modifications on Microsoft’s operating systems.
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 use lstinputlisting
to reincorporate the contents of that file back into the .tex
file.
\documentclass{standalone}
\usepackage{xcolor}
\usepackage{bashful}
\usepackage{listings}
\lstdefinestyle{BashInputStyle}{
language=bash,
firstline=2,% Supress the first line that begins with `%`
basicstyle=\small\sffamily,
numbers=left,
numberstyle=\tiny,
numbersep=3pt,
frame=tb,
columns=fullflexible,
backgroundcolor=\color{yellow!20},
linewidth=0.9\linewidth,
xleftmargin=0.1\linewidth
}
\lstdefinestyle{BashOutputStyle}{
basicstyle=\small\ttfamily,
numbers=none,
frame=tblr,
columns=fullflexible,
backgroundcolor=\color{blue!10},
linewidth=0.9\linewidth,
xleftmargin=0.1\linewidth
}
\begin{document}
\bash[verbose,scriptFile=hello.sh,stdoutFile=hello.tex]
echo "Hello World!"
echo "Today is" `date`
echo ""
echo "Disk usage is:"
df
\END
\par\noindent
Executing the following code in \textbf{bash}
\lstinputlisting[style=BashInputStyle]{hello.sh}
%
yields the following output:
\lstinputlisting[style=BashOutputStyle]{hello.tex}
\end{document}
use it this way:
\documentclass{beamer}
\usepackage{listings}
\lstset{
language=Java,
tabsize=8,
keepspaces,
extendedchars=true,
rulecolor=\color{black},
basicstyle=\footnotesize,
aboveskip=5pt,
upquote=true,
columns=fixed,
showstringspaces=false,
extendedchars=true,
breaklines=true,
frame=single,
showtabs=true,
showspaces=false,
showstringspaces=false,
}
\begin{document}
\defverbatim\lst{%
\begin{lstlisting}
public class Teddy {
public MethodName(String name, int var) {
this.name = name;
}
}
\end{lstlisting}
}
\begin{frame}
\lst
\end{frame}
\end{document}

Best Answer
Use the options
breaklines=true
andpostbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space}
for placing a red arrow at the beginning of the broken line to emphasize the line break.With the
minted
package you get nice line breaking and syntax highlighting out-of-the-box. Simply specify thebreaklines
option on your snippet. The downside is that you have to process the document with the--shell-escape
option because the external programpygmentize
is used to format the source code.