以下 MWE 展示了我尝试在文档中创建参考文献列表,其中包含从文本到参考文献的链接和返回的链接。它在文本中效果很好,但在图中效果不佳,因为我尝试让参考文献链接跟在标题后面。
超链接工作正常,但其文本表示错误。
这可能是一个 xy 问题(http://mywiki.wooledge.org/XyProblem)。如果有比我尝试的破解方法更好的解决方案,请随意...
我按照建议玩过字幕 如何为一个图形添加两个标签但没有成功。
\documentclass{book}
\usepackage{answers}
\usepackage{environ}
\usepackage{graphicx}
\usepackage{xifthen}
%\usepackage{subcaption}
\usepackage{hyperref}
\newcommand{\referencesFilename}{references}
\newcounter{csmrefcounter}
\newcounter{gotoref}
\NewEnviron{csmr}[1][]{%
\refstepcounter{csmrefcounter}
\stepcounter{gotoref}
\ifthenelse{\isempty{#1}}
{[R\ref{RR\arabic{gotoref}}]% not inside figure
\label{R\arabic{csmrefcounter}}
}
{\caption{#1 [R\ref{RR\arabic{gotoref}}]}% inside figure
\label{R\arabic{csmrefcounter}}
}%
\Writetofile{\referencesFilename}{\protect\item[R\ref{R\arabic{csmrefcounter}}]}
\Writetofile{\referencesFilename}{\protect\refstepcounter{gotoref}}
\Writetofile{\referencesFilename}{\protect\label{RR\arabic{gotoref}}}
\Writetofile{\referencesFilename}{\BODY }
}
\newcommand{\getreferences}{%
\newpage
\chapter{References}
\setcounter{gotoref}{0}
\begin{itemize}
\input{\referencesFilename}
\end{itemize}
}
\begin{document}
\Opensolutionfile{\referencesFilename}
\chapter{Fermi Problems}
\begin{quotation}
Every day the National Security Agency intercepts and
stores \emph{1.7 billion} international e-mails, phone calls, texts and
other communications.%
\begin{csmr}
ACLU National Newsletter, Summer 2012. Testing behavior if this is a
very long reference, perhaps spanning multiple lines.
\end{csmr}
\end{quotation}
Here's a second reference
\begin{csmr}
Second text reference.
\end{csmr}
inline in the text.
Now test the referencing mechanism for figures.
\begin{figure}
\centering
FIGURE 1 - without a reference
\caption{caption for Figure 1}
\label{fig:1}
\end{figure}
\begin{figure}
\centering
FIGURE 2 - with a reference
\begin{csmr}[caption for Figure 2 - correct reference label here:]
Reference for Figure 2. The link goes to the right place, but displays
incorrectly, showing the figure number rather than the reference number.
\end{csmr}
\label{fig:2}
\end{figure}
\begin{figure}
\centering
FIGURE 3 - without a reference
\caption{caption for Figure 3}
\label{fig:3}
\end{figure}
Try to refer to the figure labels: first one is
\ref{fig:1}, second is \ref{fig:2}, third is
\ref{fig:3}. Note that the link to Figure 2 displays the (wrong!)
reference number rather than the figure number, but goes the the right
place.
\Closesolutionfile{\referencesFilename}
\getreferences
\end{document}
答案1
我希望当地的巫师能尽快给出答案。由于没有人给出答案,所以我在 了解引用和标签的工作原理和 如何使用 \ref 按照计数器最初生成的方式打印其值?,特别是来自@ChristianHupfer 的评论:
您必须使用行号的标签,例如始终在第一列中使用 \label{f1},因为\label 总是关注最后一个计数器随着 \refstepcounter 的增加而增加。如果你在 \footnotemark 列中使用 \label,那么它将使用错误的计数器! – Christian Hupfer 昨天
我相应地重新排列了计数器增量和标签。以下是修订后的环境定义:
\NewEnviron{csmr}[1][]{%
\stepcounter{gotoref}
\ifthenelse{\isempty{#1}}
{[R\ref{RR\arabic{gotoref}}]% not inside figure
\refstepcounter{csmrefcounter}
\label{R\arabic{csmrefcounter}}
}
{% inside figure - the caption passed as the argument has the
% label, e.g. #1 might expand to
% The first figure\label{fig:numberone}
\caption{#1 [R\ref{RR\arabic{gotoref}}]}
\refstepcounter{csmrefcounter}
\label{R\arabic{csmrefcounter}}
}%
\Writetofile{\referencesFilename}{\protect\item[R\ref{R\arabic{csmrefcounter}}]}
\Writetofile{\referencesFilename}{\protect\refstepcounter{gotoref}}
\Writetofile{\referencesFilename}{\protect\label{RR\arabic{gotoref}}}
\Writetofile{\referencesFilename}{\BODY }
}