1

Background: In my previous question, Robert Fuster taught me how to define a global variable using calculator package.


I want to define a "counter" that also works with non-integers. I thus defined an \addtocounter-like macro as following (assuming that \mySum and \whatIAdd have been previously defined):

\ADD{\whatIAdd}{\mySum}{\mySum}
\GLOBALCOPY{\mySum}{\mySum}

My macro works well in normal text mode (see below). However, if I use it inside a table, it "mysteriously" does each addition once before plotting the table. Even if I reset it at the beginning, \mySum equals the sum of each \whatIAdd in my table.

Question: where does this artefact comes from, and how to remove it?


MWE

\documentclass{scrartcl}
    \usepackage{calculator}
    \usepackage{array,tabulary,booktabs}

    %% Macro definition %%
    \newenvironment{mytable}
    {
        \GLOBALCOPY{0}{\mySum}
        \texttt{mySum} equal \mySum

        \tabulary{\linewidth}{CC}
        \toprule
        \textbf{A}&\textbf{$\Sigma$ A}\\
        \midrule
    }{
        \endtabulary
    }


    \newcommand{\makeline}[1]
    {%
        \COPY{#1}{\whatIAdd}
        \ADD{\whatIAdd}{\mySum}{\mySum}
        \GLOBALCOPY{\mySum}{\mySum}
        %
        \whatIAdd & \mySum\\
    }

    \newcommand{\resetMySum}{\GLOBALCOPY{0}{\mySum}\noindent I've reseted \texttt{mySum}. It's value is now \mySum .\newline}
    \newcommand{\addMySum}[1]
    {
        \COPY{#1}{\whatIAdd}
        \ADD{\whatIAdd}{\mySum}{\mySum}
        \GLOBALCOPY{\mySum}{\mySum}
        %
        \noindent I added \whatIAdd{} to \texttt{mySum}. It's value is now \mySum .\newline
    }

\begin{document}
    \begin{minipage}{.25\textwidth}
        \begin{mytable}
            \makeline{0}
            \makeline{1}
            \makeline{42}
            \bottomrule
        \end{mytable}
    \end{minipage}
    \begin{minipage}{.75\textwidth}
        \resetMySum
        \addMySum{0}
        \addMySum{1}
        \addMySum{42}
    \end{minipage}
\end{document}

1 Answer 1

2

This is a side effect of the tabulary package.

In this package, tables are evaluated twice, to ensure right widths in columns. In your example, sums are increased by 43 (the last value ontained in the first evaluation!).

Modify your code to ensure that \mySum is zero when the second calculation occurs:

\begin{document}
    \begin{minipage}{.25\textwidth}
       \begin{mytable}
           \makeline{0}
           \makeline{1}
           \makeline{42}
           \bottomrule
        \GLOBALCOPY{0}{\mySum} %%%% Reset \mySum to zero
       \end{mytable}
   \end{minipage}
 \end{document}
3
  • Thank you! N.B.: Since they are pretty similar, the same artifact is observed using tabularx (and of course, same solution applies). Commented Apr 15, 2016 at 10:10
  • Do you know why it is not working if you add the \GLOBALCOPY{0}{\mySum} into the end-block definition of the mytable environment? ({ \GLOBALCOPY{0}{\mySum} \endtabulary }) Commented Apr 15, 2016 at 10:34
  • No. I do not know how tabulary really works. Commented Apr 15, 2016 at 10:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.