[Tex/LaTex] Include figure (from macro) with underscore in filename

automationexternal filesnaming

I have a system of where a .tex is generated from a program. The final .tex file has a (simplified) structure like this:

\newcommand{\PlotFrame}[1]{%
\begin{frame}
\frametitle{...}
...
...
\includegraphics{#1}
\end{frame}}



\PlotFrame{File_1.png}
\PlotFrame{File_2.png}
...
\PlotFrame{File_n.png}

Now – when compiling this with pdflatex it complains at \PlotFrame{File_n.png} statements; because of the underscore. Unfortunately I do not have control over the filenames. Any suggestions of how I could keep the current structure with the \PlotFrame{} command – and let it accept arguments with underscore?

I would strongly prefer not having to escape it with \_.

Best Answer

You can define an additional macro like this:

\documentclass{article}

\usepackage{graphicx}

\newcommand{\PlotFrameB}[1]{%
\includegraphics{#1}\endgroup}

\def\PlotFrame{\begingroup 
\catcode`\_=12
\PlotFrameB}

\begin{document}


\PlotFrame{File_1.png}
\PlotFrame{File_2.png}
...
\PlotFrame{File_n.png}

\[ a_b \]

\end{document}

This will temporarily change the catcode for _ in the argument.

Related Question