# Reference table row in LaTeX

I would like to number and label each row in my table so I can reference them later on.

But I get the flowing output:

I would like the output to look like this:

How can I fix this problem?

Code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[danish]{babel}
\usepackage{lscape}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{float}
\usepackage[top=2.81cm, bottom=2.75cm,right=2cm, left=2cm]{geometry}
\usepackage{url}
\usepackage{setspace}
\onehalfspacing
\usepackage[T1]{fontenc}

\begin{document}

\begin{table}[H]
\centering
\label{tab:Q1}
\begin{tabular}{|c|c|l|l|}
\hline
Number & Theory & Question & Explanation  \\ \hline
1 \label{que:whyisit} & B & Why is it you think ... & This is a good question\\
2 \label{que:doyouthink} & A & Do you think  ... & This Is also a good question\\
3 \label{que:isitcorrect} & B & is it correct that you think ... & This is question \\ \hline
\end{tabular}
\caption{Table with questions}
\end{table}
Question \ref{que:whyisit} is about ... \\
Question \ref{que:doyouthink} is about ... \\
Question \ref{que:isitcorrect} is about ...

\end{document}

• You need a counter that can be referenced.
– user31729
Mar 4, 2017 at 10:55
• Your \label{tab:Q1} is wrongly placed. It should be used after \caption
– user31729
Mar 4, 2017 at 11:06

Here is a method with a new columntype N which automatically increases the rowcntr, since a \label should use a counter in the background (although writing manipulating \@currentlabel is also possible, see gernot's answer).

The rowcntr is reset with every new tabular command or if a new table \caption has been used (i.e. the table counter has been increased then)

In my point of view, the row number should be prefixed with the table number, in order to confusion which question is meant in case of more than one table questions should occur.

I also shifted the wrongly placed table label to the place where it belongs, i.e. after using\caption!

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[danish]{babel}
\usepackage{lscape}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{float}
\usepackage{array}
\usepackage[top=2.81cm, bottom=2.75cm,right=2cm, left=2cm]{geometry}

\usepackage{etoolbox}
\usepackage{setspace}
\onehalfspacing

\usepackage{url}

\newcounter{rowcntr}[table]
\renewcommand{\therowcntr}{\thetable.\arabic{rowcntr}}

% A new columntype to apply automatic stepping
\newcolumntype{N}{>{\refstepcounter{rowcntr}\therowcntr}c}

% Reset the rowcntr counter at each new tabular
\AtBeginEnvironment{tabular}{\setcounter{rowcntr}{0}}

\begin{document}

\begin{table}[H]
\centering
\caption{Table with questions} \label{tab:Q1}
\begin{tabular}{|N|c|l|l|}
\hline
\multicolumn{1}{|c}{Number} & Theory & Question & Explanation  \\ \hline
\label{que:whyisit} & B & Why is it you think ... & This is a good question\\
\label{que:doyouthink} & A & Do you think  ... & This Is also a good question\\
\label{que:isitcorrect} & B & is it correct that you think ... & This is question \\ \hline
\end{tabular}
\end{table}

Question \ref{que:whyisit} is about ... \\
Question \ref{que:doyouthink} is about ... \\
Question \ref{que:isitcorrect} is about ...

\clearpage

\begin{table}[H]
\centering
\caption{Table with questions} \label{tab:Q2}
\begin{tabular}{|N|c|l|l|}
\hline
\multicolumn{1}{|c}{Number} & Theory & Question & Explanation  \\ \hline
\label{que:whyisitother} & B & Why is it you think ... & This is a good question\\
\label{que:doyouthinkother} & A & Do you think  ... & This Is also a good question\\
\label{que:isitcorrectother} & B & is it correct that you think ... & This is question \\ \hline
\end{tabular}

\end{table}

Question \ref{que:whyisitother} is about ... \\
Question \ref{que:doyouthinkother} is about ... \\
Question \ref{que:isitcorrectother} is about ...

\end{document}


• I've already upvoted this answer. :-) The only addition I'd make is to point out that the numbering method for the questions assumes that the caption is placed below, i.e., after, the tabular material. This, of course, conforms to the OP's chosen layout. Nevertheless, for future readers of this answers who may be looking to adapt it to their needs, it's probably worth stating this layout-related assumption explicitly.
– Mico
Mar 5, 2017 at 9:06
• @Mico: Thanks for the suggestion, I will provide a solution for the case the caption is placed before the tabular content, i.e. I don't have to 'increase' the table in a fake way
– user31729
Mar 5, 2017 at 9:29
– Mico
Mar 5, 2017 at 9:52
• @Mico: I'll do later on ... In the meantime I am working on some improvements of xassoccnt to incorporate some of the label/zlabel stuff I 'invented' for various answers in the last weeks.
– user31729
Mar 5, 2017 at 10:23
• This does not seem to work with forward references. Eg moving the \ref to before the table leads to undefined references. Aug 9 at 15:48

With the definition

\makeatletter
\newcommand\newtag[2]{#1\def\@currentlabel{#1}\label{#2}}
\makeatother


in the preamble you can use the command

\newtag{word}{label name}


to insert word at this point and refer to it before or after this place using \ref{label name} or \pageref{label name}.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[danish]{babel}
\usepackage{lscape}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{float}
\usepackage[top=2.81cm, bottom=2.75cm,right=2cm, left=2cm]{geometry}
\usepackage{url}
\usepackage{setspace}
\onehalfspacing
\usepackage[T1]{fontenc}

\makeatletter
\newcommand\newtag[2]{#1\def\@currentlabel{#1}\label{#2}}
\makeatother

\begin{document}

\begin{table}[H]
\centering
\label{tab:Q1}
\begin{tabular}{|c|c|l|l|}
\hline
Number & Theory & Question & Explanation  \\ \hline
\newtag{1}{que:whyisit} & B & Why is it you think ... & This is a good question\\
\newtag{2}{que:doyouthink} & A & Do you think  ... & This Is also a good question\\
\newtag{3}{que:isitcorrect} & B & is it correct that you think ... & This is question \\ \hline
\end{tabular}
\caption{Table with questions}
\end{table}
Question \ref{que:whyisit} is about ... \\
Question \ref{que:doyouthink} is about ... \\
Question \ref{que:isitcorrect} is about ...

\end{document}


If you prefer, you can remove the \label-functionality from \newtag and just define

\makeatletter
\newcommand\newref[1]{#1\def\@currentlabel{#1}}
\makeatother


and use it as

\newref{1}\label{que:whyisit} & ...
\newref{2}\label{que:doyouthink} & ...


Make a counter it is also what I would do, but with 2 answers (+1 both) around this solution, seem enough.

As proof of concept, this is a different answer in case that the counter is not a solution, because the "Number" cannot be calculated and typeset automatically, or even is not a number. Think in some score, a identification number (not necessarily ordered and consecutive), value of the question in an exam, etc., or some hexadecimal code, etc.

As you are using hyperref, you can use here \hypertarget and \hyperlink. The disadvantage is that the text of the cross-reference is not automatic, but is still an active link and the advantage is that cell content and reference text are both free, so you can use identification keys as F24E, reference the number spelled out ("nine" instead of "9"), etc. Example:

\documentclass{article}
\usepackage{booktabs}
\parskip2em\parindent0pt
\begin{document}

\begin{tabular}{ccll}\toprule
Number                        & Theory & Question                & Explanation  \\\midrule
\hypertarget{whyisit}{43}     & B      & Why is it you think ... & This is a good question\\
\hypertarget{doyouthink}{36}  & A      & Do you think  ...       & This is also a good question\\
\hypertarget{isitcorrect}{13} & B      & is it correct that  ... & This is question \\\bottomrule
\end{tabular}

Question  \hyperlink{doyouthink}{here (36)} is about ... \\

\end{document}


A more extravagant alternative for arbitrary numbers (but only numbers) could be the spreadtab package. The number of the cross-reference is automatic, but is not an active link. However could be useful if you need some math with these numbers (sum of the total points, etc.). The example show a cross-reference inside and outside the table. The example bellow show that values can be recalled by the cell position inside the table, or cross-referenced with a tag in the main text:

\documentclass{article}
\begin{document}

@Number        & @Theory & @Question                & @Explanation\\\midrule
43tag(whyisit) & @B      & @Why is it you think ... & @This is a good question\\
36             & @A      & @Do you think ...        & @This is also a good question\\
13             & @B      & @is it correct that ...  & @This is question \\\bottomrule
\multicolumn{4}{l}{Question :={a4} in this table is about ... }\\

\bigskip\bigskip\bigskip

Question \STtag{whyisit} in that table is about ...

\end{document}


The \label{} command just stores the last updated counter value for basically any counter created using \newcounter{} and stepped up using refstepcounter{}. Usually, these commands are hidden somewhere inside LaTeX commands, like \section{}, \caption{} etc. In your case you'd want to create your own counter and increase its value at every table row. Something like the following would work:

\documentclass{article}

\newcounter{question}
\newcommand{\nextquestion}{\refstepcounter{question}\arabic{question}}

\begin{document}

\begin{table}[h]
\centering
\begin{tabular}{|c|c|l|l|}
\hline
Number & Theory & Question & Explanation  \\ \hline
\nextquestion\label{que:whyisit} & B & Why is it you think ... & This is a good question\\
\nextquestion\label{que:doyouthink} & A & Do you think  ... & This Is also a good question\\
\nextquestion\label{que:isitcorrect} & B & is it correct that you think ... & This is question \\ \hline
\end{tabular}
\caption{Table with questions}
\label{tab:Q1}
\end{table}
Question \ref{que:whyisit} is about ... \\
Question \ref{que:doyouthink} is about ... \\
Question \ref{que:isitcorrect} is about ...
\end{document}


Also, if you want to reference the table itself, you should put the corresponding label after the table caption.

• What if I want to use multirow and I want all rows in it to be referenced by the same number? Jul 16, 2020 at 15:43
• @Suzie, \nextquestion should work fine inside a \multirow argument. Jul 16, 2020 at 17:35

An approach almost identical to that mentioned in this answer also works with LyX. The only difference is that you can simplify the LaTeX preamble to

\newcommand\newtag[2]{#1\def\@currentlabel{#1}\label{#2}}