如何交叉引用单个表格单元格?

如何交叉引用单个表格单元格?

我想使用允许交叉引用单个单元格的标签来实现这一点:

在此处输入图片描述

我编写的 MWE 模仿了这种行为:

\documentclass[a4paper,10pt]{article}

\usepackage{subcaption}
\usepackage{varioref}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{tabularray}
\usepackage{xcolor}

\setlength{\parindent}{0mm}
\hypersetup{colorlinks=true}

% EDIT 1: new definitions
% define an ID
\newcommand{\iddef}[1]{\hypertarget{#1}{#1}}
% reference an ID
\newcommand{\idref}[1]{\hyperlink{#1}{#1}}

\begin{document}

% easier screenshots
\vspace*{\fill}
\section{My friends}
\Cref{friends-name} is a comprehensive list of my friends.

\begin{table}[!h]
 \centering
 \caption{My friends}
 \label{friends-name}
 \begin{tblr}{}
  ID & FIRST NAME & LAST NAME \\
  % here I would like a label for each of F1, F2, etc. cells
  % it doesn't have to be a counter since I specify IDs manually
  % at least one of ref/cref/zcref/nameref should appear as a red "F1", "F2" and point to the cell, not the whole table
  \iddef{F1} & Alice & Ross \\
  \iddef{F2} & Bob & Franklin \\
 \end{tblr}
\end{table}

As seen in \cref{friends-name}, I only have two friends.
\clearpage
\subsection{Statistics about my friends}

\begin{table}[!h]
 \centering
 \caption{Age of my friends}
 \label{friends-age}
 \begin{tblr}{colspec={lr}}
  ID & AGE (YEARS) \\
  % here I would like a clickable cross-reference to F1, F2, etc. cells in the first table
  % now references are pointing to the whole table (friends-name) and not to single row
  %\hyperref[friends-name]{F1} & 20 \\
  %\hyperref[friends-name]{F2} & 30 \\
  % EDIT 1: now references are pointing to an individual row, but it's the row below!
  \idref{F1} & 20 \\
  \idref{F2} & 30 \\
 \end{tblr}
\end{table}

% here I would like F1 to be a clickable cross-reference to F1 cell in the first table
As seen in \cref{friends-age}, the average age among my friends is 25 years, while \idref{F1} is the youngest.

\end{document}

理想的结果是,当您单击单元格引用时,PDF 查看器会与该单元格行对齐。这对于您已经知道每个列名称但只想读取单元格行的值的高表格非常有用。

编辑1

感谢@Fran 的建议,我添加了两个新的命令定义,但它们指向下面的一行

答案1

来自的解决方案https://tex.stackexchange.com/a/17138/213962https://tex.stackexchange.com/a/17138/213962. 基本上我们将目标设置为比内容输出高一行。

警告:

  • 如果 ID 不是在单元格中垂直居中,而是放在顶部,它也适用于多行行。
  • \hypertarget(内部超链接引用 ID)中的第一个“#1”必须成为第一个“#1”\hyperlink
  • \hypertarget(可读文本)中的第二个“#1”应该成为第二个“#1”\hyperlink
\documentclass[a4paper,10pt]{article}

\usepackage{subcaption}
\usepackage{varioref}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{tabularray}
\usepackage{xcolor}

\setlength{\parindent}{0mm}
\hypersetup{colorlinks=true}

% define a new ID
\makeatletter
 \newcommand{\iddef}[1]{\Hy@raisedlink{\hypertarget{#1}{}}#1}
\makeatother
% reference an existing ID
\newcommand{\idref}[1]{\hyperlink{#1}{#1}}

\begin{document}

% easier screenshots
\vspace*{\fill}
\section{My friends}
\Cref{friends-name} is a comprehensive list of my friends.

\begin{table}[!h]
 \centering
 \caption{My friends}
 \label{friends-name}
 \begin{tblr}{}
  ID & FIRST NAME & LAST NAME \\
  \iddef{F1} & Alice & Ross \\
  \iddef{F2} & Bob & Franklin \\
  \iddef{F3} & Carl & { March \\ Lil \\ Geoffrey } \\
  \iddef{F4} & Daniel & Ross \\
 \end{tblr}
\end{table}

As seen in \cref{friends-name}, I now have four friends. \idref{F3} has three surnames, so I call him just ``Carl''. \idref{F4} is Alice's brother.

\clearpage
\subsection{Statistics about my friends}

\begin{table}[!h]
 \centering
 \caption{Age of my friends}
 \label{friends-age}
 \begin{tblr}{colspec={lr}}
  ID & AGE (YEARS) \\
  \idref{F1} & 20 \\
  \idref{F2} & 30 \\
  \idref{F3} & 25 \\
  \idref{F4} & 25 \\
 \end{tblr}
\end{table}

As seen in \cref{friends-age}, the average age among my friends is 25 years, while \idref{F1} is the youngest.

\end{document}

id 被正确引用

相关内容