图中自动标记标签?自动标记?

图中自动标记标签?自动标记?

有没有办法根据所使用的 eps 文件的基本名称设置图形标签?

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

这种模式在我的文档中至少重复了 15 到 20 次,那么有什么方便的方法来编程/编译这种支持?可能只是超出了我的 Latex 能力。

编辑:@texenthusiast 引用了@MMM 提供的一段有用的代码片段:https://tex.stackexchange.com/a/116843/15717

这个答案基本上概括了我目前想要的。从这段代码来看,我怎样才能让这段代码片段只考虑基本名称(没有文件夹名称或 eps 扩展名)?

编辑 2:@Fran 提供了一个很好的答案https://tex.stackexchange.com/a/123511/33383迄今为止!

答案1

\fig看看这里如何定义新命令来执行此操作:

\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}

结果表明标签被\ref命令正确识别:

平均能量损失

答案2

尽可能保留figure语法,这里有一种方法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}

您可以根据需要将路径设置得尽可能复杂。不要添加扩展名,无论如何都不建议这样做。

在此处输入图片描述

答案3

使用易图,希望这就是您所寻找的……

代码:

\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}

输出

在此处输入图片描述

相关内容