How to Increment and Decrement Variables in TikZ
At a glance
- 1. Choose the variable type
Use PGF math macros for calculated values, or TeX count registers for persistent integer counters.
- 2. Use arithmetic expressions
Write expressions such as int(x+1), int(x-1), or x+0.25 inside PGF math commands.
- 3. Handle foreach grouping
Use evaluate for per-iteration values, because each foreach iteration has its own group.
- 4. Distinguish relative coordinates
The ++ coordinate syntax moves a path relative to its current position; it does not increment a named variable.
TikZ does not have a single universal increment operator for every kind of variable. The right method depends on what you are incrementing: a calculated macro, an integer counter, a value in a \foreach loop, or the current position along a path.
For most calculations, use PGF math commands. For a value that must be maintained as mutable state across loop iterations, use a TeX count register for integers; when each iteration’s value can instead be derived from the current loop variable, use evaluate. Also note that TikZ’s ++ syntax has a different purpose: it creates a coordinate relative to the previous point.
Incrementing a calculated variable
Use \pgfmathsetmacro when the result may be fractional:
\pgfmathsetmacro{\x}{2.5}
\pgfmathsetmacro{\x}{\x + 0.25}
After these commands, \x expands to 2.75. The expression on the right is evaluated before the new value is assigned, so using the same macro on both sides is valid.
For integer results, use \pgfmathtruncatemacro:
\pgfmathtruncatemacro{\n}{3}
\pgfmathtruncatemacro{\n}{\n + 1}
The value of \n is now 4. The command truncates the result to an integer, so it is preferable when the variable represents a counter or an index.
You can decrement in exactly the same way:
\pgfmathtruncatemacro{\n}{\n - 1}
\pgfmathsetmacro{\y}{\y - 0.1}
The macro-name argument is conventionally enclosed in braces in these PGF commands; because it is a single-token argument, the braces are not strictly required. They are not the same as writing \n+1 in ordinary TeX text, where no arithmetic would automatically occur.
Calculating the next value in \foreach
For a \foreach loop, the evaluate option is usually the cleanest solution. It computes a new variable from the current loop variable:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x [evaluate=\x as \next using int(\x+1)] in {0,...,4} {
\node at (\x,0) {current: \x};
\node at (\x,-0.6) {next: \next};
}
\end{tikzpicture}
\end{document}
For each iteration, \next is one greater than \x. To calculate a previous or decremented value, change the expression:
\foreach \x [evaluate=\x as \previous using int(\x-1)] in {1,...,5} {
\node at (\x,0) {\x};
\node at (\x,-0.6) {previous: \previous};
}
The int(...) function makes the result an integer. For decimal arithmetic, omit int, for example:
\foreach \x [evaluate=\x as \y using \x+0.25] in {0,1,2,3} {
\fill (\x,\y) circle (1.5pt);
}
The loop list itself can also generate a sequence directly. Thus, if all you need is the values from 0 through 4, this is enough:
\foreach \x in {0,...,4} {
\node at (\x,0) {\x};
}
Using a persistent integer counter
A TeX count register is useful when you genuinely need to update one integer repeatedly. The \global prefix matters inside \foreach, because each loop body is processed in a group:
\documentclass{standalone}
\usepackage{tikz}
\newcount\step
\begin{document}
\begin{tikzpicture}
\step=0
\foreach \unused in {1,...,5} {
\global\advance\step by 1
\node at (\step,0) {step \the\step};
}
\end{tikzpicture}
\end{document}
The command \advance\step by 1 adds one to the count register. The decrement form is:
\global\advance\step by -1
Use \the\step when you want to print or insert the numeric value. A count register stores integers only; use \pgfmathsetmacro for decimal values.
In many TikZ drawings, evaluate is preferable to a global counter because it avoids changing state outside the current iteration.
Understanding ++ in coordinates
TikZ uses ++ for relative coordinates. It does not increment a named variable. In this path, each coordinate is measured from the previous point:
\begin{tikzpicture}
\draw (0,0) -- ++(2,0) -- ++(0,1) -- ++(-2,0);
\end{tikzpicture}
The path starts at (0,0), moves two units right, then one unit up, then two units left. The final point is (0,1).
A single + also specifies a relative coordinate, but it does not update TikZ’s reference point for later relative coordinates. For ordinary sequential path construction, ++ is the form that both moves and updates the current position.
Common mistakes
Expecting \x++ to work
TikZ and PGF math do not generally use C-style \x++ or ++\x syntax for macro arithmetic. Write an explicit expression instead:
\pgfmathtruncatemacro{\x}{\x+1}
Or use evaluate inside a loop.
Forgetting integer conversion
\pgfmathsetmacro can produce formatted decimal results. If the value is intended to be an integer index, use an already initialized scratch macro such as \idx:
\pgfmathtruncatemacro{\idx}{\idx+1}
Alternatively, use int(\idx+1) in an evaluate expression.
Updating a macro inside \foreach and expecting persistence
Assignments in a loop body can be local to that iteration. This pattern should not be your default for a running counter:
\foreach \unused in {1,...,5} {
\pgfmathtruncatemacro{\n}{\n+1}
}
Use evaluate to derive values from the loop variable, or use a count register with a global increment when a persistent state is required.
Confusing coordinates with arithmetic
++(1,0) moves a path by one unit horizontally. It does not change a macro named \x, and it cannot replace \pgfmathsetmacro when you are calculating labels, indices, or other values.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.