[Tex/LaTex] Global variables for all tikz drawings in a document

tikz-pgf

Sorry if this question sounds too elementary. I am new to TikZ, although I have experience with LaTeX.

Say I have multiple TikZ statements in a LaTeX document like the following:

\tikz\draw[line width=2mm] (0,0.1) -- (4,0.1);\hfill\textsc{\Large This is a block}%
 \hfill \tikz\draw[line width=2mm] (0,0.1) -- (4,0.1);

I would like to define variables (e.g. line_thickness and line_width:

line_thickness = 2mm
line_width = 4

that I could use in all such statements so that, if I want to change the parameters of my drawings in my LaTeX document, I could just change the value of my variables where I define them.

Is it possible to do this in TikZ? I am interested in a relatively general solution, e.g. some of these variables may specify color, length, relative coordinates, etc. although a solution to addresses my particular problem above would already help a lot.

I have read the solutions in these threads:

but I don't know how to apply the answers to my problem.

Best Answer

Variables, or more correctly styles or keys, in pgf is a very powerful feature introduced to aid the creation of the drawing framework. And here I say pgf as that is in fact the underlying mechanism which TikZ uses so much. Notice that \tikzset is equivalent to \pgfkeys{/tikz/.cd,#1}.

If you want a quick answer, goto Scopes of keys/styles.

In my answer many will see things also mentioned in the manual. This is intentionally.

I will not go over the tree structure, neither the very powerful features introduced by the handlers, nor will I go over .code as here it is .styles that govern the user interface. I will not distinguish between .styles and .code as it is not aiding the basic explanation of the key structure.

Styles: shortcuts for code execution

This is the most often used feature when a picture/path is created. Every time a path is added thick, color=red or line width=2mm.

A .style is in effect a shortcut or an abbreviation for single/multiple .code keys. This means that .style really doesn't do anything, it is the reference herein that does the job. Notice that a .style could equally refer to any other .style.

Styles are therefore often used when you need to shorten, or do multiple things to a path.

An example:

\tikzset{% 
    my box color red/.style={%
        draw,color=red!50!white,fill=black!20!red!50!white
    }
}

Now look at the following two paths. They are in fact completely identical.

\path[my box color red] (0,0) -- (1,0);
\path[draw,color=red!50!white,fill=black!20!red!50!white] (0,.1) -- (1,.1);

Notice that the fact that my box color red refers to other styles, i.e. draw, color=red!50!white and fill=black!20!red!50!white.

We then later realised that my box color <color> has to be used several places with several different colors.
One solution could be to generate .styles for each color that will be used, however, that seams redundant.

Every style is made so that it will take one optional argument.

This means that instead we can define the .style as:

\tikzset{% 
    my box color/.style={%
        draw,color=#1!50!white,fill=black!20!#1!50!white
    }
}

Now look at the following two and two paths. They are in fact completely identical, two and two respectively.

\path[my box color=red] (0,0) -- (1,0);
\path[draw,color=red!50!white,fill=black!20!red!50!white] (0,.1) -- (1,.1);

\path[my box color=blue] (0,0) -- (1,0);
\path[draw,color=blue!50!white,fill=black!20!blue!50!white] (0,.1) -- (1,.1);

We created my box color because it was way easier to typeset this and because I have to use it many times.

Styles and their defaults

The above style my box color will not compile if used as:

\path[my box color] (0,0) -- (1,0);

and the reason is that the style color has a mandatory argument. This you will learn by mistake, I did. Therefore the above code will never compile.

Often, however, you want default behaviours if no argument is specified.

Take for instance this style which adds an arrow to a path. It takes one argument which is the fractional position of the arrow on the path.

\tikzset{%
    ->-/.style={postaction={decorate},decoration={%
            markings,mark=at position #1 with {\arrow{stealth}}%
        }%
    }%
}

This seams obvious that it should always place the arrow at the 0.5 position on the path. But it doesn't unless you specify ->-=0.5 everytime you need to use it.

This is accompanied by the use of the .default specification on the style.

So:

\tikzset{%
    ->-/.default=.5%
}

ensures that if you do not specify the fractional position on the path it will take it as if you specified ->-=0.5.

\begin{tikzpicture}
  % Will have arrow at 0.5
  \draw[->-] (0,0) -- (1,0);
  % Will have arrow at 0.3
  \draw[->-=0.3] (0,.1) -- (1,.1);
\end{tikzpicture}

Lifetime of used styles

Sometimes you will be in the situation of defining several styles with the same reference to another style.

Example:

\tikzset{%
    my box/.style={thin,color=red},
    my thick box/.style={thick,color=blue}
}

\begin{tikzpicture}
  % line 1
  \draw[my box] (0,0) -- (1,0);
  % line 2
  \draw[my thick box] (0,.1) -- (1,.1);
  % line 3
  \draw[my thick box,my box] (0,.2) -- (1,.2);
  % line 4
  \draw[my box,my thick box] (0,.3) -- (1,.3);
  % line 5
  \draw[my box,my thick box,thin] (0,.3) -- (1,.3);
\end{tikzpicture}

In the above picture you see 5 lines. The first two are pretty obvious to have their respective colors of red and blue. But the last two?

This can be answered by knowing that the keys are read and executed consecutively. The lines will thus have the following properties:

  1. Red and thin
  2. Blue and thick
  3. Red and thin
  4. Blue and thick
  5. Blue and thin

Thus you do not need to worry about the keys overlapping, just know that it is the last seen key which takes precedence on the others.

Scopes of keys/styles

The scope, as your question basically refers to, is very important in TikZ.

You should expect that keys/styles have the same scope as normal TeX groups, i.e. they live until the end of the current group.

Globally

So if you in the preamble, or elsewhere not in a nested group, set:

\tikzset{%
    line width=2mm
}

this will be a globally set variable. It will take effect on all drawings and shapes where the length in line width is used.

Picture Globally

This refers to styles which only lives inside in a current picture.

\begin{tikzpicture}[line width=10mm] % Picture Globally
  % will have width of 10mm
  \draw (0,0) -- (1,0);
\end{tikzpicture}

Picture locally

A declaration of a style that will only take on the current path.

\begin{tikzpicture}
  % will have width of 7mm
  \draw[line width=7mm] (0,0) -- (1,0);
\end{tikzpicture}

Collected

An example of how they are defined at different locations in the document:

\tikzset{%
    line width=2mm
}

\begin{tikzpicture}[line width=10mm] % overwrites the above
  % will have width of 10mm
  \draw (0,0) -- (1,0);
  % will have width of 3mm, will override the above
  \draw[line width=3mm] (0,0) -- (1,0);
\end{tikzpicture}

\begin{tikzpicture}
  % will have width of 2mm, due to the globally defined style.
  \draw (0,0) -- (1,0);
  % will have width of 7mm, overrides the global set style.
  \draw[line width=7mm] (0,0) -- (1,0);
\end{tikzpicture}
Related Question