[Tex/LaTex] How to add label and reference to each row of a table

hyperreflabelstables

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.

Best Answer

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}
\hypersetup{pdftoolbar=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true}

\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
\raisetarget{0txt1}\hyperlink{0txt2}{0.txt}
  & 2 & 4 & 18 \\ \hline
\raisetarget{1txt1}\hyperlink{1txt2}{1.txt}
  & 4 & 16 & 50 \\ \hline
\raisetarget{2txt1}\hyperlink{2txt2}{2.txt}
  & 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
 \multicolumn{1}{|l|}{\raisetarget{0txt2}\hyperlink{0txt1}{0.txt}}
 & & & & & & & & & & & & \\
\hline
 \multicolumn{1}{|l|}{\raisetarget{1txt2}\hyperlink{1txt1}{1.txt}}
 & & & & & & & & & & & & \\
\hline
 \multicolumn{1}{|l|}{\raisetarget{2txt2}\hyperlink{2txt1}{2.txt}}
 & & & & & & & & & & & & \\
\hline
\end{tabular}
}
\end{table}

\end{document}