0

I want something like this: a referrable automatic counter and sub counter for each row in a tabularx environment. I could find a solution for a single counter here, but could not adapt it to tabularx. So I tried implementing it directly using two counters. How do I reference the MainNumber counter and the SubNumber, without using the dirty solution in the last line?

\documentclass{article}
\usepackage{array} 

\usepackage{tabularx}

\newcounter{MainNumber}
\newcounter{SubNumber}[MainNumber]

\begin{document}
\begin{table}
  \begin{tabularx}{\textwidth}{lX}
  \refstepcounter{MainNumber} \label{firstMain} \theMainNumber & This is first main. \\
  \refstepcounter{SubNumber} \label{1firstSub} \theMainNumber.\theSubNumber & This is first sub \\
  \refstepcounter{SubNumber} \label{1secondSub} \theMainNumber.\theSubNumber & This is second sub\\
  \refstepcounter{MainNumber} \label{secondMain} \theMainNumber & This is second main. \\   
  \refstepcounter{SubNumber} \label{2firstSub} \theMainNumber.\theSubNumber & This is first sub \\
  \refstepcounter{SubNumber} \label{2secondSub} \theMainNumber.\theSubNumber & This is second sub\\ 
\end{tabularx}
\end{table}

second main first sub:~\ref{2firstSub}

shoud be: 2.1

dirty: \ref{secondMain}.\ref{1firstSub}

\end{document}
1
  • This could be useful: \renewcommand{\theSubNumber}{\theMainNumber.\arabic{SubNumber}} so you don't need to type \theMainNumber.\theSubNumber.
    – Sigur
    Feb 18, 2019 at 14:26

2 Answers 2

2

Like this? The idea is to redefine the \theSubNumber (which contains the counter value) to have the format you want: <main>.<sub>.

So, you only type \theSubNumber instead of \theMainNumber.\theSubNumber.

enter image description here

\documentclass{article}
\usepackage{array} 

\usepackage{tabularx}

\newcounter{MainNumber}
\newcounter{SubNumber}[MainNumber]
\renewcommand{\theSubNumber}{\theMainNumber.\arabic{SubNumber}} % <-- added here
\begin{document}
\begin{table}
  \begin{tabularx}{\textwidth}{lX}
  \refstepcounter{MainNumber} \label{firstMain}  \theMainNumber & This is first main. \\
  \refstepcounter{SubNumber}  \label{1firstSub}  \theSubNumber  & This is first sub \\
  \refstepcounter{SubNumber}  \label{1secondSub} \theSubNumber  & This is second sub\\
  \refstepcounter{MainNumber} \label{secondMain} \theMainNumber & This is second main. \\   
  \refstepcounter{SubNumber}  \label{2firstSub}  \theSubNumber  & This is first sub \\
  \refstepcounter{SubNumber}  \label{2secondSub} \theSubNumber  & This is second sub\\ 
\end{tabularx}
\end{table}

second main first sub:~\ref{2firstSub}


\end{document}
1

enter image description here

Another form of automatic numbering, by defining a new columntype, say, 'b' and letting the stepping and the display be done by the b type itself.

The code determines whether it should step the main counter or the sub counter by the boolean 'variable' \ifstepmain etc.

Since the code can't know when a new main step occurs (unless some more conditions are speficied) the switch to the main stepping must be done with \prepnextmainstep.

The usage of tabularx complicates the issue a little bit since tabularx tries to 'typeset' the tabular content multiple times, each time \global etc. settings have to be made.

\documentclass{article}
\usepackage{array} 

\usepackage{tabularx}

\newcounter{MainNumber}
\newcounter{SubNumber}[MainNumber]

\counterwithin{SubNumber}{MainNumber}



\newif\ifstepmain
\newcolumntype{b}{>{\ifstepmain\refstepcounter{MainNumber}\theMainNumber\global\stepmainfalse\else\refstepcounter{SubNumber}\theSubNumber\fi}l}

\newcommand{\prepnextmainstep}{%
  \global\stepmaintrue%
}

\stepmaintrue
\begin{document}
\begin{table}
  \begin{tabularx}{\textwidth}{bX}
    \label{firstMain}   & This is first main. \tabularnewline
    \label{1firstSub}   & This is first sub \tabularnewline
    \label{1secondSub}  & This is second sub \prepnextmainstep\tabularnewline  
    \label{secondMain}  & This is second main.\tabularnewline   
    \label{2firstSub}   & This is first sub \tabularnewline
    \label{2secondSub}  & This is second sub \prepnextmainstep \tabularnewline 
    \label{thirddMain}  & This is third main.\tabularnewline   
    \label{3firstSub}   & This is first sub \tabularnewline
    \label{3secondSub}  & This is second sub \prepnextmainstep \tabularnewline 
\end{tabularx}
\end{table}

second main first sub:~\ref{2firstSub} and \ref{3secondSub} 

shoud be: 2.1 and 3.2
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .