# How to pass use lstlisting or lstinline in a command

I'm trying to define a command that typesets Maxima code by taking three arguments - the command number, the input text, and the output math. I want the input text - the second argument - formatted as a listing. This is the closest I've gotten:

\documentclass{book}

\usepackage{listings}

\newcommand{\maximaio}[3]{
\begin{tabular}{l l}
(\%i#1) &
\lstinline!#2! \\
(\%o#1) &
$#3$
\end{tabular}
}

\begin{document}

\maximaio{1}{y
/
(y+1);}{{y}\over{y+1}}

\end{document}


If you run this, you'll see that the newlines aren't correctly formatted in the second argument. Instead of "y", "/", and "(y+1);" on three separate lines, I get "y / (y+1);" on a single line.

Can anybody suggest how to pass a listing into a command like this?

• Beside the problems with verbatim as argument: \lstinline doesn't print (or like) newlines. – Ulrike Fischer Feb 22 '17 at 9:14

You can't easily pass verbatim content as part of macro arguments. So, I suggest an alternative option:

\documentclass{article}

\usepackage{fancyvrb}

\newcommand{\maximaio}[2]{%
\begin{tabular}{l l}
(\%i#1) &
\BUseVerbatim{MaximaCode} \\
(\%o#1) &
$#2$
\end{tabular}
}

\begin{document}

\begin{SaveVerbatim}{MaximaCode}
y
/
(y+1);
\end{SaveVerbatim}
\maximaio{1}{\frac{y}{y+1}}

\end{document}


In the above example, SaveVerbatim is used to store the code inside a box called MaximaCode. With a call to \maximaio{<cmd>}{<output>}, the code is processed and placed inside a tabular.