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}
Is this what you want?
Code
\documentclass{article}
\usepackage{listings,xcolor}
\lstset{
language=Octave,
moredelim=[s][\color{gray}]{octave:}{>}
}
\begin{document}
\begin{lstlisting}
octave:1> A=eye(4)
A =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
octave:2> det(A)
ans = 1
octave:3>
\end{lstlisting}
\end{document}
Output
moredelim
The moredelim
key defines new delimiters that are used to format text manually between them. In this case the delimiters are octave:
and >
and they are not thrown away (that would happen if you use [is]
instead of [s]
).
Confer to manual section 3.3 and 4.5 to read more about the use of delimiters.
Best Answer
This might answer some of your questions. For the prompt, you may use the
literate
option. Two ways are shown to color output. The first way is not very good but it gets the job done for very short and simple output. The other way is to define an output environment. The result from the two are different : when using the escape character, you are leaving the listings mode and the spacing between the letters is not the same. See the output text in red below.The output is