4

I'm working in a tabular environment. I am a writing a set of 20+ rows in the environment. At first, I was going to number each row (automatically). But then it occurred to me that I could just number every fifth row: 5, 10, 15, 20... and so forth.

What is the simplest way to do this?

Here is my source code:

\documentclass[letterpaper,12pt]{article}

\usepackage{array}
\newcounter{rowcount}
\setcounter{rowcount}{0}


\usepackage{polyglossia}
\usepackage{fontspec}


\setmainlanguage[variant=us]{english}
\setotherlanguage{hebrew}
\setotherlanguage[variant=ancient]{greek}

\newfontfamily\greekfont[Script=Greek, Scale=MatchUppercase, Ligatures=TeX]{SBL BibLit}
\newfontfamily\hebrewfont[Script=Hebrew,Contextuals=Alternate,Ligatures=Required]{SBL BibLit}

\begin{document}

\section*{Fragment I.1: Psalm 17:26b-(LXX)}

\begin{greek}
\begin{tabular}{@{\stepcounter{rowcount}\therowcount\hspace*{\tabcolsep}}ccccccr}

  & ουεμ                                & καί μ(ετ)α                        & πρὸς                            & καὶ μ(ετ)α    & καὶ μ(ετ)α\\
  & γαβρ                                & ἀ\d{ν}δρ\d{ὸ}\d{ς}           & ἀνδρ\d{ὸ}\d{ς}             & ἀνδρὸς        & ἀνδρὸς\\
  & θαμιμ                               & τ(\d{ε}\d{λ})ε\d{ι}ο\d{υ}  & ἀκ\d{έ}ραιον                 & ἀθώιου        & ἀμωμου\\
  & θεμα\d{μ}\d{μ}\d{α}\d{μ}  & τ(ελ)(\d{ε}\d{ι})ωθήσ\d{ι} & ἀκέραια \d{π}ρ\d{ά}ζεις  & ἀθώος ἔσηι  &  ἄμωμος (ἔσ)ηι\\
  & ουεμ                                & καὶ μ(ετ)α                        & πρὸς                            & καὶ μ(ετ)α    & μ(ετ)α & 27\\ %how do I make this last column bold? Current commands (textbf and bfseries) do not seem to work...
  & ναβαρ

\end{tabular}
\end{greek}

\end{document}

At the end of the day, I could always just manually input on a separate column \therowcount. I guess I'm just being curious.

Rob28
  • 378

2 Answers2

4

Nice question! Unfortunately my editor is struggling with the Greek characters, so I hope you do not mind me using ASCII characters in the example. Of course, the code does work in your use case, too. You can use \numexpr to define a mod function by exploiting that it rounds fractions.

\documentclass[letterpaper,12pt]{article}

\usepackage{array}
\newcounter{rowcount}
\setcounter{rowcount}{0}

\begin{document}
\begin{tabular}{@{\stepcounter{rowcount}%
\ifnum\the\numexpr\value{rowcount}-5*(\value{rowcount}/5)=0\relax
\number\value{rowcount}%
\fi
\hspace*{\tabcolsep}
}lc}
 & A \\
 & B \\
 & C \\
 & D \\
 & E \\
 & F \\
 & G \\
 & H \\
 & I \\
 & J \\
 & K \\
 & L \\
 & M \\
 & N \\
 & O \\
 & P \\
 & Q \\
 & R \\
 & S \\
 & T \\
 & U \\
 & V \\
 & W \\
 & X \\
 & Y \\
 & Z \\
\end{tabular}
\end{document}

enter image description here

One can of course use a macro and condense it but I wanted to keep the trick explicit. It is also obvious (I hope;-) how to generalize this to other integers, i.e. not 5. You can define this to be a new column type.

\documentclass[letterpaper,12pt]{article}
\usepackage{array}
\newcounter{rowcount}
\setcounter{rowcount}{0}
\newcolumntype{N}[1]{@{\stepcounter{rowcount}%
\ifnum\the\numexpr\value{rowcount}-#1*(\value{rowcount}/#1)=0\relax
\number\value{rowcount}%
\fi
\hspace*{\tabcolsep}
}l}
\begin{document}
\begin{tabular}{N{4}c}
 & A \\
 & B \\
 & C \\
 & D \\
 & E \\
 & F \\
 & G \\
 & H \\
 & I \\
 & J \\
 & K \\
 & L \\
 & M \\
 & N \\
 & O \\
 & P \\
 & Q \\
 & R \\
 & S \\
 & T \\
 & U \\
 & V \\
 & W \\
 & X \\
 & Y \\
 & Z \\
\end{tabular}
\end{document}
  • Thank you! This does the trick. I suppose the % signs are important. When I first input the code I ignored the percent signs and the output was off. Thank you for making the trick explicit, as I'm still pretty fresh to this world of LaTeX and I want to learn it well. – Rob28 Mar 10 '20 at 16:56
  • @Rob28 Yes, they are. They suppress spurious spaces, which can distort the output or have even more severe side-effects. –  Mar 10 '20 at 16:57
  • Can you use for loop? If I would have to write the answer (thinking on most programming languages) I would use for for sure, where the stop is the the number of rows, lol. Is that possible? Thank you! – manooooh Mar 10 '20 at 21:03
  • 1
    @manooooh Sure, you can. The point is that this works without any packages, just using \numexpr. So this answer is simple, fast, easy to modify and understand, and does not suffer from incompatibilities or other threats. –  Mar 10 '20 at 21:39
  • @Schrödinger's cat Can you explain the order of operations here: numexpr\value{rowcount}-5*(\value{rowcount}/5)=0? I think I understand the \ifnum syntax. Just not how we're getting to 0. – Rob28 Apr 12 '20 at 14:38
  • 1
    @Rob28 Sure. If rowcount is divisible by 5, \ifnum\the\numexpr\value{rowcount}-5*(\value{rowcount}/5) yields 0. What happens if rowcount is not divisible by 5? We need to know that \the\numexpr rounds the result. So 5*(\value{rowcount}/5) yields 0,0,5,5,5 for rowcount=1,2,3,4,5, and thus the whole term yields 0 only if rowcount is divisible by 5. –  Apr 12 '20 at 14:52
  • @Schrödinger's cat Am I to read \the\numexpr\value{rowcount}-5*(\value{rowcount}/5) as in value of rowcount minus 5 and then multiplied by (*) the value of the rowcount divided by 5? – Rob28 Apr 12 '20 at 16:10
  • @Rob28 Yes, this is correct. In a more mundane language this is x-5*round(x/5) where x=rowcount. See this comment by ShreevatsaR for some more of these tricks. –  Apr 12 '20 at 16:29
  • @ Schrödinger's cat Ok, these are my last 2 questions: (1) the asterisk after 5 (as in 5) indicates rounding or multiplication? (2) I'm assuming the value of "x" does not change when it's divided by 5, right? So, say x = 7 ----> "7-5(7/5)" – Rob28 Apr 12 '20 at 17:03
  • 1
    The star * is just a multiplication sign. And 7 gets mapped to 7-5*round(7/5)=7-5*1=2. You can play with \documentclass{article} \newcounter{rowcount} \begin{document} \loop \stepcounter{rowcount}% \number\value{rowcount}:\the\numexpr\value{rowcount}-5*(\value{rowcount}/5)\relax :\the\numexpr5*(\value{rowcount}/5)\relax\par \ifnum\value{rowcount}<30 \repeat \end{document} or similar codes to see things more explicitly. –  Apr 12 '20 at 17:13
  • @Schrödinger's cat Thank you! I appreciate the courtesy of answering all these questions. – Rob28 Apr 12 '20 at 17:17
2

Your idea is good. I defined a \countrows command that has as optional argument the interval of the numbering. Omitting it will number all lines.

I decided to set the numbers in the margin, with smaller type size, but this can be modified by changing the definition.

\documentclass[letterpaper,12pt]{article}
\usepackage{polyglossia}
\usepackage{fontspec}
\usepackage{array}

\setmainfont{Libertinus Serif}
\setmainlanguage[variant=us]{english}
\setotherlanguage[variant=ancient]{greek}

\ExplSyntaxOn
\int_new:N \g_robxxviii_rowcount_int

\NewDocumentCommand{\countrows}{O{1}}
 {
  \int_gincr:N \g_robxxviii_rowcount_int
  \int_compare:nT { \int_mod:nn { \g_robxxviii_rowcount_int } { #1 } = 0 }
   {
    \makebox[0pt][r]
     {
      \footnotesize
      \int_to_arabic:n { \g_robxxviii_rowcount_int }\hspace{\tabcolsep}
     }
   }
 }
\NewDocumentCommand{\resetcountrows}{}
 {
  \int_gzero:N \g_robxxviii_rowcount_int
 }
\ExplSyntaxOff

\begin{document}

\section*{Fragment I.1: Psalm 17:26b-(LXX)}

\begin{greek}
\noindent\resetcountrows
\begin{tabular}{
  @{\countrows[5]}
  ccccc
  >{\bfseries}r
  @{}
}
ουεμ     & καί μ(ετ)α     & πρὸς            & καὶ μ(ετ)α & καὶ μ(ετ)α\\
γαβρ     & ἀν̣δρὸ̣ς̣         & ἀνδρὸ̣ς̣          & ἀνδρὸς     & ἀνδρὸς\\
θαμιμ    & τ(ε̣λ̣)ει̣ου̣      & ἀκέ̣ραιον        & ἀθώιου     & ἀμωμου\\
θεμαμ̣μ̣α̣μ̣ & τ(ελ)(ε̣ι̣)ωθήσι̣ & ἀκέραια π̣ρά̣ζεις & ἀθώος ἔσηι &  ἄμωμος (ἔσ)ηι\\
ουεμ     & καὶ μ(ετ)α     & πρὸς            & καὶ μ(ετ)α & μ(ετ)α & 27\\ 
ναβαρ \\
ουεμ     & καί μ(ετ)α     & πρὸς            & καὶ μ(ετ)α & καὶ μ(ετ)α\\
γαβρ     & ἀν̣δρὸ̣ς̣         & ἀνδρὸ̣ς̣          & ἀνδρὸς     & ἀνδρὸς\\
θαμιμ    & τ(ε̣λ̣)ει̣ου̣      & ἀκέ̣ραιον        & ἀθώιου     & ἀμωμου\\
θεμαμ̣μ̣α̣μ̣ & τ(ελ)(ε̣ι̣)ωθήσι̣ & ἀκέραια π̣ρά̣ζεις & ἀθώος ἔσηι &  ἄμωμος (ἔσ)ηι\\
ουεμ     & καὶ μ(ετ)α     & πρὸς            & καὶ μ(ετ)α & μ(ετ)α & 27\\ 
ναβαρ \\
ουεμ     & καί μ(ετ)α     & πρὸς            & καὶ μ(ετ)α & καὶ μ(ετ)α\\
γαβρ     & ἀν̣δρὸ̣ς̣         & ἀνδρὸ̣ς̣          & ἀνδρὸς     & ἀνδρὸς\\
θαμιμ    & τ(ε̣λ̣)ει̣ου̣      & ἀκέ̣ραιον        & ἀθώιου     & ἀμωμου\\
θεμαμ̣μ̣α̣μ̣ & τ(ελ)(ε̣ι̣)ωθήσι̣ & ἀκέραια π̣ρά̣ζεις & ἀθώος ἔσηι &  ἄμωμος (ἔσ)ηι\\
ουεμ     & καὶ μ(ετ)α     & πρὸς            & καὶ μ(ετ)α & μ(ετ)α & 27\\ 
ναβαρ \\
ουεμ     & καί μ(ετ)α     & πρὸς            & καὶ μ(ετ)α & καὶ μ(ετ)α\\
γαβρ     & ἀν̣δρὸ̣ς̣         & ἀνδρὸ̣ς̣          & ἀνδρὸς     & ἀνδρὸς\\
θαμιμ    & τ(ε̣λ̣)ει̣ου̣      & ἀκέ̣ραιον        & ἀθώιου     & ἀμωμου\\
θεμαμ̣μ̣α̣μ̣ & τ(ελ)(ε̣ι̣)ωθήσι̣ & ἀκέραια π̣ρά̣ζεις & ἀθώος ἔσηι &  ἄμωμος (ἔσ)ηι\\
ουεμ     & καὶ μ(ετ)α     & πρὸς            & καὶ μ(ετ)α & μ(ετ)α & 27\\ 
ναβαρ \\
\end{tabular}
\end{greek}

\end{document}

enter image description here

egreg
  • 1,121,712