Is there a way to set a figure label based on the basename of the eps file used?

  \caption{my caption} 

This pattern repeats itself at least 15 to 20 times in my document, so what is a convenient way to program/compile this support in? Probably just a touch beyond my Latex competence.

Edit: @texenthusiast referred to a useful snippet of code by @MMM: https://tex.stackexchange.com/a/116843/15717

This answer generally captures what I want so far. From the looks of this code, how can I take this code snippet to only consider the basename (no folder name or eps extension)?

Edit 2: @Fran provided an excellent answer https://tex.stackexchange.com/a/123511/33383 so far!

  • Related: Based on figure's file name as label see MMM's answer to global relabeling of all figures – texenthusiast Jul 10 '13 at 23:19
  • I use pdflatex and therefore my images are not .eps, but propably you can omit safely the extension. That work at least with .png, .jpg or .pdf images. – Fran Jul 10 '13 at 23:47
  • @Fran I see, I was able to omit the extension without issue – Kevin Lee Jul 10 '13 at 23:51

See how the new command \fig is defined here to do that:

\usepackage[demo]{graphicx} %remove demo for real images! 



\fig{MYFIGNAME001}{my caption} % 
\fig{MYFIGNAME002}{my other caption} % 

See figures \ref{fig:MYFIGNAME001} and \ref{fig:MYFIGNAME002}


Result show that labels are recognized correctly by \ref commands:


  • I think this captures what I want, how would I insert another argument for adjusting the width? set width=#3 and include another argument? And fig[2] to fig[3]? – Kevin Lee Jul 10 '13 at 23:42
  • 1
    @KevinLee: \newcommand\fig[3][.8\textwidth]{...\includegraphics[width=#1]{figures/#2}\caption{#3}\label{fig:#2}...} – Werner Jul 10 '13 at 23:48
  • @KevinLee, You caught the idea ;-) Up to nine (for more you will need another tricks) – Fran Jul 10 '13 at 23:53
  • @Werner Good point suggest a default width – Fran Jul 10 '13 at 23:55
  • 1
    I'd suggest something like \newcommand\fig[3][scale=1]{ \begin{figure} \centering \includegraphics[#1]{figures/#2} \caption{#3} \label{fig:#2} \end{figure} } which gives the possibility to use in the first optional argument not only width, but all the other keys known to \includegraphics, and if not used, maintains the natural size of the image. – Gonzalo Medina Jul 11 '13 at 0:28

Preserving as much as possible the figure syntax, here's a way with xparse:


\usepackage[demo]{graphicx} % demo just for the example


% save a copy of \includegraphics

  \cs_set_eq:NN \includegraphics \kevinincludegraphics
  \kevin_include_graphics:nn { #1 } { #2 }
\cs_new_protected:Npn \kevin_include_graphics:nn #1 #2
  % split the path into components
  \seq_set_split:Nnn \l_kevin_path_seq { / } { #2 }
  % get the last component (file name)
  \seq_pop_right:NN \l_kevin_path_seq \l_kevin_figurename_tl
\seq_new:N \l_kevin_path_seq
\tl_new:N \l_kevin_figurename_tl


  \caption{my caption} 

  \caption{my caption} 

Figures \ref{fig:MYFIGNAMEXXX} and \ref{fig:MYFIGNAMEYYY}


You can have the path as complex as you want. Don't add the extension, which isn't recommended anyway.

enter image description here


Using easyfig, Hope this is what you are looking for...


\usepackage{mwe} % http://ctan.org/pkg/mwe % For loading the example-image-X figures
\usepackage{easyfig} % http://ctan.org/pkg/easyfig
\usepackage{hyperref} % Loading order is important: cleveref after hyperref  
\usepackage{cleveref} % Automatic referencing with "\Cref" to get "Figure" Number
\section*{Fig:label as figure-name : An example}

Autocompletion of \Cref{fig:example-image-a} might not work
in some \TeX{} editors as explicit label command is not 
defined and figure environment is missing. 

\Figure[width=4cm,placement=h,caption={An image}]{example-image-a}

The default figure is centered image. Incase you are 
interested more options in \textit{easyfig} package, have a 
look at its documentation in \textit{texdoc easyfig} on \TeX{}Live 
or ctan website

\Figure[width=4cm,placement=h,caption={An image}]{example-image-b}

Just a second image from \textit{mwe} package for referencing    
\Cref{fig:example-image-b} second image. Another third image 
\Cref{fig:example-image-c} to fill the space

\Figure[width=4cm,placement=h,caption={An image}]{example-image-c}



enter image description here

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.