Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there a way to add the usage of a table/reference in the caption of the reference/table in LaTeX?

For example:

\begin{table}
    \begin{tabular}{ll}
    \textbf{Name} & \textbf{Description} \\
        Foo & bar \\
        Foo & bar
    \end{tabular}
    \caption{Nice Table. Used on pages [2,3,4].}
    \label{tab:table}
\end{table}

I would like the "Used on pages[2,3,4]." to appear automatically and linked to the correct reference, to all the locations in the document that use \ref{tab:table}.

Anyone knows a package that takes care of this?

share|improve this question
Welcome to TeX.sx! Your question was migrated here from another stackexchange site. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. – doncherry Sep 4 '11 at 10:31

migrated from stackoverflow.com Apr 5 '11 at 8:18

3 Answers

EDIT: Working with hyperref

EDIT: 2 Few minutes ago I mentioned that the package crossreference also create the command \xref.

I saw this question during the session Answer the Unanswered

Now I want to present a solution:

\documentclass{article}
\usepackage{etoolbox,xstring,xspace}
\makeatletter
\let\origref\ref
\renewcommand*\ref[1]{%
  \origref{#1}\xlabel{#1}}
\def\xlabel#1{%
   \ifcsdef{siteref@doc@#1}{}{\csgdef{siteref@doc@#1}{,}}%
    \@bsphack%
       \csxdef{siteref@doc@#1}{\csuse{siteref@doc@#1},\thepage}%
         \protected@write\@auxout{}%
        {\string\SiteRef{siteref@#1}{\csuse{siteref@doc@#1}}}%
     \@esphack%
}

\newrobustcmd*\SiteRef[2]{\csgdef{#1}{#2}}

\newrobustcmd*\xref[1]{%
\ifcsundef{siteref@#1}{%
     \@latex@warning@no@line{Label `#1' not defined}
     }{%
       [\StrGobbleLeft{\csuse{siteref@#1}}{2}]\xspace%
   }%
}
\makeatother
\usepackage{lipsum}
\begin{document}
\ref{tab:table}
\lipsum[1]
\begin{table}[!ht]
    \begin{tabular}{ll}
    \textbf{Name} & \textbf{Description} \\
        Foo & bar \\
        Foo & bar
    \end{tabular}
    \caption{Nice Table. Used on pages \xref{tab:table}.}
    \label{tab:table}
\end{table}

\lipsum
\ref{tab:table}
\end{document}

EDIT:

Now it works with hyperref

\documentclass{article}
\usepackage{etoolbox,xstring,xspace}
\usepackage[colorlinks=true]{hyperref}
\makeatletter

\AtBeginDocument{%
\let\origref\ref
\renewcommand*\ref[1]{%
  \origref{#1}\xlabel{#1}}
}
\newrobustcmd*\xlabel[1]{%
   \ifcsdef{siteref@doc@#1}{}{\csgdef{siteref@doc@#1}{,}}%
    \@bsphack%
    \begingroup
       \csxdef{siteref@doc@#1}{\csuse{siteref@doc@#1},\thepage}%
         \protected@write\@auxout{}%
        {\string\SiteRef{siteref@#1}{\csuse{siteref@doc@#1}}}%
     \endgroup
     \@esphack%
}

\newrobustcmd*\SiteRef[2]{\csgdef{#1}{#2}}

\newrobustcmd*\xref[1]{%
\ifcsundef{siteref@#1}{%
     \@latex@warning@no@line{Label `#1' not defined}
     }{%
    \begingroup
      \StrGobbleLeft{\csuse{siteref@#1}}{2}[\@tempa]\relax%
      \def\@tempb{}%
      \@tempcnta=0\relax%
      \@tempcntb=\@ne\relax%
      \def\do##1{\advance\@tempcnta\@ne}%
      \expandafter\docsvlist\expandafter{\@tempa}%
       \def\do##1{%
         \ifnum\@tempcntb=\@tempcnta\relax%
            \hyperpage{##1}%
         \else
            \hyperpage{##1},%
          \fi%
          \advance\@tempcntb\@ne
       }%
       [\expandafter\docsvlist\expandafter{\@tempa}]\xspace%
    \endgroup
   }%
}
\makeatother

\usepackage{lipsum}
\begin{document}
\ref{tab:table}
\lipsum[1]
\begin{table}[!ht]
    \begin{tabular}{ll}
    \textbf{Name} & \textbf{Description} \\
        Foo & bar \\
        Foo & bar
    \end{tabular}
    \caption{Nice Table. Used on pages \xref{tab:table}.}
    \label{tab:table}
\end{table}

\lipsum
\ref{tab:table}
\end{document}
share|improve this answer
1  
Awesome answer! However, when I tried it, if I reference something on the page more than once, the same page number will be written out more than once. Is it somehow possible to only print the page number only once? – jaye1234 Jan 9 at 10:44

You can get page numbers by putting labels where the table is used and adding \pageref{..}, but \pageref is "fragile", so must be preceded with a \protect, e.g.,

Table \ref{tab:table}\label{tabuse:table} used here...

then in the caption:

\caption{Nice Table. Used on page \protect\pageref{tabuse:table}.}

A dirty trick for articles is to enter an empty ToC line:

\caption[]{Nice Table. Used on page \pageref{tabuse:table}.}
share|improve this answer
Welcome to TeX.sx! A tip: you can use backticks ` to mark your inline code as I did in my edit. – doncherry Sep 4 '11 at 10:30

Have a look at the backrefx package.

share|improve this answer
3  
i looked at backref but isn't it only for bibliographic entries? i don't see how it could be used for just any \label – Anonymous Aug 2 '10 at 12:52
phimuemue said backrefx, which is on the ctan obsolete tree, not (afaik) in any distribution. the reason it was sent to obsolete seems to be that backref now has the same functionality. – wasteofspace Jun 4 '11 at 11:28
1  
I have pulled backrefx years ago (2004) after the functionality was included in the hyperef/backref packages. In principle you can program the same type of thing, but I do not see a wide use for it. – Danie Els Jul 4 '11 at 10:34
@Danie: would you make an answer from your comment? – egreg Sep 3 '11 at 21:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.