2

I would like to accomplish this using labels that allow cross-referencing of individual cells:

enter image description here

The MWE I wrote to mimic this behavior:

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

The desired outcome is that when you click a cell reference, your PDF viewer aligns to that cell row. This is helpful for tall tables where you already know each column name, but you want to be read just the values of the cell row.

EDIT 1

Thanks to @Fran suggestions, I've added two new command definitions, but they are pointing one row below

2
  • Check if this help, or if you have some R background, indeed this or this to easily reference but (not link) individual cells, sums, averages or whatever about you can do with a data frame than you show also as a nice latex table data.
    – Fran
    Jul 21, 2023 at 10:36
  • @Fran, Mico, please see my EDIT 1
    – Polizi8
    Jul 21, 2023 at 11:11

1 Answer 1

1

Solutions from https://tex.stackexchange.com/a/17138/213962 and https://tex.stackexchange.com/a/17138/213962. Basically we set the target one row higher than the output of the content.

Caveat:

  • It works well also for multiline rows, if the ID is not vertically centered in the cell, but placed at the top
  • The first "#1" in \hypertarget (the internal hyperref reference ID) must be the first "#1" in \hyperlink
  • The second "#1" in \hypertarget (the readable text) should be the second "#1" in \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 are referenced properly

1
  • 2
    you can use \MakeLinkTarget to create the target, it raises the target automatically. Also it will not error if hyperref is not loaded. See texdoc hyperref-linktarget for the documentation. Jul 21, 2023 at 11:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .