# Increment/Decrement variables in TikZ

I need to increment/decrement variables in TikZ, i.e., I have

\def\a{0}

and then I need to perform

\a=\a-1.5;

The code compiles, but it does not change the value of the variable \a.

How do I do this?

• In which context are you trying to do this? Do you need a loop, for example? Please elaborate a little more. Jun 17, 2013 at 20:32
• @GonzaloMedina No. I do not need a loop. I am making a drawing in TikZ and the variable stores the coordinates of the center of a piece of the drawing and relative to this, I need to draw the rest.
– user1876
Jun 17, 2013 at 20:34
• Simply having \a=\a-1.5; in a TikZ picture doesn’t do anything. (Such text is usually simply discarded as TikZ installs some sort of null font as it simply expands to 0=0-1.5;.) If you want to use the result of that calculation inside of TikZ you mostly can simply say \edef\a{\a-1.5} as TikZ evaluates nearly everything again anyway. Otherwise you need to do \pgfmathsetmacro\a{\a-1.5}. Jun 17, 2013 at 20:47
• @Qrrbrbirlbel Thanks! \edef is the one I was looking for! Could you add that as an answer? I will accept it.
– user1876
Jun 17, 2013 at 21:59
• I think you are doing something inefficient. If you include your use case then it will be much easier to show how to use relative coordinates. Jun 18, 2013 at 11:01

There's not much to go on in your question. So, I'll do my best here. There are several approaches you can take. You wanted to decrement \a; I increment it in the following examples to visually emphasis how the effects are taking place and being used: just make the necessary changes for decrementing.

The first is to combine a use of \pgfmathparse with \edef

code for above image:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

Use \verb\pgfmathparse together with \verb\edef

\begin{tikzpicture}
\edef\mya{0}
\pgfmathparse{\mya+1.5}
\edef\mya{\pgfmathresult}
\node[circle,draw] at (\mya,\mya) {\mya};
\end{tikzpicture}

\end{document}


If you want to remember the changed macro outside of the tikzpicture then you can use \xdef

code for above image:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

To remember outside of the picture use \verb\xdef

\begin{tikzpicture}
\edef\mya{0}
\pgfmathparse{\mya+1.5}
\xdef\mya{\pgfmathresult}
\node[circle,draw] at (\mya,\mya) {\mya};
\end{tikzpicture}

\verb\mya=\mya

\end{document}


You can use a \foreach loop with the above tricks:

code for above image:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

If used within a \verb\foreach loop, you have several choices:

Use \verb\xdef

\begin{tikzpicture}
\edef\mya{0}
\foreach \x in {1,2,...,8}
{
\pgfmathparse{\mya+1.5}
\xdef\mya{\pgfmathresult}
\node[circle,draw,inner sep=\mya pt] at (\mya*0.50cm,\mya*0.25cm) {\makebox[0pt]{\mya}};
}
\end{tikzpicture}
\end{document}


Better yet, you can use evaluate=\x as ...

code for above image:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

Using \verbevaluate=\x as ...

\begin{tikzpicture}
\edef\mya{0}
\foreach \x [evaluate=\x as \mya using \x*1.5] in {1,2,...,8}
{
\node[circle,draw,inner sep=\mya pt] at (\mya*0.5cm,\mya*0.25cm) {\makebox[0pt]{\mya}};
}
\end{tikzpicture}

\end{document}


Finally, if you need to remember the value outside of the \foreach loop, you can do something like:

code for above image:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

Using \verbevaluate=\x as ... and remembering

\begin{tikzpicture}
\edef\mya{0}
\foreach \x [evaluate=\x as \mya using \x*1.5] in {1,2,...,8}
{
\node[circle,draw,inner sep=\mya pt] at (\mya*0.5cm,\mya*0.25cm) {\makebox[0pt]{\mya}};
\xdef\remembermya{\mya}
}
\edef\mya{\remembermya}
\node at (0,0) {\mya};
\end{tikzpicture}

\end{document}


This should probably be enough to get you started. If you could post a more thorough MWE illustrating how you're trying to use \a, then I could probably give you a more complete answer.

You could also take a rather different approach using coordinate arithmetic:

code for above image:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

Using \verbcalc package to perform coordinate arithmetic: \verb($(0,0)+\x*(<coordinate>)$)

\begin{tikzpicture}
\coordinate (myinc) at (1.5*0.5cm,0.25cm);
\foreach \x  in {1,2,...,8}
{
\pgfmathparse{\x*1.5}
\edef\mya{\pgfmathresult}
\node[circle,draw,inner sep=\mya pt] at ($(0,0)+\x*(myinc)$) {\makebox[0pt]{\mya}};
}
\end{tikzpicture}

\end{document}

• Where do I can look for his to use \def, \xdef, and \edef? They all seem LaTeX macros used for something else in TikZ. Feb 17 at 7:00
• @Atcold I'm unclear about what you're asking. But there are a number of posts on this site about how to use these commands. See for example: What are the differences between \def, \edef, \gdef and \xdef? and the link to the duplicate. Feb 17 at 21:24