For example, those lines works perfectly fine.

\includegraphics[scale=1.1]{9709_s10_qp_11_Q3} \\
\flushright \verb=9709_s10_qp_11_Q3= \\[2ex]
\includegraphics[scale=1.1]{9709_s10_qp_12_Q7} \\
\flushright \verb=9709_s10_qp_11_Q3= \\[2ex]
\includegraphics[scale=1.1]{9709_s10_qp_13_Q1} \\
\flushright \verb=9709_s10_qp_11_Q3= \\[2ex]
\includegraphics[scale=1.1]{9709_s11_qp_11_Q8} \\

To save time, I want to define a new command mytopic (to use in enumerate),

\item { \flushright \verb= {#1} = } \\
    \includegraphics[scale=1.1]{#1} \\[2ex]

The problem arises as I have lots of underscore in my filenames, which I don't really want o rename.

How can I adapt it so that it take the filename as well as enabling the verb output on the filename? I tried to use \texttt{} but it seems to have the same problem.

  • 2
    For one thing, the issue is not only the underscore, but you cannot place \verb inside a macro definition. Feb 20, 2020 at 18:21
  • 2
    Replace \verb= {#1} = with \detokenize{ {#1} }. I believe that is the issue, not the underscore. i.e., try this definition: \newcommand{\mytopic}[1]{\item \flushright \detokenize{ {#1} } \\ \includegraphics[scale=1.1]{#1} \\[2ex] } Feb 20, 2020 at 18:30
  • unrelated but \flushright is doing nothing in { \flushright \verb= {#1} = } as the scope ends at the } before it can affect the paragraph setting. Feb 20, 2020 at 18:55
  • You want to handle underscores in command arguments (in commands you write yourself), and ship them off to other commands.
    – vonbrand
    Feb 20, 2020 at 22:24

1 Answer 1


The command \path from hyperref is handy here:

\item {\path{#1}} \\
    \includegraphics[scale=1.1]{#1} \\[2ex]


\path allows underscores and other characters used for file paths -- exactly what you need.

  • Is \path a recent addition to hyperref? I can't find this command in the documentation or in the source code?
    – user30471
    Feb 20, 2020 at 22:40
  • 1
    @Andrew It's defined in url.sty that's loaded by hyperref.
    – egreg
    Feb 20, 2020 at 22:43
  • @egreg I am so used to always loading hyperref, that I forget where the goodies are :)
    – Boris
    Feb 20, 2020 at 22:44
  • Worked like a charm! \path very useful indeed!
    – CasperYC
    Feb 21, 2020 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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