I'd like to simplify my code appendix with this command definition:
\usepackage{listings}
% ...
\newcommand{\CodeListing}[1]{%
\lstinputlisting[caption=#1]{#1}%
}
That is, the caption should be the file name. However, the caption argument and the file name argument seem to handle underscores differently. For the former, I need to escape them (\_), but for the latter, non-escaped underscores work fine. How can I solve this problem, if I only want to pass the file name once?
! Package Listings Error: File `spt2/my\T1\textunderscorefile(.m)' not found.
Here is a minimal example:
\documentclass[a4paper]{scrreprt}
\usepackage{listings}
\begin{document}
\newcommand{\codelst}[1]{
\lstinputlisting[caption=#1]{#1}
}
\codelst{/home/tim/projekt/matlab/path/save\_plot\_as.m}
\end{document}
which works fine for underscore-less paths.