# How can I add label and reference to each row of a table?

I have 2 tables, and they have a common column so I want each row from the first table to point to the corresponding row of the second and the other way around. And also, if it is possible that the label be the value inside the cell.

Here is part of the first table:

\begin{table}[h]
\centering
\caption{Test files}
\label{my-label}
\resizebox{\textwidth}{!}{%
\begin{tabular}{|l|l|l|l|}
\hline
File name & Number of depots & Number of vehicles & Number of customers \\ \hline
0.txt        \label{0txt1}   \ref{0txt2}      & 2                & 4                   & 18                  \\ \hline
1.txt        \label{1txt1}   \ref{1txt2}      & 4                & 16                 & 50                  \\ \hline
2.txt        \label{2txt1}   \ref{2txt2}      & 4                & 8                   & 50
\end{tabular}
}
\end{table}

Here is part of the second table:

\begin{table}[h]
\centering
\resizebox{\textwidth}{!}{%
\begin{tabular}{l|l|l|l|l|l|l|l|l|l|l|l|l|}
\cline{2-13}
& \multicolumn{12}{c|}{\textit{\textbf{COST}}}                                                                                                                                                                                                                                                                                                                                                                                                              \\ \cline{2-13}
& \multicolumn{3}{c|}{\textbf{NO OPTION}}                                                                     & \multicolumn{3}{c|}{\textbf{REINITIALIZATION}}                                                              & \multicolumn{3}{l|}{\textbf{PERMUTATION}}                                                                   & \multicolumn{3}{l|}{\textbf{PERTURBANCE}}                                                                   \\ \hline
\multicolumn{1}{|c|}{\textit{File name}} & \multicolumn{1}{c|}{\textit{None}} & \multicolumn{1}{c|}{\textit{2Opt}} & \multicolumn{1}{c|}{\textit{All}} & \multicolumn{1}{c|}{\textit{None}} & \multicolumn{1}{c|}{\textit{2Opt}} & \multicolumn{1}{c|}{\textit{All}} & \multicolumn{1}{c|}{\textit{None}} & \multicolumn{1}{c|}{\textit{2Opt}} & \multicolumn{1}{c|}{\textit{All}} & \multicolumn{1}{c|}{\textit{None}} & \multicolumn{1}{c|}{\textit{2Opt}} & \multicolumn{1}{c|}{\textit{All}} \\ \hline
\multicolumn{1}{|l|}{0.txt        \label{0txt2}   \ref{0txt1}}              &                                    &                                    &                                   &                                    &                                    &                                   &                                    &                                    &                                   &                                    &                                    &                                   \\ \hline
\multicolumn{1}{|l|}{1.txt        \label{1txt2}   \ref{1txt1}}              &                                    &                                    &                                   &                                    &                                    &                                   &                                    &                                    &                                   &                                    &                                    &                                   \\ \hline
\end{tabular}
}
\end{table}

This is how the labels and references look for the first and second table:

\label{0txt1} \ref{0txt2}

\label{0txt2} \ref{0txt1}.

At the moment I can only go from the second table to the first not the other way around.

• What do you mean by "point to"? Do you want a hyperlink in the table that you can click and it will jump from row X in table 1 to row X in table 2? If so, what should this clickable link say? Should it be the 0.txt that is clickable, which from table 1 will jump to 0.txt in table 2? – Werner Jun 21 '14 at 16:41
• Please make a complete example document with two tables, and say what should link to what (do you really want to inflict \resizebox{\textwidth}{!}{% on yout tables? – David Carlisle Jun 21 '14 at 16:45
• I have added a part of the second table. @Werner yes, that is exactly what I would want. – Alexandru Cimpanu Jun 21 '14 at 16:53
• Interestingly, you need to add a caption. Also, all of the references simply link to the caption: \ref{table.1} and \ref{table.2}. You really don't need any other labels. – John Kormylo Jun 21 '14 at 19:25
• That's rely the last solution I want to use, I have 32 rows in the tables and I would like to make each table full page. – Alexandru Cimpanu Jun 21 '14 at 19:29

The problem with linking to the line below the one you want seems to be inherent in tabulars. At first I compensated by putting an invisible target in the line above. Later I achieved the same effect using \raisebox (see hyperref and tabular).

\documentclass{article}
\usepackage{graphics}
\usepackage{hyperref}

\newlength{\offset}
\newcommand{\raisetarget}[1]% #1 = label
{\raisebox{\offset}[0pt][0pt]{\hypertarget{#1}{}}}

\begin{document}

\begin{table}[h]
\setlength{\offset}{\baselineskip}% must be outside tabular
\centering
\caption{Test files}
\smallskip
\resizebox{\textwidth}{!}{%
\begin{tabular}{|l|l|l|l|}
\hline
File name & Number of depots & Number of vehicles & Number of customers \\ \hline
& 2 & 4 & 18 \\ \hline
& 4 & 16 & 50 \\ \hline
& 4 & 8 & 50 \\ \hline
\end{tabular}
}
\end{table}

\begin{table}[h]
\centering
\caption{second table}
\smallskip
\resizebox{\textwidth}{!}{%
\setlength{\offset}{\baselineskip}% must be outside tabular
\begin{tabular}{l|l|l|l|l|l|l|l|l|l|l|l|l|}
\cline{2-13}
& \multicolumn{12}{c|}{\textit{\textbf{COST}}} \\
\cline{2-13}
& \multicolumn{3}{c|}{\textbf{NO OPTION}} & \multicolumn{3}{c|}{\textbf{REINITIALIZATION}} & \multicolumn{3}{l|}{\textbf{PERMUTATION}}
& \multicolumn{3}{l|}{\textbf{PERTURBANCE}} \\
\hline
\multicolumn{1}{|c|}{\textit{File name}} & \multicolumn{1}{c|}{\textit{None}} &  \multicolumn{1}{c|}{\textit{2Opt}} & \multicolumn{1}{c|}{\textit{All}}
& \multicolumn{1}{c|}{\textit{None}} & \multicolumn{1}{c|}{\textit{2Opt}} & \multicolumn{1}{c|}{\textit{All}} & \multicolumn{1}{c|}{\textit{None}}
& \multicolumn{1}{c|}{\textit{2Opt}} & \multicolumn{1}{c|}{\textit{All}} & \multicolumn{1}{c|}{\textit{None}} & \multicolumn{1}{c|}{\textit{2Opt}}
& \multicolumn{1}{c|}{\textit{All}} \\
\hline
& & & & & & & & & & & & \\
\hline
& & & & & & & & & & & & \\
\hline
& & & & & & & & & & & & \\
\hline
\end{tabular}
}
\end{table}

\end{document}
• hypcap and \hypcapspace did nothing for me. – John Kormylo Jun 22 '14 at 19:34

If I understand what you are trying to do then you want to have entries in two tables which function as links to corresponding entries in the other table. To do this you will have to create a dummy counter so that \label and \ref can track the different table entries. You probably also want to create the links directly using hyperref rather than with \ref.

Some variation on the following should do the the trick:

\documentclass{article}

\begin{document}

\newcounter{MyLabels}
\newcommand\MyLabel[2]{\refstepcounter{MyLabels}\label{#1-#2}%
\ifx#11\hyperref[#1-2]{#1}\else\hyperref[#1-1]{#1}\fi%
}
\begin{table}[h]
\centering
\caption{Test files}
\label{my-label}
\resizebox{\textwidth}{!}{%
\begin{tabular}{|l|l|l|l|}
\hline
File name & Number of depots & Number of vehicles & Number of customers \\ \hline
\MyLabel{0txt}{1}       & 2                & 4                   & 18                  \\ \hline
\MyLabel{1txt}{1}       & 4                & 16                 & 50                  \\ \hline
\MyLabel{2txt}{1}       & 4                & 8                   & 50
\end{tabular}
}
\end{table}

\begin{table}[h]
\centering
\resizebox{\textwidth}{!}{%
\begin{tabular}{l|l|l|l|l|l|l|l|l|l|l|l|l|}
\cline{2-13}
& \multicolumn{3}{c|}{\textbf{NO OPTION}}
& \multicolumn{3}{c|}{\textbf{REINITIALIZATION}}
& \multicolumn{3}{l|}{\textbf{PERMUTATION}}
& \multicolumn{3}{l|}{\textbf{PERTURBANCE}}
\\ \hline \multicolumn{1}{|c|}{\textit{File name}} &
\multicolumn{1}{c|}{\textit{None}} & \multicolumn{1}{c|}{\textit{2Opt}}
& \multicolumn{1}{c|}{\textit{All}} & \multicolumn{1}{c|}{\textit{None}}
& \multicolumn{1}{c|}{\textit{2Opt}} & \multicolumn{1}{c|}{\textit{All}}
& \multicolumn{1}{c|}{\textit{None}} &
\multicolumn{1}{c|}{\textit{2Opt}} & \multicolumn{1}{c|}{\textit{All}} &
\multicolumn{1}{c|}{\textit{None}} & \multicolumn{1}{c|}{\textit{2Opt}}
& \multicolumn{1}{c|}{\textit{All}} \\ \hline
\multicolumn{1}{|l|}{\MyLabel{0txt}{2} }              &
&                                    &
&                                    &
&                                   &
&                                    &
&                                    &
&                                   \\ \hline
\multicolumn{1}{|l|}{\MyLabel{1txt}{2}  }              &
&                                    &
&                                    &
&                                   &
&                                    &
&                                    &
&                                   \\ \hline
\end{tabular}
}
\end{table}

\end{document}

This produces:

The \newcounter{MyLabels} defines a counter that is then used to track the labels in your two tables via the \refstepcounter{MyLabels} in \MyLabel macro. The command \MyLabel is used to insert the hyperlink between the entries in the two tables. The syntax is:

\MyLabel{text}{table number=1 or 2}

This add a link around text to the corresponding entry in the other table. If your text is going to be reasonably complicated, or if it differs between the two tables, then you will probably need to add a third label argument to the macro. Similarly, if you need these links between more than two tables you'd probably need to specify the source and target tables in the macro.

• Your example seems to take me right after the link not on it. For example if I want to go to 0.txt I am able to see from 1.txt down. – Alexandru Cimpanu Jun 22 '14 at 7:40
• could you tell me why that happens? – Alexandru Cimpanu Jun 22 '14 at 14:58
• @AlexandruCimpanu I agree that this happens but, unfortunately, I have no idea what the cause i or how to fix it. I suspect that hyperref is not always able to accurately locate where a link points to, but it might be a bug in my implementation or a problem/limitation with pdf.... – Andrew Jun 22 '14 at 20:22

Maybe this is in line with what you're after:

\documentclass{article}
\usepackage{hyperref}

\usepackage{booktabs}
\begin{document}

\begin{table}[ht]
\centering
\caption{First table}\label{tbl:label-1}
\begin{tabular}{llll}
\toprule
File name & Number of depots & Number of vehicles & Number of customers \\
\midrule
\linkbacktext{0txt2}{0txt1}{\texttt{0.txt}} &  2 &  4 & 18 \\
\linkbacktext{1txt2}{1txt1}{\texttt{1.txt}} &  4 & 16 & 50 \\
\linkbacktext{2txt2}{2txt1}{\texttt{2.txt}} &  4 &  8 & 50 \\
\bottomrule
\end{tabular}
\end{table}

\begin{table}[ht]
\centering
\caption{Second table}\label{tbl:label-2}
\begin{tabular}{llll}
\toprule
File name & Number of depots & Number of vehicles & Number of customers \\
\midrule
\linkbacktext{0txt1}{0txt2}{\texttt{0.txt}} &  2 &  4 & 18 \\
\linkbacktext{1txt1}{1txt2}{\texttt{1.txt}} &  4 & 16 & 50 \\
\linkbacktext{2txt1}{2txt2}{\texttt{2.txt}} &  4 &  8 & 50 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}