10

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

\begin{figure}
  \centering
  \includegraphics[width=.8\textwidth]{figures/MYFIGNAMEXXX.eps}
  \caption{my caption} 
  \label{fig:MYFIGNAMEXXX}
\end{figure}

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!

Kevin Lee
  • 215

3 Answers3

9

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

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

\newcommand\fig[2]{
\begin{figure}
  \centering
  \includegraphics[width=.8\textwidth]{figures/#1}
  \caption{#2} 
  \label{fig:#1}
\end{figure}
}

\begin{document}

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

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

\end{document}

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

MWE

Fran
  • 80,769
  • 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 00:28
  • @GonzaloMedina, good point also, but in my practice rarely the natural size of the images is the good one. For me a better default could be some fixed limits as [width=.8\textwidth,height=.4\textwidth,keepaspectratio] – Fran Jul 11 '13 at 00:43
7

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

\documentclass{article}

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

\usepackage{xparse,letltxmacro}

% save a copy of \includegraphics
\AtBeginDocument{\LetLtxMacro\ORGincludegraphics\includegraphics}

\ExplSyntaxOn
\NewDocumentEnvironment{autofigure}{o}
 {
  \IfNoValueTF{#1}
   {\figure[#1]}
   {\figure}
  \cs_set_eq:NN \includegraphics \kevinincludegraphics
 }
 {
  \label{fig:\l_kevin_figurename_tl}
  \endfigure
 }
\NewDocumentCommand{\kevinincludegraphics}{O{}m}
 {
  \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
  \ORGincludegraphics[#1]{#2}
 }
\seq_new:N \l_kevin_path_seq
\tl_new:N \l_kevin_figurename_tl
\ExplSyntaxOff

\begin{document}

\begin{autofigure}[htp]
  \centering
  \includegraphics[width=.8\textwidth]{figures/MYFIGNAMEXXX}
  \caption{my caption} 
\end{autofigure}

\begin{autofigure}[ht]
  \centering
  \includegraphics[width=.8\textwidth]{figures/chap1/MYFIGNAMEYYY}
  \caption{my caption} 
\end{autofigure}

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

\end{document}

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

enter image description here

egreg
  • 1,121,712
4

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

Code:

\documentclass[12pt]{article}
\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
\Crefname{figure}{Figure}{Figures}
\pagestyle{empty}
\begin{document}
\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}

\end{document}

Output:

enter image description here