3

Hi I've got a strange problem. I've defined a new command to print Linux commands line in the document:

\documentclass[12pt,a4paper]{article}
\usepackage{listings}
\newcommand{\shellcmd}[1]
{
  \begin{lstlisting}
    {#1}
  \end{lstlisting}
}

\begin{document}
\shellcmd{gdb \textit{program core}}
\end{document}

If I don't use my own defined command but use \begin...\end then it compiles. If I however use the command then I get:

! Missing \endcsname inserted.
<to be read again> 
                   \protect 
l.11 \shellcmd{gdb \textit{program core}}

Not sure how to resolve this. I haven't seen a similar problem online.

  • 2
    Welcome to TeX.sx! You can't put a listings environment in the argument to another command. Have a look at the \lstinline command of the same package. – egreg Apr 28 '13 at 16:01
  • Thanks. This works for the most basic input. For example \lstinline{gdb \texttt{program}} doesn't work. – s5s Apr 28 '13 at 16:25
  • 1
    The purpose of \lstinline is to typeset an "enhanced verbatim", so formatting instructions like \texttt don't make much sense inside it. – egreg Apr 28 '13 at 16:26
  • 3
    What you're trying to do does not and cannot work. tex.ac.uk/cgi-bin/texfaq2html?label=verbwithin – kahen Apr 28 '13 at 16:44
5

LaTeX is not known for its transparent error messages, so this one has nothing to do with the actual problem. In fact, the issue is that the lstlisting environment is a form of "verbatim" text, which requires special care in being used as input. This is the subject of at least one previous question (actually, I found several; this one seemed similar to yours), and the reason is that such an environment needs its contents to be "pristine" and not previously scanned by TeX. As egreg suggests in his comment, you should use \lstinline instead; however, that too cannot be buried in another macro, for the same reason. Use it directly instead of \shellcmd.

|improve this answer|||||
  • OK. It doesn't work with minted either. \lstinline seems to only accept the most basic input. I think I'll just preprocess my latex file with a preprocessor first... – s5s Apr 28 '13 at 16:24
  • 1
    You should read the listings manual. You can customize the keywords and their style. – Ryan Reich Apr 28 '13 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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