Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

I created a counter named foo to add up so money which I want to get from my boss. When having an itemize environment the counter is reset accidently. Does anyone know why or how to solve problem (so tell my what I am doing wrong ;-) )? I added comment lines to clear up what I want to do. Everytime I add a value of money, the sum is created.

\documentclass{scrbook}
%\usepackage{eurosym}
%\newcommand{\eu}[1]{\advance\foo by #1 \EUR{#1}}
\begin{document}
Create Counter: \newcount\foo

Give counter: \the\foo

Increase by three: \advance\foo by 3

Give result: \the\foo

\begin{itemize}
    \item Increase by seven: \advance\foo by 7
    %\item \eu{7}
    \item Give result: \the\foo
\end{itemize}

Give result: \the\foo
\end{document}  

The PDF looks like:

Create Counter:

Give counter: 0

Increase by three:

Give result: 3

• Increase by seven:

• Give result: 10

Give result: 3

I use texniccenter with Miktex 2.9 on a Win7 64 machine.

share|improve this question

marked as duplicate by yo', Papiro, Jesse, Kurt, Martin Schröder Aug 19 at 10:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
tex.stackexchange.com/questions/35084/… might help here: you are using a 'raw' TeX count register rather than the LaTeX wrapper as a counter, and you therefore have to allow for grouping. –  Joseph Wright Aug 19 at 7:50
1  
Any environment is a single group. So everything you are doing inside this group is local. Using some values outside a group you have to work with \global. Using expl3 gives you some more options. –  Marco Daniel Aug 19 at 7:52

1 Answer 1

These are TeX-core counters, which work locally. You would have to use \global\advance. However, I suggest using LaTeX counters, which are global by default:

\documentclass{scrbook}

\begin{document}
Create Counter: \newcounter{foo}

Give counter: \arabic{foo}

Increase by three: \addtocounter{foo}{3}

Give result: \arabic{foo}

\begin{itemize}
    \item Increase by seven: \addtocounter{foo}{3}
    \item Give result: \arabic{foo}
\end{itemize}

Give result: \arabic{foo}
\end{document} 
share|improve this answer
    
Thank you very much. Before asking this question I tried this way, because I read it in another topic. But obviously I did another mistake. –  karl.ranseier Aug 19 at 7:55

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