Is it possible to show the figure name (fig.eps) using showkeys package

I have a lot of figures in my document, and which are recalled multiple times in the same document. It makes cumbersome to check in the TeX file the name of the figures again and again when it is needed to be recalled.

Does there exist a solution which imprints the figure name (too) along with its label, just like the \showkeys package does to show the labels?

\documentclass[10pt]{article}
\usepackage{showkeys}
\usepackage{todonotes}

\begin{document}

\begin{figure}[t]
\centering
%   \captionsetup{justification=centering}
\includegraphics[width=.7\linewidth]{fig.eps}
\caption{This is caption.}
\label{f02}
\end{figure}

\end{document}


EDIT 2: Relevant to answer by CarLaTeX. In multiple figures environment, the file names overlap, as below:

\documentclass[twoside]{IEEEtran}
\usepackage{subcaption}
\usepackage{showkeys}
\usepackage{todonotes}
\usepackage{marginnote}
\let\oldig\includegraphics
\renewcommand{\includegraphics}[2][]{\oldig[#1]{#2}\marginnote{\fbox{#2}}}

\begin{document}

\begin{figure}[t]
\centering
\includegraphics[width=.7\linewidth]{example-image-a}
\caption{This is caption $f$.}
\label{f02}
\end{figure}
\begin{figure*}[ht]
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{ }
\label{fig2a}
\end{subfigure}
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-c}
\caption{ }
\label{fig2b}
\end{subfigure} \\%
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{ }
\label{fig2c}
\end{subfigure} %
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{ }
\label{fig2d}
\end{subfigure}
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-c}
\caption{ }
\label{fig2e}
\end{subfigure} %
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{ }
\label{fig2f}
\end{subfigure} %
\caption{Kinetic energy of  $f(t)$.         \todo[inline]{file\_2.m        } }
\label{f2}
\end{figure*}
\end{document}

• You would like to write the file name, in addition to the label? – Bernard May 25 '17 at 23:43
• Yes, the file name. – kaka May 26 '17 at 3:19

2 Answers

Here's a solution with \marginnote:

\documentclass[10pt]{article}
\usepackage{showkeys}
\usepackage{todonotes}
\usepackage{marginnote}
\let\oldig\includegraphics
\renewcommand{\includegraphics}[2][]{\oldig[#1]{#2}\marginnote{\fbox{#2}}}

\begin{document}

\begin{figure}[t]
\centering
%   \captionsetup{justification=centering}
%\includegraphics[width=.7\linewidth]{fig.eps}
\includegraphics[width=.7\linewidth]{example-image-a}
\caption{This is caption.\label{f02}}
\end{figure}

\end{document}


Edit: The problem of the underscore in the name of the image can be easily avoided with \detokenize (see here, for example).

For the problem of the overlapping file names, since you document class doesn't have enough space in the margins, I suggest two alternative solutions.

The first one is with \todo[inline,color=white,nolist]{\detokenize{...}} (the nolist option makes this note not to be printed in todonote list), but this changes a bit the spacing in the original document:

\documentclass[twoside]{IEEEtran}
\usepackage[T1]{fontenc}% <-- to have _ in the name of the image included
\usepackage{subcaption}
\usepackage{showkeys}
\usepackage{todonotes}

\let\oldig\includegraphics
% \detokenize to have the _ printed, nolist not to heve to todonote printed in the todonote list:
\renewcommand{\includegraphics}[2][]{\oldig[#1]{#2}\todo[inline,color=white,nolist]{\detokenize{#2}}}

\begin{document}

\begin{figure}[t]
%\centering
\includegraphics[width=.7\linewidth]{example-image-a}
\caption{This is caption $f$.}
\label{f02}
\end{figure}
\begin{figure*}[ht]
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{fig_2.eps}
\caption{\label{fig2a}}
\end{subfigure}
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-c}
\caption{\label{fig2b}}
\end{subfigure} \\%
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{\label{fig2c}}
\end{subfigure} %
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{\label{fig2d}}
\end{subfigure}
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-c}
\caption{\label{fig2e}}
\end{subfigure} %
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{\label{fig2f}}
\end{subfigure} %
\caption{Kinetic energy of  $f(t)$.\todo[inline]{file\_2.m}\label{f2}}
\end{figure*}
\end{document}


With this output:

The second solution is with \tikz[overlay]{...}, I prefer this one because it doesn't changes the spacing in the original document.

If you want to have a transparent node background, remove fill=white. If you wanto to change to note (node) position, change the coordinates at (-2,.5):

\documentclass[twoside]{IEEEtran}
\usepackage[T1]{fontenc}% <-- to have _ in the name of the image included
\usepackage{subcaption}
\usepackage{showkeys}
\usepackage{todonotes}

\let\oldig\includegraphics
\renewcommand{\includegraphics}[2][]{\oldig[#1]{#2}\tikz[overlay]{%
\node[draw, fill=white] at (-2,.5) {\detokenize{#2}};}
}% \detokenize to have the _ printed

\begin{document}

\begin{figure}[t]
%\centering
\includegraphics[width=.7\linewidth]{example-image-a}
\caption{This is caption $f$.}
\label{f02}
\end{figure}
\begin{figure*}[ht]
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{fig_2.eps}
\caption{\label{fig2a}}
\end{subfigure}
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-c}
\caption{\label{fig2b}}
\end{subfigure} \\%
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{\label{fig2c}}
\end{subfigure} %
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-b}
\caption{\label{fig2d}}
\end{subfigure}
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-c}
\caption{\label{fig2e}}
\end{subfigure} %
\begin{subfigure}{.49\textwidth}
%       \centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{\label{fig2f}}
\end{subfigure} %
\caption{Kinetic energy of  $f(t)$.\todo[inline]{file\_2.m}\label{f2}}
\end{figure*}
\end{document}


With this output:

• It works, thanks. One problem I am still having is: when the file name contains underscore then I get an error. For instance when name of file is fig_2.eps Any workaround for this too? Second problem is when there are multiple figures in the figure environment, the file names overlap. Check my edited question above please. – kaka May 27 '17 at 22:04
• @kaka I solved both problems, see my renewed answer. I suggest to you to use the solution with \tikz[overlay]{...}. – CarLaTeX May 28 '17 at 8:18
• Works like a charm. Thank you so much for such a detailed answer and perfect solution. – kaka May 28 '17 at 17:58

Here's one possibility:

\documentclass[10pt]{article}
\usepackage{graphicx}

\begin{document}

\let\origincludegraphics\includegraphics
\def\includegraphics[#1]#2{%
% \marginpar and \vadjust would be nice here,
% but we're in the wrong mode for either
\vbox to 0pt{\hbox to \linewidth{\hfil\hbox to 0pt{file: #2\hss}}\vss}\par
\origincludegraphics[#1]{#2}}

\begin{figure}[t]
\centering
\includegraphics[width=.7\linewidth]{spiral.pdf}
\caption{This is caption.}
\label{f02}
\end{figure}

\end{document}


This doesn't have anything to do with the \label, and can't, because \label can't ‘see’ the \includegraphics argument (which is what you want to display, in your clarified question).

This illustrates a general technique (called ‘monkeypatching’ in some circles!) where you save the original version of a command with \let, and then define a new version of it which does some useful work before or after in turn invoking that original version.

(The contents of the \vbox could be more or less sophisticated than what appears there – what's there is probably good enough for a draft-mode functionality)