# In-text reference to a number in a Table

I was wondering if it is possible to make an in-text cross-reference to a specific number in a Table, such that the in-text number changes whenever the number, which I refer to in the text, changes in the Table? In my text, I repeatedly have to refer to a specific sum in a table. The problem is that, because the elements used to derive the sum are continuously altered, I have to change the in-text number manually several times. I am afraid that I might miss correcting some of the in-text numbers. Providing an example, assume that Table 1 takes the following form

$$\begin{array}{|c|c|} & \text{Sum} \\ \hline Bla bla & 5 \\ \hline \end{array}$$


In Table 1, the sum equals 5.

As I repeatedly make a similar reference to the sum, I was hoping for a solution something like:

"Table 1 shows that the sum equals \ref{sum in table 1}"

, where I have 'labeled' the number of 5 in Table 1 "sum in table 1". (I wanted to produce an example, but I don't think it is possible) finally, I am hoping for a more 'general solution', as I have several Tables 'of this kind' in my actual thesis.

• Will you have multiple of thouse tables or just one? Nov 4 '20 at 10:04
• I would say multiple. I was hoping for a 'general procedure', because I generally have a lot of tables. I am sorry for not making this clear in my initial inquiry Nov 4 '20 at 10:08
• @Bubaya That's not really a general approach. The OP says that there might be multiple tables with—I guess—multiple different values.
– Sam
Nov 4 '20 at 11:00
• @Sam Why isn't that a general approach? Is there any difference between a hypothetical \GiveNameToNumber{interesting table:sum of first column}{42} and \newcommand in practice (except for the characters in the identifier)? Nov 4 '20 at 11:16

In the text of your question you use the environment array to create a table. But the environment array is used to create matrices in math mode. I recommend not to misuse the environment array for creating tables but to use the tabular environment for creating tables.

By means of the package zref you can easily introduce your own cross-referencing system analogously to the \label-\ref-mechanism implemented in the LaTeX 2ε-kernel:

The following example defines

\StoreNumber{⟨ID/label⟩}{⟨something that in the text should denote a number⟩}

and

\RetrieveNumber{⟨ID/label⟩}

Edit in November 5, 2020:

I (Ulrich Diez) overlooked that "number" doesn't imply "integer".
I removed the \number\numexpr...\relax-thingie from \StoreNumber.

With ⟨something that in the text should denote a number⟩ you need to take care of expansion-prevention and of creation of pdf-strings for the .pdf-file's bookmarks.

With the zref-things I did not use zref's wrapper for enabling babel-shorthands.
If I did and \RetrieveNumber ended up as part of the argument of a sectioning-command which also might end up in pdf-bookmarks, then zref's wrapper for babel-shorthands would cause problems while creating the bookmark for the section in question takes place.

\documentclass{article}

\usepackage[unicode=true]{hyperref}
\usepackage{zref}
\makeatletter
\@ifdefinable\RetrieveNumber@RefUndefText{%
\DeclareRobustCommand\RetrieveNumber@RefUndefText{%
\texorpdfstring{\nfss@text{\reset@font\bfseries ??}}{??}%
}%
}%
\@ifdefinable\RetrieveNumber{%
\DeclareRobustCommand\RetrieveNumber[1]{%
\texorpdfstring{\zref@refused{#1}}{}%
\zref@extractdefault{#1}{numbertostore}{\RetrieveNumber@RefUndefText}%
}%
}%
}{%
\@ifdefinable\RetrieveNumber@RefUndefText{%
\DeclareRobustCommand\RetrieveNumber@RefUndefText{%
\nfss@text{\reset@font\bfseries ??}%
}%
}%
\@ifdefinable\RetrieveNumber{%
\DeclareRobustCommand\RetrieveNumber[1]{%
\zref@refused{#1}%
\zref@extractdefault{#1}{numbertostore}{\RetrieveNumber@RefUndefText}%
}%
}%
}%
%
\zref@newprop{numbertostore}[0]{0}%
\@ifdefinable\StoreNumber{%
\DeclareRobustCommand\StoreNumber[2]{%
\begingroup
\zref@setcurrent{numbertostore}{#2}%
\zref@labelbyprops{#1}{numbertostore}%
\endgroup
\RetrieveNumber{#1}%
}%
}%
\@ifdefinable\CheckWhetherMath{%
\DeclareRobustCommand\CheckWhetherMath{%
\relax\ifmmode\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}%
}%
\makeatother

\usepackage{amsmath}

\usepackage[cbgreek]{textgreek} % cbgreek/euler/artemisia

\begin{document}

\section{About a sum which equals \protect\NoCaseChange{\RetrieveNumber{sum in table 1}}}
\label{about a sum in table 1}

This section has the title: \nameref*{about a sum in table 1}.

\bigskip

\noindent\autoref*{table1} shows that the sum equals \RetrieveNumber{sum in table 1}.

% pi-symbol is  0x03C0 in utf-16.
% higher byte is 03 (Hex) = 3(dec) = 003(oct),
% lower byte is C0 (Hex) = 192(dec) =  300(oct),
% Higher byte must be marked by \9, thus the unicode-octal-triplet-pdfstring is: \9003\300

\begin{table}[h]
\centering
\begin{tabular}{|c|c|}
\hline
&\text{Sum}\\ \hline
Bla bla&\StoreNumber{sum in table 1}{\string\texorpdfstring{\CheckWhetherMath{\pi}{\textpi}}{\string\9003\string\300}}\\\hline
\end{tabular}%
\caption{A table where the sum equals \RetrieveNumber{sum in table 1}}%
\label{table1}%
\end{table}

\noindent\autoref*{table1} shows that the sum equals \RetrieveNumber{sum in table 1}.

$$\RetrieveNumber{sum in table 1}+\RetrieveNumber{sum in table 1}=2\cdot\RetrieveNumber{sum in table 1}$$

\bigskip

\noindent\autoref*{table1} has the title: \nameref*{table1}

\bigskip

\hrule

\bigskip

\section{About a sum which equals \protect\NoCaseChange{\RetrieveNumber{sum in table 2}}}
\label{about a sum in table 2}

This section has the title: \nameref*{about a sum in table 2}.

\bigskip

\noindent\autoref*{table2} shows that the sum equals \RetrieveNumber{sum in table 2}.

\begin{table}[h]
\centering
\begin{tabular}{|c|c|}
\hline
&\text{Sum}\\ \hline
Bla bla&\StoreNumber{sum in table 2}{\number\numexpr(1*3)+1+1\relax}\\\hline
\end{tabular}%
\caption{A table where the sum equals \RetrieveNumber{sum in table 2}}%
\label{table2}%
\end{table}

\noindent\autoref*{table2} shows that the sum equals \RetrieveNumber{sum in table 2}.

\bigskip

\noindent\autoref*{table2} has the title: \nameref*{table2}

$$\RetrieveNumber{sum in table 2}+\RetrieveNumber{sum in table 2}=2\cdot\RetrieveNumber{sum in table 2}$$

\bigskip

\hrule

\bigskip

\tableofcontents

\listoftables

\end{document}


As storing and retrieving numbers is implemented similar to the \label-\ref-mechanism of the LaTeX 2ε-kernel you need to compile the document at least twice until everything matches out and ?? are replaced by the correct number everywhere in the document. In case the package hyperref is in use while the package bookmark is not loaded you need to compile the document at least three times.

In my example I loaded the textcase-package. It provides means for preventing the uppercasing/lowercasing of characters.

With documentclasses like article which with \pagestyle{headings} automatically create page-headers with sectioning titles in uppercase-letters this is needed to prevent the uppercasing of characters that denote the names of referencing-labels or numbers to retrieve. Otherwise a \Retrievenumber{sum in table 1} would be turned into \Retrievenumber{SUM IN TABLE 1} within page-headers, which would be a problem as (unlike the number "sum in table 1") a number "SUM IN TABLE 1" was never stored.

Bookmarks look like this: