LaTeX 中的引用表格行

LaTeX 中的引用表格行

我想对表格中的每一行进行编号和标记,以便以后可以参考它们。

但我得到了流畅的输出:

代码输出

我希望输出看起来像这样:

我希望输出看起来像这样

我该如何解决这个问题?

代码:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[danish]{babel}
\usepackage{lscape}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{float}
\usepackage[top=2.81cm, bottom=2.75cm,right=2cm, left=2cm]{geometry}
\usepackage{url}
\usepackage[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\usepackage{setspace}
   \onehalfspacing
   \usepackage[T1]{fontenc}


\begin{document}

\begin{table}[H]
\centering
\label{tab:Q1}
\begin{tabular}{|c|c|l|l|}
\hline
Number & Theory & Question & Explanation  \\ \hline
1 \label{que:whyisit} & B & Why is it you think ... & This is a good question\\
2 \label{que:doyouthink} & A & Do you think  ... & This Is also a good question\\
3 \label{que:isitcorrect} & B & is it correct that you think ... & This is question \\ \hline
\end{tabular}
\caption{Table with questions}
\end{table}
Question \ref{que:whyisit} is about ... \\
Question \ref{que:doyouthink} is about ... \\
Question \ref{que:isitcorrect} is about ...

\end{document}

答案1

这是一个使用新列类型的方法N,它会自动增加rowcntr,因为\label应该在后台使用计数器(尽管\@currentlabel也可以进行写入操作,请参阅 gernot 的回答)。

使用rowcntr每个新命令或使用tabular新表时会重置(即计数器已增加)\captiontable

在我看来,行号应该以表号作为前缀,以便在出现多个表问题时避免混淆所指的问题。

超链接确实有效!

我还将错误放置的表格标签移到了它所属的位置,即使用\caption

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[danish]{babel}
\usepackage{lscape}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{float} 
\usepackage{array}
\usepackage[top=2.81cm, bottom=2.75cm,right=2cm, left=2cm]{geometry}

\usepackage{etoolbox}
\usepackage{setspace}
\onehalfspacing

\usepackage{url}
\usepackage[hidelinks]{hyperref}

\hypersetup{breaklinks=true}

\newcounter{rowcntr}[table]
\renewcommand{\therowcntr}{\thetable.\arabic{rowcntr}}

% A new columntype to apply automatic stepping
\newcolumntype{N}{>{\refstepcounter{rowcntr}\therowcntr}c}

% Reset the rowcntr counter at each new tabular
\AtBeginEnvironment{tabular}{\setcounter{rowcntr}{0}}



\begin{document}

\begin{table}[H]
\centering
\caption{Table with questions} \label{tab:Q1}
\begin{tabular}{|N|c|l|l|}
\hline
\multicolumn{1}{|c}{Number} & Theory & Question & Explanation  \\ \hline
\label{que:whyisit} & B & Why is it you think ... & This is a good question\\
\label{que:doyouthink} & A & Do you think  ... & This Is also a good question\\
\label{que:isitcorrect} & B & is it correct that you think ... & This is question \\ \hline
\end{tabular}
\end{table}

Question \ref{que:whyisit} is about ... \\
Question \ref{que:doyouthink} is about ... \\
Question \ref{que:isitcorrect} is about ... 

\clearpage

\begin{table}[H]
\centering
\caption{Table with questions} \label{tab:Q2}
\begin{tabular}{|N|c|l|l|}
\hline
\multicolumn{1}{|c}{Number} & Theory & Question & Explanation  \\ \hline
\label{que:whyisitother} & B & Why is it you think ... & This is a good question\\
\label{que:doyouthinkother} & A & Do you think  ... & This Is also a good question\\
\label{que:isitcorrectother} & B & is it correct that you think ... & This is question \\ \hline
\end{tabular}

\end{table}

Question \ref{que:whyisitother} is about ... \\
Question \ref{que:doyouthinkother} is about ... \\
Question \ref{que:isitcorrectother} is about ... 



\end{document}

在此处输入图片描述

答案2

根据定义

\makeatletter
\newcommand\newtag[2]{#1\def\@currentlabel{#1}\label{#2}}
\makeatother

在序言中你可以使用命令

\newtag{word}{label name}

在此点插入并使用或word在此处之前或之后引用它。\ref{label name}\pageref{label name}

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[danish]{babel}
\usepackage{lscape}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color} 
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{float} 
\usepackage[top=2.81cm, bottom=2.75cm,right=2cm, left=2cm]{geometry}
\usepackage{url}
\usepackage[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\usepackage{setspace}
   \onehalfspacing
   \usepackage[T1]{fontenc}

\makeatletter
\newcommand\newtag[2]{#1\def\@currentlabel{#1}\label{#2}}
\makeatother

\begin{document}

\begin{table}[H]
\centering
\label{tab:Q1}
\begin{tabular}{|c|c|l|l|}
\hline
Number & Theory & Question & Explanation  \\ \hline
\newtag{1}{que:whyisit} & B & Why is it you think ... & This is a good question\\
\newtag{2}{que:doyouthink} & A & Do you think  ... & This Is also a good question\\
\newtag{3}{que:isitcorrect} & B & is it correct that you think ... & This is question \\ \hline
\end{tabular}
\caption{Table with questions}
\end{table}
Question \ref{que:whyisit} is about ... \\
Question \ref{que:doyouthink} is about ... \\
Question \ref{que:isitcorrect} is about ... 

\end{document}

如果你愿意,你可以删除\label- 功能\newtag并只定义

\makeatletter
\newcommand\newref[1]{#1\def\@currentlabel{#1}}
\makeatother

并将其用作

\newref{1}\label{que:whyisit} & ...
\newref{2}\label{que:doyouthink} & ...

答案3

做出反驳也是我会做的,但是围绕这个解决方案有 2 个答案(均为 +1),似乎足够了。

作为概念证明,如果计数器不是解决方案,则答案会有所不同,因为“数字”无法自动计算和排版,甚至不是数字。想想一些分数、识别号(不一定是有序和连续的)、考试中问题的值等,或者一些十六进制代码等。

由于您使用的是hyperref,因此您可以在此处使用\hypertarget\hyperlink。缺点是交叉引用的文本不是自动的,但仍是活动链接,优点是单元格内容和引用文本都是免费的,因此您可以使用标识键作为 F24E,引用拼写出的数字(“nine”而不是“9”)等。示例:

姆韦

\documentclass{article}
\usepackage{booktabs}
\usepackage[colorlinks]{hyperref}
\parskip2em\parindent0pt
\begin{document}

\begin{tabular}{ccll}\toprule
Number                        & Theory & Question                & Explanation  \\\midrule
\hypertarget{whyisit}{43}     & B      & Why is it you think ... & This is a good question\\
\hypertarget{doyouthink}{36}  & A      & Do you think  ...       & This is also a good question\\
\hypertarget{isitcorrect}{13} & B      & is it correct that  ... & This is question \\\bottomrule
\end{tabular}

Question \hyperlink{whyisit}{43} is about ... \\
Question  \hyperlink{doyouthink}{here (36)} is about ... \\
Question \hyperlink{isitcorrect}{13} is about ... 

\end{document}

对于任意数字(但仅限数字),一个更奢侈的替代方案可能是spreadtab包。交叉引用的编号是自动的,但不是活动链接。但是,如果您需要对这些数字进行一些数学运算(总分之和等),它可能很有用。该示例显示了表格内部和外部的交叉引用。下面的示例显示可以通过表格内的单元格位置调用值,或者使用正文中的标签进行交叉引用:

mwe2

\documentclass{article}
\usepackage{spreadtab,booktabs}
\begin{document}

\begin{spreadtab}{{tabular}{ccll}}
@Number        & @Theory & @Question                & @Explanation\\\midrule
43tag(whyisit) & @B      & @Why is it you think ... & @This is a good question\\
36             & @A      & @Do you think ...        & @This is also a good question\\
13             & @B      & @is it correct that ...  & @This is question \\\bottomrule
\addlinespace[.5ex]
\multicolumn{4}{l}{Question :={a4} in this table is about ... }\\
\end{spreadtab}

\bigskip\bigskip\bigskip

Question \STtag{whyisit} in that table is about ... 

\end{document}

答案4

与提到的方法几乎相同这个答案也适用于 LyX。唯一的区别是,你可以将 LaTeX 前言简化为

\newcommand\newtag[2]{#1\def\@currentlabel{#1}\label{#2}}

相关内容