如何使用(a)、(b)、(c)等引用表格单元格?

如何使用(a)、(b)、(c)等引用表格单元格?

我有一张有两列的表格。第一列是形式 (a)、(b)、(c) 等的计数器。第二列是内容。

我想要在表格单元格和文本中引用带有标签 (a)、(b)、(c) 等的每一行。

\label{cell:a}(肯定)不起作用。请参阅可编辑代码@overleaf 以及下图。

标签表格单元格

如何实现?

\documentclass{report}
\usepackage{graphicx}
\usepackage{xcolor}

\begin{document}
\begin{table}[t]
  \centering
  \label{table:label-in-cell}
  \begin{tabular}{|c|c|}
  \hline
  (a) \label{cell:a} & aaa \\ \hline
  (b) \label{cell:b} & bbb (Cell \ref{cell:a} [\textcolor{red}{What I want: Cell (a)}]) \\ \hline
  \end{tabular}
\end{table}

  refs to table cells: Cell \label{cell:a} and Cell \label{cell:b}

  \textcolor{red}{What I want: Cell (a) and Cell (b)}
\end{document}

答案1

LaTeX 的\label交叉\ref引用机制基于\label能够将其参数与最近递增的计数器变量相关联。

为了实现格式化目标,您需要

  1. 定义一个计数器变量,

  2. 定义计数器变量的显示方式(此处为:“(a)”、“(b)”、“(c)”等,而不是“1”、“2”、“3”等),以及

  3. 必要时,适当增加此计数器变量。我说的“适当”是指必须使用\refstepcounter而不是\stepcounter执行增加。\refstepcounter之所以需要这样做,是因为我们需要告诉 LaTeX哪个柜台使用——,在创建 的参数和计数器\refstepcounter之间的关联或链接时,最近通过 -- 增加的计数器。\label

  4. 如果左侧单元格的整个可见内容是计数器变量的值,并且计数器的值应该是连续的(如您提供的示例代码中的情况),我还建议创建一个专用的列类型,自动增加和显示计数器变量的值。

在此处输入图片描述

单独的评论:大概,您还希望cellcounter每次启动新环境时都将其重置为 0。table为了实现这个额外的目标,您需要 (a) 更改为\newcounter{cellcounter}\newcounter{cellcounter}[table]以便将cellcounter计数器从属于table计数器,(b)\caption在每个表中使用指令,因为\caption指令实际上会增加table计数器,以及 (c) 将\caption指令放在代码块之前而不是之后tabular。另外:由于您的示例代码只包含一个table环境并且没有\caption指令,因此我没有在以下代码中实现前面的指令。

\documentclass{report}

% Define a new counter:
\newcounter{cellcounter} % feel free to use a different name
% Define how this counter variable should be displayed:
\renewcommand{\thecellcounter}{(\alph{cellcounter})}
\usepackage{array} % for '\newcolumntype' macro
% A column type that automatically increments and displays the counter
\newcolumntype{Z}{>{\refstepcounter{cellcounter}%
                    \thecellcounter}c}

\begin{document}

\begin{table}[ht]
  \centering
  %%\label{table:label-in-cell} % this doesn't have the desired effect
  \begin{tabular}{|Z|c|}
  \hline
  \label{cell:a} & aaa \\ \hline
  \label{cell:b} & Cell \ref{cell:a} \\ \hline
  \end{tabular}
\end{table}

\end{document}

相关内容