将图片文件名映射到图片编号上

将图片文件名映射到图片编号上

我正在寻找一种方法来在图形文件名和其图形编号之间建立映射。您可以在下面找到 MWE,其中指定了所需的输出。

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{duckuments}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Some code that provides the same output as the following fragment:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Figure map:
\begin{itemize}
\item Figure 1: example-image-a
\item Figure 2: example-image-b
\item Figure 3: example-image-c
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{figure}
\centering
\includegraphics{example-image-a}
\label{fig:alef}
\caption[Alef figure.]{Alef.}
\end{figure}

\begin{figure}
\centering
\includegraphics{example-image-b}
\label{fig:bet}
\caption[Bet figure.]{Bet.}
\end{figure}

\begin{figure}
\centering
\includegraphics{example-image-c}
\label{fig:gimel}
\caption[Gimel figure.]{Gimel.}
\end{figure}

\end{document}

次优解决方案发布于获取单个图形文件的标题编号,但有时我需要使用, []\listoffigures参数来生成真正的目录\caption

我想过创建一个副本\listoffigures,但这似乎不可行(修改命令以获取替代图表列表)。

答案1

在另一个主题中(如何对按图号索引的列表进行排序?)您要求采用更自动化的方式来完成此任务。

既然你说这是一份要发送给出版商的清单,我认为目标不是将文本包含在实际文档中。因此,我认为最好将这些数据注销到辅助文件中。

添加你的序言

\usepackage{etoolbox}

% BEGIN Source:
% https://tex.stackexchange.com/questions/597506/map-the-figure-filenames-onto-the-figure-numbers/597710?noredirect=1#comment1499574_597710

% BEGIN Optional
\usepackage{xstring}

\def\formatfigpathline#1#2{%
    \bgroup
    \def\frontpart{Figure #1:}%
    \StrLen{\frontpart}[\frontpartlength]%
    \ifnum\numexpr\frontpartlength\relax<11\relax
        \edef\frontpart{\frontpart\space\space\space\space}%
        \StrLeft{\frontpart}{11}[\frontpart]%
    \fi
    \xdef\figpathline{\frontpart\space #2}
    \egroup
}
% END

\makeatletter

\newcounter{fignumlength}

\ifx\formatfigpathline\@undefined
\def\formatfigpathline#1#2{%
    \def\figpathline{Figure #1: #2}%
}
\fi


\newwrite\figpathfile
\immediate\openout\figpathfile=\jobname_figures.txt\relax
\immediate\write\figpathfile{Version: \today}%

\def\figpath@stagedfigs{}
\def\figpath@stagefig#1{%
    \expandafter\gdef\expandafter\figpath@stagedfigs\expandafter{%
        \figpath@stagedfigs\do{#1}%
    }%
}

\newbool{FigpathIsEnabled}
\newbool{FigpathEnableOnIncludegraphics}
\setbool{FigpathEnableOnIncludegraphics}{true}

\newcommand\figpathEnable{%
    \unless\ifx\label\@figpathLabel
        \global\let\orig@label\label
        \let\label\@figpathLabel
    \fi
    \unless\ifFigpathIsEnabled
        \global\booltrue{FigpathIsEnabled}%
    \fi
}

\newcommand\figpathDiscard{%
    \gdef\figpath@stagedfigs{}%
}

\newcommand\figpathDisable{%
    \ifx\label\@figpathLabel
        \let\label\orig@label
    \fi
    \ifFigpathIsEnabled
        \figpathDiscard
        \global\boolfalse{FigpathIsEnabled}%
    \fi
}

\newbool{@figpath@isfigure}

\newcommand\figpathFlush{%
    \boolfalse{@figpath@isfigure}
    \def\@cntr@name@figure{figure}%
    \def\@cntr@name@subfigure{subfigure}%
    \ifx\@currentcounter\@cntr@name@figure
        \@figpath@isfiguretrue
    \fi
    \ifx\@currentcounter\@cntr@name@subfigure
        \@figpath@isfiguretrue
    \fi
    \if@figpath@isfigure
        \bgroup\def\do##1{%
            \formatfigpathline{\@currentlabel}{##1}%
            \immediate\write\figpathfile{\figpathline}%
        }%
        \figpath@stagedfigs\gdef\figpath@stagedfigs{}%
        \egroup
    \else
        \PackageWarning{figpath}{Not flushing figure paths: wrong counter (\@currentcounter)!}%
    \fi
}

\AtBeginDocument{
    \newcommand\@figpathIncludegraphics[2][]{%
        \ifFigpathEnableOnIncludegraphics
            \unless\ifFigpathIsEnabled
                \figpathEnable
                \aftergroup\figpathDisable
            \fi
        \fi
        \ifFigpathIsEnabled
            \figpath@stagefig{#2}%
        \fi
        %
        \orig@includegraphics[#1]{#2}%
    }
    \global\let\orig@includegraphics\includegraphics
    \global\let\includegraphics\@figpathIncludegraphics

    \let\orig@label\label
    \let\label\@figpathLabel
}

\def\@figpathLabel{%
    \ifFigpathIsEnabled
        \figpathFlush
    \fi
    \orig@label
}

\makeatother

\AtEndEnvironment{figure}{
    \figpathFlush
}
\AtEndEnvironment{subfigure}{
    \figpathFlush
}

\AtBeginEnvironment{figure}{
    \figpathDiscard
    \figpathEnable
}
\AtEndEnvironment{figure}{
    \figpathDisable
}
% END

并像平常一样使用您的图形和子图形。

这将创建一个包含

Figure 1: example-image-a
Figure 2a: example-image-b
Figure 2b: example-image-c

如果您的文档名为document.tex,则该文件名为document_figures.txt,即_figures.txt附加在其后面。

这就是你想要的吗?

答案2

这是一个可能的方法。请注意,\label应该標題。

\documentclass{article}
\usepackage{graphicx}

\makeatletter
\newcommand{\RFincludegraphics}[2][]{% RF for `remember file'
  \def\@currentimage{#2}%
  \includegraphics[#1]{#2}%
}
\newcommand{\RFlabel}[1]{%
  \label{#1}\let\@currentlabel\@currentimage\label{RF@#1}%
}
\newcommand{\fileref}[1]{%
  \ref{#1}: \texttt{\ref{RF@#1}}%
}
\makeatother

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Some code that provides the same output as the following fragment:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Figure map:
\begin{itemize}
\item Figure \fileref{fig:alef}
\item Figure \fileref{fig:bet}
\item Figure \fileref{fig:gimel}
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{figure}[htp]
\centering
\RFincludegraphics{example-image-a}
\caption[Alef figure.]{Alef.}
\RFlabel{fig:alef}
\end{figure}

\begin{figure}[htp]
\centering
\RFincludegraphics{example-image-b}
\caption[Bet figure.]{Bet.}
\RFlabel{fig:bet}
\end{figure}

\begin{figure}[htp]
\centering
\RFincludegraphics{example-image-c}
\caption[Gimel figure.]{Gimel.}
\RFlabel{fig:gimel}
\end{figure}

\end{document}

在此处输入图片描述

相关内容