自动对表格中的某些行进行编号

自动对表格中的某些行进行编号

是否有可能在这样的表格中自动编号?

\documentclass{article}


\begin{document}

\begin{tabular}{lcl}
\hline
Step && Instruction \\\hline
   1  & & At this step we will \hfill \\\hline
 2 & & Then there is another step \\
 && then more \\
 & $\vert$ & this \\\hline
 3 && then some more text \\
 & $\vert$ & here is some more \\
 & $\vert$ & then some more \\\hline
\end{tabular}


\end{document}

答案1

像这样?

在此处输入图片描述

一种方法是使用tabularray包。使用它你可以编写(基于此回答) 下列:

\documentclass{article}
\usepackage{tabularray}

\begin{document}
\begin{tblr}{width=0.5\linewidth,
             hlines,
             colspec = {l X[l]},
             cell{2-Z}{1} = {cmd=\the\numexpr\arabic{rownum}-1},
             row{1}  = {font=\bfseries}  % if you liked boldface columns headers ...
             }
Step    &   Instruction                     \\
        &   At this step we will            \\
        &   {Then there is another step\\
                this}                 \\
        &   {then some more text\\
                here is some more\\
                then some more}       \\
\end{tblr}
\end{document}

答案2

如果我正确理解了您的格式化目标,您希望创建一个tabular包含两列的环境;左侧列应显示自动递增的行号,右侧列将包含文本并应允许自动换行。此外,应该有一种机制来覆盖左侧列中行号的自动显示,例如,因为您需要能够在标题行中排版单词“Step”。

以下代码实现了这些目标。请注意,名为 的实用程序宏\ZZ(请随意选择其他名称...)允许您覆盖自动行编号。\ZZ{Step}显示单词“Step”;\ZZ{}单元格留空,并且不会增加名为 的计数器rownum

在此处输入图片描述

\documentclass{article}
\newcounter{rownum}
\usepackage{array} % for \newcolumntype macro
\newcolumntype{N}{% increment and display the value of 'rownum' counter
   >{\refstepcounter{rownum}\therownum}l}
\newcolumntype{P}[1]{% suppress full justification
   >{\raggedright\arraybackslash}p{#1}}
\newcommand\ZZ[1]{\multicolumn{1}{@{}l}{#1}} % macro to override action of 'N' col. type 
\begin{document}

\begin{tabular}[t]{@{} N P{3cm} @{}} % choose width of 2nd column as needed
\hline
\ZZ{Step} & Instruction \\
\hline
& At this step we will then more this \\
\hline
& Then there is another step then more this then \\
\hline
\ZZ{} & then some more text here is some more then some more then more  \\
\hline
& then some more text here is some more then some more then more more then more \\
\hline
\end{tabular}
\qquad
\setcounter{rownum}{0} % reset 'rownum' counter to 0
\begin{tabular}[t]{@{} N P{3.5cm} @{}} % choose width of 2nd column as needed
\hline
\ZZ{Step} & Instruction \\
\hline
& At this step we will then more this \\
\hline
& Then there is another step then more this then more this \\
\hline
\end{tabular}

\end{document}

相关内容