[Tex/LaTex] Using \verbatiminput on filenames with underscores

external filesincludetypewriterverbatim

I'm trying to include some source files using \verbatiminput{}. Some of these filenames have underscores in them. This works fine by default. But if I try to include the underscore package so that I can use underscores in text and within \texttt{}, suddenly my verbatim input stops working (it tries to read the file specified by the name before the underscore, and it just prints out the text after the underscore). How can I fix this?

Best Answer

The documentation of the underscore package states that _ must be avoided in file names. However, if you do need to use an _ in the file name, there is the [strings] package option which along with the \UnderscoreCommands can be used to specify a list of commands where an _ is needed.

Below, I have added the command \verbatiminput to the this list -- the others are directly from the documentation. Note that this list must be defined before the package is loaded.

\documentclass{article}
\usepackage{verbatim}

\newcommand{\UnderscoreCommands}{\do\verbatiminput%
\do\citeNP \do\citeA \do\citeANP \do\citeN \do\shortcite%
\do\shortciteNP \do\shortciteA \do\shortciteANP \do\shortciteN%
\do\citeyear \do\citeyearNP%
}
\usepackage[strings]{underscore}

\begin{document}
\texttt{Am going to include the file a_b here}% This still produces the correct output
\verbatiminput{a_b}
\end{document}
Related Question