Is there a way to use a counters value at one point in the code, but with the counters state of a line, that comes somewhat after the corresponding use of the counters value?

E.g. Define the counter in line 10, use it in line 15 with \value{cnt} and keep increasing the value for three lines one after another. So in line 18, the counters value holds 3. I want to use this value (3) in line 15! So \value{cnt} in line 15 should return 3.

Below find my MWE:

% MWE

\documentclass{scrbook}
\usepackage{lmodern}        
\usepackage[T1]{fontenc}    
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}  
\usepackage{tabularx}
\usepackage{multirow}

\begin{document}
    \newcounter{linecnt}


    \begin{table}
        \begin{tabular}[tbh]{p{4.2cm} p{0.68\textwidth}}
            \textbf{Field}      & \textbf{Entrys} \\ 
            \hline
        % This works fine:
            \multirow{4 %------------------------------------------------------
            }{4cm}{This Field}                  & Entry 1         \\    
            & Entry 2                                       \\
            & Entry 3                                       \\
            & Entry 4                                       \\[2ex]



        % But it would be great if a didn't have to write the amount of lines everytime.
        % Is there a way to use a counters value AFTER it's 'use' in the code. Somethining like:

        \multirow{\value{linecnt}   %------------------------------------------------------
        }{4cm}{Another Field}                   & Automatic Entry 1 \stepcounter{linecnt} \\    
        &  Automatic Entry 2                    \stepcounter{linecnt} \\
        &    Automatic Entry 3                  \stepcounter{linecnt} \\
        &    Automatic Entry 4                  \stepcounter{linecnt} \\

        % I would like to use the value of 'linecnt' NOW but in the beginning of the multirow-environment 

        \end{tabular}
    \end{table}


\end{document}

I appreciate your help.

Kind regards.

  • you can do this with a label-ref system, e.g. with zref. It then needs two compilations. – Ulrike Fischer Oct 17 at 11:28

By the time you know the final number of the counter, the \multirow is already typeset, so you can't do much about it.

The usual approach in these cases is a two-pass compilation, writing the value of the counter to the .aux file.

First, in the preamble, we define an initial value for the counter:

\def\savedthelinecnt{0}

then, when we know the final value of the counter, we write it to the .aux file, so in the next compilation, at the \begin{document} this counter is updated to the correct value:

\immediate\protected@write\@auxout{}
  {\gdef\protect\savedthelinecnt{\thelinecnt}}

then after two passes the value is correct:

enter image description here

\documentclass{scrbook}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{tabularx}
\usepackage{multirow}

\makeatletter
\def\savedthelinecnt{0}
\makeatother

\begin{document}

\newcounter{linecnt}

\begin{table}[tbh]
  \begin{tabular}{p{4.2cm} p{0.68\textwidth}}
    \textbf{Field} & \textbf{Entries} \\
    \hline
    \multirow{4}{4cm}{This Field}
      & Entry 1 \\
      & Entry 2 \\
      & Entry 3 \\
      & Entry 4 \\[2ex]
  \multirow{\savedthelinecnt}{4cm}{Another Field \savedthelinecnt}
      & Automatic Entry 1 \stepcounter{linecnt} \\
      & Automatic Entry 2 \stepcounter{linecnt} \\
      & Automatic Entry 3 \stepcounter{linecnt} \\
      & Automatic Entry 4 \stepcounter{linecnt} \\
  \end{tabular}
  \makeatletter
  \immediate\protected@write\@auxout{}
    {\gdef\protect\savedthelinecnt{\thelinecnt}}
\end{table}

\end{document}

By the way, the [tbh] optional argument you passed to tabular is (kind of) invalid. If you meant the float positions top, bottom and here, then you need to pass the arguments to \begin{table}. If you meant the vertical position of the baseline of the table, then the valid options are t for top, b for bottom, and c (anything else, actually) for centered. But only one character is passed here.

  • You're completely right about the tbh ;) I just came up with this MWE really quickly and didn't care a lot about anything other than the multirow-environment – Michael Schmid Oct 17 at 11:48

Use refcount; note \refstepcounter instead of \stepcounter. Watch out for spaces: Automatic Entry 1 \refstepcounter{linecnt} would have a trailing space.

Note that [tbh] makes no sense for tabular or tabularx. The table environment takes a positioning argument.

\documentclass{scrbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{lmodern}

\usepackage{multirow}
\usepackage{refcount}

\newcounter{linecnt}

\begin{document}

\begin{table}[htp]

\begin{tabular}{p{4.2cm} p{0.68\textwidth}}
  \textbf{Field}      & \textbf{Entries} \\ 
  \hline
  % This works fine:
  \multirow{4}{4cm}{This Field}
  & Entry 1 \\
  & Entry 2 \\
  & Entry 3 \\
  & Entry 4 \\[2ex]
  \multirow{\getrefnumber{step1}}{4cm}{Another Field}
  & Automatic Entry 1\refstepcounter{linecnt} \\    
  & Automatic Entry 2\refstepcounter{linecnt} \\
  & Automatic Entry 3\refstepcounter{linecnt} \\
  & Automatic Entry 4\refstepcounter{linecnt}\label{step1} \\
\end{tabular}

\end{table}


\end{document}

enter image description here

On the other hand, the following table is more readable:

\documentclass{scrbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{lmodern}

\usepackage{tabularx}
\usepackage{booktabs}

\begin{document}

\begin{table}

\begin{tabularx}{\textwidth}{lX}
\toprule
  \textbf{Field}      & \textbf{Entries} \\ 
\midrule
  This Field
  & Entry 1 \\
  & Entry 2 \\
  & Entry 3 \\
  & Entry 4 \\
\midrule
  Another Field
  & Automatic Entry 1 \\
  & Automatic Entry 2 \\
  & Automatic Entry 3 \\
  & Automatic Entry 4 \\
\bottomrule
\end{tabularx}

\end{table}

\end{document}

There is no need to center the field name, which may actually become ambiguous.

enter image description here

You can do this in one pass.

Depending on your use case, the code may need some additional twiddling, but it works fine here (main limitation is assuming the material ends exactly with a \\ or \\<space>).

\documentclass{scrbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{lmodern}

\usepackage{multirow}

\makeatletter
\newcommand\myautorow[3]{%
  \expandafter\multirow\expandafter
  {\the\numexpr0\myautorow@getnboflines#3\\}{#1}{#2}#3%
}

\def\myautorow@getnboflines #1\\%
% the romannumeral is to swallow a space if present
   {\if\relax\romannumeral-`0\detokenize{#1}\relax\expandafter\relax\else
    +1\expandafter\myautorow@getnboflines \fi}

\makeatother

\begin{document}

\begin{table}[htp]

\begin{tabular}{p{4.2cm} p{0.68\textwidth}}
  \textbf{Field}      & \textbf{Entries} \\ 
  \hline
  % This works fine:
  \multirow{4}{4cm}{This Field}
  & Entry 1 \\
  & Entry 2 \\
  & Entry 3 \\
  & Entry 4 \\[2ex]
  \myautorow{4cm}{Another Field}{%
  & Automatic Entry 1 \\    
  & Automatic Entry 2 \\
  & Automatic Entry 3 \\
  & Automatic Entry 4 \\}\hline
  \myautorow{4cm}{Another Field}{%
  & Automatic Entry 1 \\    
  & Automatic Entry 2 \\
  & Automatic Entry 3 \\
  & Automatic Entry 4 \\
  & Automatic Entry 5 \\    
  & Automatic Entry 6 \\
  & Automatic Entry 7 \\
}
\end{tabular}

\end{table}    
\end{document}

enter image description here

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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