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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I increment variable from the same variable?

\pgfmathsetmacro\S{5};
\pgfmathsetmacro\S{\S + 1};%   not working

How can I work around this? I need counters which I use as line coordinates increment in certain conditions.

Update

\pgfmathsetmacro\cA{0}; 
\newcounter{cB}
\foreach \x in {1,...,10}
{ 
    \pgfmathtruncatemacro\cA{\cA+1)};
    \pgfmathaddtocounter{cB}{1};            
    \node at (\x,1) { \cA };
    \node at (\x,0) { \the\numexpr\value{cB} };         
}

printed out this

1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1

I need

1 2 3 4 ...

Yes, I could do this in this simple example by just using \x variable but in my real diagram I need to increment them irregularly. So I need variable which can be incremented inside the loop without resets. Or, am I missing something and it should work?

share|improve this question
up vote 4 down vote accepted

To be used in a \foreach loop, there are better options:

\documentclass[tikz,border=2pt]{standalone}
\begin{document}

\begin{tikzpicture}
\foreach \i [count=\S from 5] in {1,...,5}
    \node [draw, xshift=\i cm] {\S};
\end{tikzpicture}

\end{document}

enter image description here

where the syntax count=\S from 5 is used here to set \S to 5 and advance it by 1 in each iteration. Another syntax may be evaluate=\i as \S using \i+4, which will achieve the same result.

Update

The increment can be changed within the loop based on a condition like this:

\newcounter{cA} 
\setcounter{cA}{0}
\newcounter{cB}
\setcounter{cB}{0}

\begin{tikzpicture}
\foreach \x in {1,...,10}{ 
    \addtocounter{cA}{1}
    \ifnum\x<6\addtocounter{cB}{1}\else\addtocounter{cB}{2}\fi            
    \node at (\x,1) { \thecA };
    \node at (\x,0) { \thecB };          
}
\end{tikzpicture}

enter image description here

share|improve this answer
    
ok, thx.. but value is reset when foreach loop increments, any workaround for that? – Martin877 Mar 26 at 0:53
    
@Martin877 - The \pgfmathsetcounter should be placed before the loop so as not to be reset. – AboAmmar Mar 26 at 0:56
    
i got that idea..i created counter and first set of pgfmacros before loop but that doesn't help, still resets when i inc them inside loop. I need to increment numerous times these variables in loop otherwise its pointless. Is there any counter which doesnt get reset, besides foreach variables? – Martin877 Mar 26 at 1:21
    
@Martin877 - What do you mean by besides foreach variables? In my update, \S is not the loop counter and it can start from any value and can also be incremented/decremented by any value. What exactly are your requirements? – AboAmmar Mar 26 at 1:34
    
edited question – Martin877 Mar 26 at 1:47

No need to use pgf math for counters. You can just use \setcounter, \stepcounter or \addtocounter. Using these, the counter value is retained after the \foreach loop:

enter image description here

Not sure I fully understand the intent code snippet given, but can easy be adapted to use TeX counters as well (as shown in the second MWE below):

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\newcounter{foo}

\begin{document}
    \setcounter{foo}{0}
    After \verb|\setcounter|: foo=\arabic{foo}

    \stepcounter{foo}
    After \verb|\stepcounter|: foo=\arabic{foo}

    \addtocounter{foo}{4}
    After \verb|\addtocounter|: foo=\arabic{foo}

    \foreach \x in {1,...,20} {%
        \stepcounter{foo}%
    }%
    After \verb|\foreach|: foo=\arabic{foo}
\end{document}

Code:

\documentclass{article}
\usepackage{tikz}

\newcounter{cA}
\newcounter{cB}

\begin{document}
\begin{tikzpicture}
    \foreach \x in {1,...,10} {%
        \stepcounter{cA};
        \stepcounter{cB};            
        \node at (\x,1) { \the\value{cA} };
        \node at (\x,0) { \the\value{cB} };         
    }        
\end{tikzpicture}
\end{document}
share|improve this answer

It works with pgf 3.0.1a:

\documentclass{article}
\usepackage{pgf}
\pgfmathsetmacro\S{5}
\pgfmathsetmacro\S{\S + 1}

\begin{document}
\S
\end{document}

Result

Remarks:

  • \pgfmathsetmacro is not a path command, therefore its syntax does not know a closing semicolon. In the preamble the additional semicolon causes an error (Missing \begin{document}).

  • If you want to have an integer as result, then \pgfmathtruncatemacro helps:

    \documentclass{article}
    \usepackage{pgf}
    \pgfmathsetmacro\S{5}
    \pgfmathtruncatemacro\S{\S + 1}
    
    \begin{document}
    \S
    \end{document}
    

    Result with \pgfmathtruncatemacro

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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