4

I'm using autoref to refer to tables in the text, e.g.

In \autoref{tab:main_result}, you can see ...

I'd like to be able to refer to a variable that lists the pages that tab:main_result is called on. The ultimate goal is to be able to put that reference in the table note (e.g. "For discussion of this table, see page XX").

Is this possible? I know that I could refer to the section in the table notes, but I'd like to be able to refer to the page.

  • How about \pageref{tab:main_result}? – marmot Apr 27 at 3:38
  • That references the page the table is on, not the page the table is referred to on. – texxx Apr 27 at 3:40
  • AFAICT, LaTeX doesn't provide any built-in facilities for keep tracking of where some item (e.g., a table) is being cross-referenced elsewhere in the document. A preprocessor approach might offer a viable solution. pdfLaTeX and XeLaTeX don't provide a built-in preprocessor facility, but LuaLaTeX does. Are you able to switch to and use LuaLaTeX? – Mico Apr 27 at 9:42
7

Something like this?

enter image description here

\documentclass{article}
\usepackage{varioref}
\usepackage[colorlinks]{hyperref}
\usepackage[noabbrev]{cleveref}  
\begin{document}  
\begin{table} \caption{A}\label{tab:main} \end{table}
\clearpage \null \clearpage

In \vref{tab:main}, you can see \dots

For a discussion of \cref{tab:main}, see \cpageref{tab:main}.
\end{document}
3

When I first read this I thought that the question was asking for a command that gave the list of pages that contain a reference to a given table. That is, an analogue of backref for \autoref instead of for \cite. Rereading the question it probably does not ask for this but, nonetheless, I thought this an interesting question and the code below is one way of providing this functionality.

The code works by redefining the \autoref command so that whenever it is called it first adds the current page number to a corresponding latex3 sequence. At the end of the document this sequence is saved to the aux file. Once the document has been compiled at least once the command \pagerefs{...} can be used to print the list of pages that the reference appears on. So, for example, the MWE below produces:

enter image description here

Here is the code:

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn
\NewDocumentCommand\PageReferences{mm}{
  \seq_if_exist:cF {g__pages_#1_seq}{
    \seq_new:c {g__pages_#1_seq}
    \seq_gset_from_clist:cn {g__pages_#1_seq} {#2}
  }
}
\RenewDocumentCommand\autoref{sm}{
  \seq_if_exist:cF {g_pages_#2_seq}{
    \seq_new:c {g_pages_#2_seq}
    \AtEndDocument{
      \iow_now:cx { @auxout } {
        \token_to_str:N \PageReferences{#2} { \seq_use:cn {g_pages_#2_seq}{,}}
      }
    }
  }
  \seq_if_in:cxF {g_pages_#2_seq} {\thepage} {
        \seq_gput_right:cx {g_pages_#2_seq} {\thepage}
  }

  % now call the real autoref, which is really \HyPsd@autoref
  \IfBooleanTF{#1}{\csuse{HyPsd@autoref}*{#2}}{\csuse{HyPsd@autoref}{#2}}
}
\NewDocumentCommand\pagerefs{m}{
  \seq_if_exist:cTF {g__pages_#1_seq}{
   \seq_use:cnnn {g__pages_#1_seq} { ~and~ } { ,~ } { ,~and~ }
  }{??}
}
\ExplSyntaxOff

\begin{document}

\begin{equation}\label{E:1} 1+1=2 \end{equation}
  Look at \autoref{E:1}! It occurs on pages \pagerefs{E:1}.

  \lipsum

  Look at \autoref{E:1}

  \lipsum

  Look at \autoref{E:1}

  \lipsum

  Look at \autoref{E:1}

  \lipsum

  Look at \autoref{E:1}

  \lipsum

  Look at \autoref{E:1}

  \lipsum

\end{document}
  • Your answer is fine and gives what is seams that OP looking for, however, i can't imagine why someone will repeat the same equation with the same label many times in a document. This, to my opinion, only confuse readers :-(. – Zarko Apr 27 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.