2

I'd like to understand how to properly use variables in TikZ. I've read a large number of Q&A's and, so far, I've encountered \{g,x,e,}def, \newcommand, \pgfmathsetmacro, and \let.

The objective is to parameterise a drawing without repercussions to the rest of the document.

Additionally, the \foreach and [evaluate = <> as <>…] commands seem also to be creating (local? global?) variables.

6
  • I think that you should propose a minimal example of use. Your current question can bring to an XY problem .
    – projetmbc
    Feb 17 at 7:56
  • Related Question: What is the preferred way of defining a TikZ constant?. Even thought that question is specfically about constants, same applies to variables. Feb 17 at 7:57
  • Everything defined in the tikzpicture should be local to it, unless you explicitly make it \global in scope. ie: don't use \gdef, \xdef. then it won't affect the rest of the code. As can be seen from \newcommand{\Foo}{7} Before:\Foo \begin{tikzpicture} \def\Foo{11}% \end{tikzpicture} After:\Foo. The output before and after the tikzpicture is 7 even thought it was defined to be 11 in the tikzpicture. If you want to define \Foo before and use it in the tikzpicture, then don't redefine it within the tikzpicture -- sounds obvious, but I was once bitten by this bug. Feb 17 at 8:01
  • Thank you @PeterGrill, you indeed nailed my question. I didn't think to search for “constant” and indeed it was what I was after. I understand how \def, \newcommand, \pgfmathsetmacro, and \let work. Can you point me in the direction for learning about the {g,x,e} variants of \def? I'll compile a small summary as a self-answer to my question and ask for review / feedback. Moreover, any idea about the local / global behaviour of \foreach and [evaluate macros?
    – Atcold
    Feb 17 at 15:51
  • Using an edef (\def with expansion) is still local. The \gdef is the equivalent to \global\def and \xdef is equivalent to \global\edef. More details on those can be found at What are the differences between \def, \edef, \gdef and \xdef? and When to use \edef, \noexpand, and \expandafter?. The \foreach loop keep everything local. Feb 17 at 17:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.