我怎样才能完成这项工作:
\namedinclude{perceptron\_multilayer.h}{Multi-layer perceptron class definition}
其中namedinclude
定义为
\newcommand{\namedinclude}[2]{\subsection{#1}#2\lstinputlisting{#1}}
目前我得到了“紧急停止”。
答案1
\_
不允许使用文件名中的类似标记,而_
允许。
你可以做
\newcommand{\namedinclude}[2]{%
\subsection{\texttt{#1}}%
#2\par
\begingroup\def\_{_}%
\lstinputlisting{#1}%
\endgroup
}
或者
\newcommand{\namedinclude}[2]{%
\subsection{\texttt{\detokenize{#1}}}%
#2\par
\lstinputlisting{#1}%
}
但在这种情况下,你需要
\namedinclude{perceptron_multilayer.h}{Multi-layer perceptron class definition}
然而,我更愿意将其视为一个环境:
\usepackage{xparse}
\newenvironment{codesection}{m}
{\subsection{\texttt{\detokenize{#1}}}}
{\par\lstinputlisting{#1}}
并将其用作
\begin{codesection}{perceptron_multilayer.h}
Multi-layer perceptron class definition
with more words.
Even a new paragraph.
\end{codesection}