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}


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

\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}



How can I solve this?


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.