[Tex/LaTex] Reference table row in LaTeX

cross-referencingnumberingtables

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:

Output from code

I would like the output to look like this:

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[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\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}

Best Answer

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}

enter image description here

Related Question