![如何为表格的每一行添加标签和引用?](https://linux22.com/image/292999/%E5%A6%82%E4%BD%95%E4%B8%BA%E8%A1%A8%E6%A0%BC%E7%9A%84%E6%AF%8F%E4%B8%80%E8%A1%8C%E6%B7%BB%E5%8A%A0%E6%A0%87%E7%AD%BE%E5%92%8C%E5%BC%95%E7%94%A8%EF%BC%9F.png)
我有 2 个表,它们有一个共同的列,因此我希望第一个表中的每一行都指向第二个表中的相应行,反之亦然。此外,标签是否可以是单元格内的值。
以下是第一张表的一部分:
\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}
以下是第二张表的一部分:
\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}
第一个表和第二个表的标签和引用如下所示:
\label{0txt1} \ref{0txt2}
\label{0txt2} \ref{0txt1}
。
目前我只能从第二张桌子走到第一张桌子,而不能反过来。
答案1
链接到所需行下方的行的问题似乎是表格固有的问题。起初,我通过在上方的行中放置一个不可见的目标来弥补。后来,我使用 \raisebox 实现了同样的效果(请参阅超链接和表格)。
\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}
答案2
如果我理解了您要做什么,那么您希望在两个表中都有条目,这些条目可作为指向另一个表中相应条目的链接。为此,您必须创建一个虚拟计数器,以便\label
和\ref
可以跟踪不同的表条目。您可能还想直接使用hyperref
而不是使用来创建链接\ref
。
下面的一些变化应该可以达到目的:
\documentclass{article}
\usepackage[colorlinks=true]{hyperref}
\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}
得出的结果为:
定义一个计数器,然后通过in宏\newcounter{MyLabels}
使用该计数器跟踪两个表中的标签。该命令用于在两个表中的条目之间插入超链接。语法为:\refstepcounter{MyLabels}
\MyLabel
\MyLabel
\MyLabel{text}{table number=1 or 2}
这会添加指向另一个表中相应条目的链接text
。如果您的文本相当复杂,或者两个表之间的文本不同,那么您可能需要label
向宏添加第三个参数。同样,如果您需要两个以上表之间的这些链接,您可能需要source
在宏中指定和目标表。
答案3
也许这符合你的要求:
\documentclass{article}
\usepackage{hyperref}
\newcommand{\linkbacktext}[3]{% \linkbacktext{<to>}{<name>}{<text>}
\hyperlink{#1}{\smash{\raisebox{\normalbaselineskip}{\hypertarget{#2}{\raisebox{-\normalbaselineskip}{#3}}}}}}
\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}
\linkbacktext{<to>}{<name>}{<text>}
<name>
插入带有文本的链接<text>
。它还提供指向的超链接<to>
。