1

For the purpose of managing a large reference document with many included files, I include links to these files in the margin. Broken down to an MWE, this is what I do:

\documentclass{article}
\usepackage{hyperref,marginnote,graphicx}

\newcommand{\inclfile}[1]{%
  \includegraphics{#1}%
  \def\filename{\detokenize{#1}}%
  \marginnote{%
      \rlap{\href{run:\filename}{\texttt{\filename}}}%
  }%
}

\begin{document}
  \inclfile{somefile.png}

  \inclfile{other_file.png}

  \inclfile{{third.file}.png}
\end{document}

The result is as expected (using some dummy images with appropriate names):

enter image description here

The detokenization has to happen for files with special characters in their names, e.g. the not so uncommon underscore.

Now, we need the braces for the third file so we don't skrew up the magics of graphicx. However, the braces have to be removed as the link won't work like this.

I thought that this usage of stringstrings should do the job:

\def\filename{\convertchar{\convertchar{\detokenize{#1}}{\{}{}}{\}}{}}%

I escape the braces because unescaped ones surely won't work. Now, this causes pdflatex to terminate with TeX capacity exceeded.

How can I remove the braces from the parameter of inclimage so I can use the string as parameter for \includegraphics, regular text and the URL of \href?

1

The best is not having braces in the file name to begin with; the grffile package allows file names with multiple periods in them.

If you can't do with it, you can use the facility already present in LaTeX for getting a file name:

\documentclass{article}
\usepackage{marginnote}
\usepackage[demo]{graphicx}
\usepackage{hyperref}

\makeatletter
\newcommand{\inclfile}[2][]{%
  \includegraphics[#1]{#2}%
  \filename@parse{#2}%
  \edef\filename{\detokenize\expandafter{\filename@base}.\detokenize\expandafter{\filename@ext}}%
  \marginnote{%
      \rlap{\href{run:\filename}{\texttt{\filename}}}%
  }%
}
\makeatother

\begin{document}
  \inclfile{somefile.png}

  \inclfile{other_file.png}

  \inclfile{{third.file}.png}
\end{document}

enter image description here

  • I've changed the question so it is more about using graphicx correctly. I'll repost my question about replacing characters in strings in a different way. Thanks again! – Raphael May 27 '15 at 15:23

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.