1

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/

Andrew Swann
  • 95,762
  • 1
    Yes, this is because your redefinitions are local. –  Nov 19 '18 at 14:45
  • 1
    each cell of a table is a group, &test\renewcommand{\myvalue}{1}\\ is like {test\renewcommand{\myvalue}{1}} and the value will not be seen outside that scope. – David Carlisle Nov 19 '18 at 14:45
  • and how can I change that, to suit my intended behaviour? – Peter Miehle Nov 19 '18 at 14:47
  • 1
    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. –  Nov 19 '18 at 16:46

1 Answers1

1

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. – Peter Miehle Nov 19 '18 at 14:52
  • So my questtion: if it is not a counter, but plain text (like "EUR") what can I use instead of setcounter? – Peter Miehle Nov 19 '18 at 14:53
  • @PeterMiehle I added something in which I make the definitions global... –  Nov 19 '18 at 14:56
  • what I try to achieve is an invoice, that can span many pages, and I want to put a overflow ("Übertrag EUR 570,00") at the bottom of the page and the start of the next page. The table rows are genereated from an outside source. – Peter Miehle Nov 19 '18 at 15:14
  • 1
    @marmot you can abbreviate \global\def to \gdef. –  Nov 19 '18 at 16:43
  • 1
    @jfbu I know, thanks! My rationale here, however, was to make explicit what the g stands for. –  Nov 19 '18 at 16:45
  • I did suspect you knew :)... we can delete comments if you prefer. I added one to OP. –  Nov 19 '18 at 16:47
  • @jfbu I appreciate your comment. Just wanted to explain why I am not changing this now unless the OP wants me to. And I also agree with your above comment, and I still don't get the purpose of all this. Why redefining a macro for something that just gets used once? –  Nov 19 '18 at 16:49
  • 1
    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. –  Nov 19 '18 at 16:59