1

I have made a custom command which includes an image if it exists and writes an missing file text if it does not. The problem is that if I use underscore in the file name or path it is does not print the path right.

\documentclass[a4paper, 10pt]{report}

\usepackage[utf8]{inputenc}
\usepackage[export]{adjustbox}

\newcommand{\includegraphicsmaybe}[2]{
    \IfFileExists{#2}
        {\includegraphics[valign=c, width=#1]{\detokenize{#2}}}
        {\fbox{\parbox[c][#1][c]{#1}{\center \textbf{File not found}\\ \detokenize{#2}} }}}

\begin{document}
\centering
\includegraphicsmaybe{0.4 \textwidth}{example-image}
\includegraphicsmaybe{0.4 \textwidth}{something.png}\\[1cm]
\includegraphicsmaybe{0.4 \textwidth}{something_1.jpg}
\includegraphicsmaybe{0.4 \textwidth}{something_1.png}

\end{document}

Result

How can I solve this?

1

Use \usepackage[T1]{fontenc} or use \ttfamily to typeset the filename.

OT1 fonts do not have a _ in the _ slot, except for the special case of typewriter monospace fonts.

Not the answer you're looking for? Browse other questions tagged or ask your own question.