![将自动文本添加到图号](https://linux22.com/image/339562/%E5%B0%86%E8%87%AA%E5%8A%A8%E6%96%87%E6%9C%AC%E6%B7%BB%E5%8A%A0%E5%88%B0%E5%9B%BE%E5%8F%B7.png)
我想根据(自动)参考编号向图号添加一些自动文本。
一个最小的例子。
\documentclass{article}
\begin{document}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 1}
\label{fig1}
\end{figure}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 2}
\label{fig2}
\end{figure}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 3}
\label{fig3}
\end{figure}
i want to get "figure (1)'de" when i write " figure (\ref{fig1})",\\
i want to get "figure (2)'se" when i write "figure (\ref{fig2})",\\
i want to get "figure (3)'te" when i write "figure (\ref{fig3})"
\end{document}
答案1
据我理解,图号后应出现后缀de
或se
。我已定义一个宏,可自动添加这些后缀,假设图号大于或等于 3 时仍应保留后缀。作为额外奖励,该宏会自动将单词“图”添加到参考资料中。te
\fref
te
输出如下:
代码如下:
\documentclass{article}
\usepackage{refcount}
\newcommand\fref[1]{%
Figure~(\getrefnumber{#1})`\ifcase\getrefnumber{#1}\or de\or se\else te\fi}
\begin{document}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 1}
\label{fig1}
\end{figure}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 2}
\label{fig2}
\end{figure}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 3}
\label{fig3}
\end{figure}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 4}
\label{fig4}
\end{figure}
Reference for fig1: \fref{fig1}.
Reference for fig2: \fref{fig2}.
Reference for fig3: \fref{fig3}.
Reference for fig4: \fref{fig4}.
\end{document}
需要注意的是,这适用于您的 MWE,但如果图形编号包含章节或节号,例如看起来像 3.4,那么就需要进行修改。
编辑
正如已经强调的那样,如果图号包含节号或章节号,则上述解决方案将不起作用。以下贡萨洛·梅迪纳,以下是处理这种情况的一种方法:
\documentclass{amsart}
\numberwithin{figure}{section}
\usepackage{refcount}
\usepackage{xstring}
\newcommand\fref[1]{\IfRefUndefinedExpandable{#1}{??}{Figure~(\ref{#1})`\addrefending{#1}}}
\newcommand\addrefending[1]{\StrBehind{\getrefnumber{#1}}{.}[\fignumber]%
\ifcase\fignumber\relax\or de\or se\else te\fi}
\begin{document}
\section{Section with figures}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 1}
\label{fig1}
\end{figure}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 2}
\label{fig2}
\end{figure}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 3}
\label{fig3}
\end{figure}
\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{This is figure 4}
\label{fig4}
\end{figure}
Reference for fig1: \fref{fig1}.
Reference for fig2: \fref{fig2}.
Reference for fig3: \fref{fig3}.
Reference for fig4: \fref{fig4}.
\end{document}