I want to "define" a variable inside my tikzpicture. This answer gives a solution for how to do that, using the math library.
The thing I am concerned with is that when I assign \x1 the value of 1, let's say, this value is defined globally across my latex document.
So if I want to draw two rather similar graphics, which for both it is comfortable to use the name x1 to describe a specific length, then I need to either
- come up with a new name for this length, such as
x1andx1new - or I need to run over the definition for
\x1=1later in the document with\x1=2.
My question is whether there is a way to define a variable and assign it a value just for the current tikzpicture environment?
A MWE:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{math} \tikzmath{\x1 = 2; \y1=1;} %%%% <<----- First definition \begin{document} In this lecture we will talk about complex numbers. The complex number $2+\mathrm{i}$ can be pictured in the following way: \begin{tikzpicture} \draw[step=.5cm,gray,very thin] (-2.4,-2.4) grid (2.4,2.4); \draw [-latex] (-2.5,0) -- (2.5,0); \draw [-latex] (0,-2.5) -- (0,2.5); \draw [thick, - latex] (0,0) -- (\x1,\y1); \end{tikzpicture} An example for a vector with a negative argument is the following: \begin{tikzpicture} \tikzmath{\y1=-1;} %%%% <<----- run over the previous value of y1 \draw[step=.5cm,gray,very thin] (-2.4,-2.4) grid (2.4,2.4); \draw [-latex] (-2.5,0) -- (2.5,0); \draw [-latex] (0,-2.5) -- (0,2.5); \draw [thick, - latex] (0,0) -- (\x1,\y1); \end{tikzpicture} \end{document}