我遇到一种情况,即我有一个带有重复标签的表格环境:
\documentclass{article}
\begin{document}
\begin{tabular}{ll}\hline
Label 1 & Bunch of text\\
Label 2 & More text\\
Label 3 & Even more things to write\\
Label 4 & Lots of stuff\\\hline
Label 1 & Second set of entries\\
Label 2 & Can be written here\\
Label 3 & All sorts of stuf\\
Label 4 & Last of the second entries\\\hline
Label 1 & Third set of entries\\
Label 2 & With its own unique text\\
Label 3 & But the same set of labels\\
Label 4 & Off to the left\\\hline
\end{tabular}
\end{document}
本质上,我有一张表,上面有一堆小表,所有标签都相同。如上例所示,我必须手动重复每个标签。有没有办法自动创建这些标签?或者,有没有办法创建一个表格模式,我们只需填写缺失的信息?
答案1
这是一个可能的解决方案,带有一个自动重置的计数器;应该清楚如何将标签修改为真实的标签。
\documentclass{article}
\usepackage{array}
\newcommand{\rowlabels}{%
\ifcase\value{rowlabelcount}\or
Label 1\or
Label 2\or
Label 3\or
Label 4\fi
}
\newcommand{\printlabel}{%
\stepcounter{rowlabelcount}%
\ifnum\value{rowlabelcount}=5 % change here the value when resetting is needed
\setcounter{rowlabelcount}{1}%
\fi
\rowlabels
}
\newcounter{rowlabelcount}
\begin{document}
\begin{tabular}{>{\printlabel}ll}\hline
& Bunch of text\\
& More text\\
& Even more things to write\\
& Lots of stuff\\\hline
& Second set of entries\\
& Can be written here\\
& All sorts of stuf\\
& Last of the second entries\\\hline
& Third set of entries\\
& With its own unique text\\
& But the same set of labels\\
& Off to the left\\\hline
\end{tabular}
\end{document}
答案2
您还可以使用包裹collcell
定义自定义列环境。我已定义L
以下环境,以便在未提供文本时自动添加标签。如果该列中已有数据,则该文本将用作标签。此处添加的红色只是为了显示自动标签的添加位置。
下面的默认设置是要求\RestartLabelCounter
在需要时调用来重新启动标签。但是,如果调用
\toggletrue{AutoResetCounter}
\setcounter{LabelCounter}{0}
代码将每输入 4 次自动重置:
笔记:
在当前实现中你需要调用
\setcounter{LabelCounter}{0}
前每个表格,除非您希望标签从一个表格延续到下一个表格,如果需要,这也可以自动化。
代码:
\documentclass{article}
\usepackage{xcolor}
\usepackage{collcell}
\usepackage{ifmtarg}
\usepackage{etoolbox}
% http://tex.stackexchange.com/questions/125714/use-of-ifmtarg-yields-spurious-space
\makeatletter
\newcommand{\IfIsEmptyArg}[3]{%
\expandafter\@ifmtarg\expandafter{#1}{#2}{#3}
}
\makeatother
\newtoggle{AutoResetCounter}%
\togglefalse{AutoResetCounter}%
\newcounter{LabelCounter}%
\setcounter{LabelCounter}{0}%
\newcommand*{\StepCounter}[1]{%
\iftoggle{AutoResetCounter}{%
\ifnum\value{#1}=4\relax%
\setcounter{#1}{1}%
\else
\stepcounter{#1}%
\fi
}{%
\stepcounter{#1}%
}%
}%
\newcommand*{\RestartLabelCounter}{\setcounter{LabelCounter}{0}}%
\newcommand*{\ApplyLabel}[1]{%
\IfIsEmptyArg{#1}{%
\textcolor{red}{Label \theLabelCounter}%
\StepCounter{LabelCounter}%
}{%
#1%
}%
}%
\newcolumntype{L}{>{\collectcell\ApplyLabel}l<{\endcollectcell}}
\begin{document}
\begin{tabular}{Ll}\hline
& Bunch of text\\
& More text\\
& Even more things to write\\
& Lots of stuff\\
\hline
Label X & Second set of entries\\
Label Y & Can be written here\\
& All sorts of stuf\\
& Last of the second entries \RestartLabelCounter\\
\hline
& Third set of entries\\
& With its own unique text\\
& But the same set of labels\\
& Off to the left\\\hline
\end{tabular}
\bigskip
\toggletrue{AutoResetCounter}
\setcounter{LabelCounter}{0}
With auto reset enabled:\medskip
\begin{tabular}{Ll}\hline
& Bunch of text\\
& More text\\
& Even more things to write\\
& Lots of stuff\\
\hline
& Another set of entries\\
& With its own unique text\\
& But the same set of labels\\
& Off to the left\\\hline
\end{tabular}
\end{document}
答案3
我不会使用计数器和空单元格,而是只使用自定义语法来填充内部表,同时使用数组包\extrarowheight
使文本远离水平线。
\documentclass{article}
\usepackage{array}
\newenvironment{foolist}
{\par
\setlength\extrarowheight{2pt}%
\centering\begin{tabular}{ll}\hline}
{\end{tabular}\par}
\newcommand\foo[4]{%
Label 1 & #1\\
Label 2 & #2\\
Label 3 & #3\\
Label 4 & #4\\\hline
}
\begin{document}
\begin{foolist}
\foo{Bunch of text}
{More text}
{Even more things to write}
{Lots of stuff}
\foo{Second set of entries}
{Can be written here}
{All sorts of stuf}
{Last of the second entries}
\foo{Third set of entries}
{With its own unique text}
{But the same set of labels}
{Off to the left}
\end{foolist}
\end{document}
答案4
这里没什么可说的。所以我只说明一种你可以使用的方法
\documentclass{article}
\makeatletter
\newcounter{mytablelabel}
\newcommand{\myentry}{%%
\@ifnextchar*{\setcounter{mytablelabel}{0}\@myentry}
{\@myentry*}}
\def\@myentry*#1{\stepcounter{mytablelabel}Label \arabic{mytablelabel} & #1 \\}
\makeatother
\pagestyle{empty}
\begin{document}
\begin{tabular}{ll}\hline
\myentry{Bunch of text}
\myentry{More text}
\myentry{Even more htings to write}
\myentry{Lots of stuff}\hline
\myentry*{Bunch of text}
\myentry{More text}
\myentry{Even more things to write}
\myentry{Last of the second entries}\hline
\myentry*{Third set of entries}
\myentry{With its own unique text}
\myentry{But the same set of labels}
\myentry{Off to the left}
\end{tabular}
\end{document}
这里我创建了一个计数器和一个命令,用于格式化表格中每行的内容。我允许已加星标重置计数器的命令版本。如果您有更复杂的问题,可以对此进行调整。