1

I would like to have a macro to change the extension of a file name. For example, my macro needs to take an argument like Figures/Fig.xxx and return me Figures/Fig0.yyy.

I found a solution in Extract filename from path. It is as follows:

\makeatletter
\DeclareRobustCommand{\changefilename}[1]{%
    \begingroup
    % \lstname seems to change hyphens into \textendash
    \def\textendash{-}%
    \filename@parse{#1}%
    \edef\filename@base{\detokenize\expandafter{\filename@base}}%
    \texttt{\filename@area\filename@base0.yyy}%
    \endgroup
}
\makeatother

But, when I use the output of this macro in \includegraphics{}, I get an error.

3
  • 2
    You can't put your macro \changefilename inside \includegraphics because it has typesetting commands, thus it doesn't expand to “plain text”. Try this definition: \DeclareRobustCommand{\changefilename}[2][\relax]{% \begingroup \def\textendash{-}% \filename@parse{#2}% \edef\filename@base{\detokenize\expandafter{\filename@base}}% \ifx\relax#1% \texttt{\filename@area\filename@base0.yyy}% \else \xdef#1{\filename@area\filename@base0.yyy}% \fi \endgroup } then you can do \changefilename[\foo]{Figures/Fig.xxx} \includegraphics{\foo}. – Phelype Oleinik Jun 24 '20 at 12:20
  • @PhelypeOleinik The expl3 parser would work there ... – Joseph Wright Jun 24 '20 at 12:25
  • @PhelypeOleinik Your suggestion is good, can you make it as answer? It will help others... – MadyYuvi Jun 24 '20 at 14:17

Your Answer

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

Browse other questions tagged or ask your own question.