# Can I refer to cells in a tabular?

I have a tabular table, and in my text I want to refer to cells in the table. One way is to superscript the cell content:

\begin{table}
\begin{center}
\begin{tabular}{lrr}\hline
This & $99^a$ & $1.23$\\
That & $22$ & $3.45$\\
The Other & $69^b$ & $9.99$\\
\hline
\end{tabular}
\end{center}
\end{table}

I enjoyed This' (a) and The Other (b).


but then if I rearrange the rows of the table the letters are out of alphabetical order. \label just uses the table number as reference so that doesn't work.

Maybe there's a better stylistic device to do this...

-

You can modify \@currentlabel to be whatever you want and then use \label-\ref as usual. \@currentlabel is used as the reference component when using \ref. The command \speciallabel{<stuff>}{<label>} in the MWE below modifies \@currentlabel to be <stuff> and sets the label to <label>:

\documentclass{article}
\makeatletter
\newcommand{\speciallabel}[2]{% \speciallabel{<stuff>}{<label>}
\edef\@currentlabel{#1}\label{#2}%
}
\makeatother
\begin{document}
\begin{table}
\centering
\begin{tabular}{lrr}\hline
This & $99^a$\speciallabel{a}{this} & $1.23$\\
That & $22$ & $3.45$\\
The Other & $69^b$\speciallabel{b}{theother} & $9.99$\\
\hline
\end{tabular}
\end{table}

I enjoyed This' (\ref{this}) and The Other (\ref{theother}).
\end{document}


There was no request for hyperref accommodation.

For an automated counter-style reference, consider the following version of \speciallabel{<label>}:

\documentclass{article}
\newcounter{spcounter}\renewcommand{\thespcounter}{\alph{spcounter}}
\newcommand{\speciallabel}[1]{% \speciallabel{<label>}
\refstepcounter{spcounter}\textsuperscript{\thespcounter}\label{#1}%
}
\begin{document}
\begin{table}
\centering
\begin{tabular}{lrr}\hline
This & $99\speciallabel{this}$ & $1.23$\\
That & $22$ & $3.45$\\
The Other & $69\speciallabel{theother}$ & $9.99$\\
\hline
\end{tabular}
\end{table}

I enjoyed This' (\ref{this}) and The Other (\ref{theother}).
\end{document}


Note that this will have the counter increment throughout the document. However, it can also be made to reset at the start of the table (or tabular) environment, if needed.

-
Does exactly the job. I've also realised I might want to refer to several cells with the same label at some point, saying something like "three cells (labelled \ref{large}) were significantly large and two (\ref{small}) very small". But that's probably another question... –  Spacedman Nov 9 '12 at 14:01
@Spacedman: Then an automated solution might interfere with this. However, my first solution provides a manual way of selecting the reference. If need be, you can combine the two by using a reference as the argument to \speciallabel{\ref{..}}{<label>}. Perhaps a renewed syntax that allows a mix of the two is in order. I'll think of something... –  Werner Nov 9 '12 at 14:34

\documentclass{article}

\usepackage{array}

\newcounter{mycount}
\renewcommand\themycount{%
\arabic{mycount}%
\ifcase\value{mycount}%
th\or st\or nd\or rd\else th\fi}

\begin{document}

\begin{table}
\begin{tabular}{>{\refstepcounter{mycount}\themycount}lc}
& a \\
\label{here} & b \\
& c \\
& d
\end{tabular}
\caption{}\label{}
\end{table}

blah blah see the \ref{here} row.
\end{document}

-
That references rows, I'd really like to do cells, and I would prefer to have a mark in the cell (alpha or numeric) so the reader can eyeball it quickly. I need to pick out two or three cells in a table of about twenty numbers. –  Spacedman Nov 8 '12 at 17:57
It doesn't really reference rows (there is no TeX group that corresponds to a row) it just references the first cell, if you put >{\refstepcounter{mycount}} into every column the counter would increment and be referenceable in all cells. You can have any mark you want I just used \themycount above but you can have nothing or put it in a \textsuperscript` or whatever. –  David Carlisle Nov 8 '12 at 18:00
or do you mean you only want the counter to increment for the cells you reference (that's also possible but I'm offline for an hour or two from now) ping a comment here I'll pick it up later –  David Carlisle Nov 8 '12 at 18:03