
My problem to be solved is: I want to set and reset a value (like a summation value) per line in my longtable (whose content is generated from the outside of the document).

\documentclass[12pt,twoside]{article}
\newcommand{\myvalue}{initialise}
\usepackage{longtable}
\begin{document}
\begin{longtable}{ll}
\myvalue&test\renewcommand{\myvalue}{1}\\
test &test\renewcommand{\myvalue}{2}\\
test &test\renewcommand{\myvalue}{3}\\
test&test\renewcommand{\myvalue}{4}\\\pagebreak
\myvalue&test\renewcommand{\myvalue}{5}\\
test&test\renewcommand{\myvalue}{6}\\\pagebreak
\myvalue&test\renewcommand{\myvalue}{7}\\
test &test\renewcommand{\myvalue}{8}
\end{longtable}
This was my longtable example for \myvalue
\end{document}


What I expect is, that at the beginning of each page, there is a new \myvalue ("initialise", "4", "6") and after the table "8".

What I get is only "initialise", \myvalue is never changed.

https://v1.overleaf.com/17449534ktcscvntyfhy#/66313130/

• Yes, this is because your redefinitions are local.
– user121799
Nov 19, 2018 at 14:45
• and how can I change that, to suit my intended behaviour? Nov 19, 2018 at 14:47
• You would have the problem with \renewcommand inside any LaTeX environment. Tables add the compilcation that each table cell is a scope group, but the issue would be there in more friendly environments too. (as you want to use data outside the environment). LaTeX provides no interface to work around that problem. You must resort to \global\def or \gdef as in @marmot answer, or, if possible, use auxiliary files which is quite more complicated.
– user4686
Nov 19, 2018 at 16:46

Try this: use a counter, which is automatically global, and seems to be more appropriate for your purpose.

\documentclass[12pt,twoside]{article}
\newcounter{myvalue}
\usepackage{longtable}
\begin{document}
\setcounter{myvalue}{0}
\begin{longtable}{ll}
\themyvalue&test\stepcounter{myvalue}\\
test &test\stepcounter{myvalue}\\
test &test\stepcounter{myvalue}\\
test&test\stepcounter{myvalue}\\\pagebreak
\themyvalue&test\stepcounter{myvalue}\\
test&test\stepcounter{myvalue}\\\pagebreak
\themyvalue&test\stepcounter{myvalue}\\
test &test\stepcounter{myvalue}
\end{longtable}
This was my longtable example for \themyvalue
\end{document}


As for your comment, you could do

\documentclass[12pt,twoside]{article}
\newcommand{\myvalue}{initialise}
\usepackage{longtable}
\begin{document}
\begin{longtable}{ll}
\myvalue&test\global\def\myvalue{1}\\
test &test\global\def\myvalue{2}\\
test &test\global\def\myvalue{3}\\
test&test\global\def\myvalue{4}\\\pagebreak
\myvalue&test\global\def\myvalue{5}\\
test&test\global\def\myvalue{6}\\\pagebreak
\myvalue&test\global\def\myvalue{7}\\
test &test\global\def\myvalue{8}
\end{longtable}
This was my longtable example for \myvalue
\end{document}


If you tell us what you are ultimately after, I guess there is a more elegant way of achieving what you really want.

• urgs, the "myvalue" can be everything "like EUR 20,56", the stepped counter was just to make the question simple. Nov 19, 2018 at 14:52
• So my questtion: if it is not a counter, but plain text (like "EUR") what can I use instead of setcounter? Nov 19, 2018 at 14:53
• @marmot you can abbreviate \global\def to \gdef.
– user4686
Nov 19, 2018 at 16:43
• @jfbu I know, thanks! My rationale here, however, was to make explicit what the g stands for.
– user121799
Nov 19, 2018 at 16:45
• The OP wants to display it at pagebreaks, the (cumulative) values will be there. Primitive calcsheet mechanism, and your answer shows the simplest way. Probably some \global\edef or \global\let rather after the table cell has done updating of the \myvalue value.
– user4686
Nov 19, 2018 at 16:59