2

In my application I (by means of a latex generation) create a table filled with figures. Now since those figures can have difficult names I need to make sure the generator makes each legal windows name a legal latex filepath.

I started with the percentage sign, as I know for sure that is in some of the files. (It's a left-over from a name that contained a "/" sign which is converted by another part to "%2F").

Now this broke quite a few things, as I obviously use % a lot for code readibility, the latex file right now:

\documentclass{article}
\usepackage[a4paper, portrait]{geometry}
\usepackage{graphicx}
\usepackage{tabu}


\catcode`\%=12
\begin{document}
\begin{table}
\centering
\begin{tabu}{c}
\includegraphics{{"testing %"}}
\\%
\includegraphics{test2}
\end{tabu}
\end{table}

\end{document}

Now (as one can expect) the problem is that the percentage sign is no longer considered a "continu line here" sign, but instead a simple character, and is printed as such. I tried including the \catcode inside the table cell:

\begin{tabu}{c}
\catcode`\%=12
\includegraphics{{"testing %"}}
\catcode`\%=14
\\%
\includegraphics{test2}
\end{tabu}

But this breaks the latex compiler with "file ended scanning use of \tabu@collectbody". So how to make this work?

1 Answer 1

5

You can make a command that expands to a safe percent

\documentclass{article}
\usepackage[a4paper, portrait]{geometry}
\usepackage{graphicx}
\usepackage{tabu}


\catcode`\%=12
\newcommand\pcnt{%}
\catcode`\%=14

\begin{document}
\begin{table}
\centering
\begin{tabu}{c}
\includegraphics{"testing \pcnt"}\\% this is a comment
\includegraphics{test2}
\end{tabu}
\end{table}

\end{document}
2
  • How would I use that command if the percent is in the middle of the file name? Like \includegraphics{"test\pcnting"}? Another answer mentions to put \catcode`\%=12 before the includegraphics.
    – mxmlnkn
    Apr 22, 2020 at 17:37
  • 1
    @mxmlnkn test\pcnt ing Nov 30, 2021 at 7:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .