3

How can I get this work:

\namedinclude{perceptron\_multilayer.h}{Multi-layer perceptron class definition}

where namedinclude is defined as

\newcommand{\namedinclude}[2]{\subsection{#1}#2\lstinputlisting{#1}}

Currently I get an "Emergency stop".

3
  • \subsection doesn't take two arguments, but I guess that doesn't matter in this case.
    – Werner
    Oct 7, 2014 at 6:34
  • The second argument should hold a description. Changed macro definition does not help.
    – user877329
    Oct 7, 2014 at 6:37
  • \lstinputlisting{a\_b} is not allowed.
    – egreg
    Oct 7, 2014 at 6:54

1 Answer 1

3

A token like \_ in the file name is not allowed, whereas _ is.

You can do

\newcommand{\namedinclude}[2]{%
  \subsection{\texttt{#1}}%
  #2\par
  \begingroup\def\_{_}%
  \lstinputlisting{#1}%
  \endgroup
}

or

\newcommand{\namedinclude}[2]{%
  \subsection{\texttt{\detokenize{#1}}}%
  #2\par
  \lstinputlisting{#1}%
}

but, in this case, you do

\namedinclude{perceptron_multilayer.h}{Multi-layer perceptron class definition}

However, I'd see better this as an environment:

\usepackage{xparse}
\newenvironment{codesection}{m}
 {\subsection{\texttt{\detokenize{#1}}}}
 {\par\lstinputlisting{#1}}

and use this as

\begin{codesection}{perceptron_multilayer.h}
Multi-layer perceptron class definition
with more words.

Even a new paragraph.
\end{codesection}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .