11

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}
  • You need a counter that can be referenced. – user31729 Mar 4 '17 at 10:55
  • Your \label{tab:Q1} is wrongly placed. It should be used after \caption – user31729 Mar 4 '17 at 11:06
16

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

|improve this answer|||||
  • 1
    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 '17 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 '17 at 9:29
  • All I would add, honestly, is a comment that should the caption be placed above the tabular material, the instruction \renewcommand{\therowcntr}{\@arabic{\numexpr\c@table+1}.\arabic{rowcntr}} may be simplified to \renewcommand{\therowcntr}{\thetable.\arabic{rowcntr}}. – Mico Mar 5 '17 at 9:52
  • 1
    @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 '17 at 10:23
8

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}.

enter image description here

\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} & ...
|improve this answer|||||
6

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:

mwe

\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:

mwe2

\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}
|improve this answer|||||
5

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.