In LaTeX is there a way to include the filename of a graphic into a caption?
I have a LaTeX document with a large number of drawings included from external files with \includegraphics{bla.pdf}
. The drawings are done in Inkscape. This all works fine. However sometimes it is hard to keep track of all the drawings and their source files. Hence I'm looking for a way to include the source file name, in the example bla.pdf
, into the caption. Not being a LaTeX expert I haven't found a way to automatically access the filename string from the caption. 开发者_如何学C Using Google-foo didn't yield a result either. Is there an existing way to refer to the filename and include it in the caption, like \caption{A fancy drawing of bla (\filename}
?
You can capture the image filename by patching a part of the nested calls inside \includegraphics
:
\documentclass{article}
\usepackage{graphicx,etoolbox}
\makeatletter
\patchcmd{\Gin@setfile}% <cmd>
{\ProvidesFile}% <search>
{\xdef\imgfilename{#3}\ProvidesFile}% <replace>
{}{}% <success><failure>
\makeatother
\begin{document}
\begin{figure}
\centering
\includegraphics[width=.5\linewidth]{example-image}
\caption{Image: \imgfilename}
\end{figure}
\end{document}
\imgfilename
will contain the filename used in \includegraphics
only after it's used.
Redefine \includegraphics
. For the case of no arguments (width=3cm, scale=2 or something like this) we have
\let \saveinclude \includegraphics
\def \includegraphics#1{\def\filename{#1}\saveinclude{#1}}
The use
\includegraphics{bla.pdf}
\caption{A fancy drawing of bla (\filename)}
精彩评论