5

Is it possible to automate the numbering in a tabular like this?

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

  • Unfortunately not as I don’t want every row to be numbered. – Wendy Taylor Nov 09 '23 at 07:20
  • But if you want so manually select the rows, that should be numbered, how should this be automatically? Maybe you want to use a p column for the last column, so you can use linebreaks in the cell and indeed can automatically number each table row. – cabohah Nov 09 '23 at 07:23
  • Maybe you should tell more about the real problem. If you just want to define and use a counter, see \newcounter, \setpcounter (or \refstepcounter if you want to refer to) and \the<counter>. Or maybe is shouldn't really be a table but simething like align with only some lines tagged? Even using enumerate and some items without numbering could be a suggestion. Sorry but it is unclear. – cabohah Nov 09 '23 at 07:28
  • I think that will work perfectly. I wasn’t aware of that type of cell. Thank you! – Wendy Taylor Nov 09 '23 at 07:29
  • 3
    I’m voting to close this question because it was solved in the comments. – José Carlos Santos Nov 09 '23 at 08:01

2 Answers2

7

Like this?

enter image description here

One way is to use tabularray package. Using it you can write (based on this answer) the following:

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

Zarko
  • 296,517
3

If I understand your formatting objective correctly, you wish to create a tabular environment with two columns; the left-hand column should display an automatically incremented row number, and the right-hand column will contain text and should allow automatic line wrapping. Moreover, there should be a mechanism to override the automatic display of a row number in the left-hand column, say, because you need to be able to typeset the word "Step" in the header row.

The following code achieves these goals. Observe that the utility macro called \ZZ -- feel free to choose a different name ... -- lets you override the automatic row numbering. \ZZ{Step} shows the word "Step"; \ZZ{} leaves the cell blank, and it doesn't increment the counter called rownum.

enter image description here

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

Mico
  • 506,678