1

I'm trying to add automatic row numbers and dotfills to a table, with a toggle that disables them for certain rows, however it doesn't work as expected. In the MWE below, the toggle is working for row numbers, but not for the fill. How do I fix this?

In other words, why does the toggle appear to prematurely revert to true?

example

MWE:

\documentclass{memoir}

\usepackage{etoolbox}



\AtBeginEnvironment{tabularx}{%
    \ifcscounter{c@rownumbers}{}{\newcounter{rownumbers}}%
    \setcounter{rownumbers}{0}%

    \providetoggle{showrownumber}%
    \toggletrue{showrownumber}%

    \newcommand\ifshowrownumber[1]{%
        \iftoggle{showrownumber}{#1}{}%
    }
    \newcommand\nonum{%
        \togglefalse{showrownumber}%
    }
    \newcommand\rownumberdisplay{%
        \makebox[1.5em][r]{\ifshowrownumber{%
            \stepcounter{rownumbers}\arabic{rownumbers}.%
        }}\space%
    }
    \newcommand\rowfill{%
        \ifshowrownumber{\dotfill}%
    }
    \newcommand\resetrownumber{%
        \setcounter{rownumbers}{0}%
    }
    \newcommand\rowdone{%
        \toggletrue{showrownumber}%
    }
}



\begin{document}

\begin{tabularx}{.9\textwidth}{r<{\rownumberdisplay} @{} X<{\rowfill} @{} r r<{\rowdone}}%
\toprule%
    \nonum & Name & A & B\\
\midrule%
    \nonum & \emph{Section} \\
    & Name & 100.0 & 3.5 \\
    \nonum & -- and Name & 10.15 & 56.8 \\
    \nonum & \emph{Section} \\
    & Name & 66.4 & 43.1 \\
    & Name & 66.4 & 43.1 \\
    & Name & 66.4 & 43.1 \\
    & Name & 66.4 & 43.1 \\
    & Name & 66.4 & 43.1 \\
    & Name & 66.4 & 43.1 \\
    & Name & 66.4 & 43.1 \\
    & Name & 66.4 & 43.1 \\
\bottomrule%
\end{tabularx}

\end{document}
1

Like this?

enter image description here

It is not automatic. As you manually interrupt numbering of rows, you can do the same for \dotfill:

\documentclass{memoir}
\newcounter{rownum}
\newcommand{\getEqNo}{\refstepcounter{rownum}
                      \therownum.\,}
\newcommand\mcl[1]{\multicolumn{1}{l}{#1}}

\begin{document}
\begin{tabularx}{.9\textwidth}{>{\getEqNo}r @{} X<{\dotfill} @{} r r}
    \toprule
\mcl{}  & \mcl{Name}            & A     & B     \\
    \midrule
\mcl{}  & \mcl{\emph{Section}}  &       &       \\
        & Name                  & 100.0 &  3.5  \\
\mcl{}  & \mcl{-- and Name}     & 10.15 & 56.8  \\
\mcl{}  & \mcl{\emph{Section}}  &       &       \\
        & Name                  & 66.4  & 43.1  \\
        & Name                  & 66.4  & 43.1  \\
        & Name                  & 66.4  & 43.1  \\
        & Name                  & 66.4  & 43.1  \\
        & Name                  & 66.4  & 43.1  \\
        & Name                  & 66.4  & 43.1  \\
        & Name                  & 66.4  & 43.1  \\
        & Name                  & 66.4  & 43.1  \\
    \bottomrule
    \end{tabularx}
\end{document}

| improve this answer | |
  • Looks pretty good, I will play around with this and report back :) – meide Jun 4 '19 at 9:40
  • It works, but I'm not a fan of having to put \mcl with the names. Any idea why my toggle seems to become true prematurely? – meide Jun 4 '19 at 12:38
  • you not have anything "dead", you only locally changes column type. as you already do in your mwe. – Zarko Jun 4 '19 at 12:40
  • Sorry, meant having to use mcl twice... – meide Jun 4 '19 at 12:42
  • @meide, well, than you should wait on better solution. However, meantime you can use this simple and robust one. Do you have many such tables? if it is only few, is not worth to spend time for far more complex solution. But, as you wish :-) – Zarko Jun 4 '19 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.