1

I know a way to automatically number. But at the same time, you still need to write the word \Rownum on a new line every time. Is there anything I can do to avoid writing this all the time?

    \documentclass[a4paper,12pt]{article}
\usepackage{cmap}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{caption}
\begin{document}
    \newcounter{rownum} 
    \setcounter{rownum}{0}
    \newcommand{\Rownum}{\stepcounter{rownum}
        \arabic{rownum}. }

\begin{tabular}{ |l|l| } number & value \ \Rownum & 4.53 \ \Rownum & 6.74 \ \end{tabular} \end{document}

  • 1
    https://tex.stackexchange.com/a/21245/134144 may be interesting. – leandriis Jan 18 '22 at 11:01
  • 2
    You can load the array package and use {|>{\Rownum}l|l|} as the table preamble. Of course for the first row, you'd have to use \multicolumn{1}{|l}{number}. – Bernard Jan 18 '22 at 11:02
  • 1
    Well, really {|l|} to no lost the awful vertical lines, but Bernard comment is the simplest (best) general answer. – Fran Jan 18 '22 at 11:35

3 Answers3

1

An implementation with expl3.

enter image description here

\documentclass{article}
\ExplSyntaxOn
% #1 body var #2 body #3 head
\cs_new_protected:Nn \__table_with_number:Nnn {
  \regex_split:nnN { \c{\\} } {#2} \l__table_line_seq
  \tl_clear_new:N #1
  \seq_map_indexed_inline:Nn \l__table_line_seq {
    \int_compare:nNnTF {##1} = {1} {
      \tl_put_right:Nn #1 { #3 & ##2 \\ }
    } {
      \tl_if_empty:nF {##2}
        { \tl_put_right:Nn #1 { \int_eval:n { ##1 - 1 }. & ##2 \\ } }
    }
  }
}

\NewDocumentEnvironment { Table } { O{number} m +b } { __table_with_number:Nnn \l__table_body_tl {#3} {#1} \begin{tabular}{#2} \l__table_body_tl \end{tabular} } \ExplSyntaxOff

\begin{document}

\begin{Table}{ll} value \ 1 \ 2.3 \ \end{Table}

\end{document}

ZhiyuanLck
  • 4,516
0

Thanks everyone, I found the shortest, easiest and most effective way:

\documentclass[a4paper,12pt]{article}
\usepackage{cmap}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{caption}
\usepackage{array,etoolbox} % For automatic numbering
\begin{document}
    \newcounter{rownum} % Declare a new counter for autonumbering rows in the table
    \setcounter{rownum}{0} % Set the initial value of the counter
    \newcommand{\Rownum}{\stepcounter{rownum} % Add one when calling a function
        \arabic{rownum} } % You can add a dot and a space at the end
\begin{tabular}{|>{\Rownum}l|l|}
    \multicolumn{1}{|l|}{№} & value \\
    & 4.53 \\
    & 6.74 \\
\end{tabular}

\end{document}

enter image description here

0

mwe


As commented, Bernard's solution is the simplest and therefore the best to use the counter, IMHO. Said that, another possibility is not set any counter nor any complex preamble.

If you don't know R and knitr, this solution is a bit like killing flies with cannon shots (although I strongly suggest you to know knitr if you often deal with numeric data and/or graphs in LaTeX documents). If you know how compile the test.Rnw below, is very simple, as any data frame output show by default row numbers, so you don't have to do anything special, just have some data frame (it can be imported from several formats as CSV or Excel or just typed in R code as showed in the example) and print it like a LaTeX table with xtable (or kable, or some others R packages):

% test.Rnw
\documentclass{article}
\usepackage{booktabs} % for better rules 
\begin{document}
<<table1, echo=F,results='asis'>>=
library(xtable)
# the data
df  <- data.frame( 
  value=c(4.53, 6.74,7.1),
  price=c(23,45,15))
# the table
xtable(df)
@

With a header for the counter (rather superfluous, but ...) and some table format:

<<table2,echo=F,results='asis'>>= df$number <- rownames(df) # numbers as variable df <- df[,c(3,1,2)] # numbers as first column print(xtable(df,align="lccc",digits=c(0,0,2,0)), include.rownames=F,booktabs=T) @

\end{document}

Fran
  • 80,769