Reference table row in LaTeX
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.
Hyperlinks do work!
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}
\usepackage[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\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}
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[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\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}
\usepackage[colorlinks]{hyperref}
\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{whyisit}{43} is about ... \\
Question \hyperlink{doyouthink}{here (36)} is about ... \\
Question \hyperlink{isitcorrect}{13} 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}
\usepackage{spreadtab,booktabs}
\begin{document}
\begin{spreadtab}{{tabular}{ccll}}
@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
\addlinespace[.5ex]
\multicolumn{4}{l}{Question :={a4} in this table is about ... }\\
\end{spreadtab}
\bigskip\bigskip\bigskip
Question \STtag{whyisit} in that table is about ...
\end{document}