2

The following MWE shows my attempt to create a list of references in a document, with links from text to reference and back. It works well in text, but badly in figures when I try make the reference link follow the caption.

Hyperlinks work correctly but their text representations are wrong.

This may be an xy-problem (http://mywiki.wooledge.org/XyProblem). If there's a better solution than my attempted hack, feel free...

I've played with subcaptions as suggested at How to put two labels for one figure with no success.

enter image description here

enter image description here

enter image description here

\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}
2
  • Why don't you use LaTeX's built-in cross-reference and footnote systems and a bibliographic package like biblatex? Jul 20, 2015 at 22:10
  • @AndrewCashner I have about 200-300 references with varying formats, often free text. To maintain my document I have to keep the reference text where the reference is in the document rather than in another file. "Footnotes" might work if there were a robust backreferencing mechanism (that worked for figures and tables too). I'm more than willing to be educated. Jul 20, 2015 at 23:14

1 Answer 1

1

I'd hoped a local wizard would provide an answer quickly. Since no one did, I worked it out for myself in time, with help from Understanding how references and labels work and How to use \ref to print the value of a counter the way it was originally produced?, particularly this comment from @ChristianHupfer :

You must use the label for the linenumber, say \label{f1} in the first column always, since \label does always look after the last counter being increased with \refstepcounter. If you use \label in the \footnotemark column, it will use the wrong counter then! – Christian Hupfer yesterday

I rearranged the counter increments and the labels accordingly. Here's the revised environment definition:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .