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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityThere'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 \verb`evaluate=\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 \verb`evaluate=\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 \verb`calc` 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}
\def
, \xdef
, and \edef
? They all seem LaTeX macros used for something else in TikZ.
\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 to0=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}
.\edef
is the one I was looking for! Could you add that as an answer? I will accept it.