1

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 x1 and x1new
  • or I need to run over the definition for \x1=1 later 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} 
4
  • Define your variable inside a group Commented Dec 18, 2022 at 11:28
  • Right. Do you want to write it as an answer? Commented Dec 18, 2022 at 11:42
  • No, there is no code in your question so I would have to come up with a minimal working example first ... too lazy for that Commented Dec 18, 2022 at 11:49
  • @samcarter_is_at_topanswers.xyz Added Commented Dec 18, 2022 at 12:03

1 Answer 1

1

If you make your definition inside a group around your tikzpicture, it will only define the variable inside this group.

You can explicitly add a group by e.g. using {...} or you can place your definition inside the tikpicture, which automatically forms a group.

\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: { \tikzmath{\x1 = 1;} \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} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.