# Using reserved characters as argument to custom macro

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".

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

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}