Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
dublicate in solution and usage, please see: tex.stackexchange.com/questions/41721/… –  zeroth Jan 30 '12 at 15:14
2  
@zeroth Are you sure? –  egreg Jan 30 '12 at 15:30
    
Thanks @zeroth, TikZ styles seem to be a powerful mechanism to define predefined sets of options. However, I am having a hard time understanding the answer you provided in that thread (in particular why bar seems to be copying the style from foo in \tikzset in the very first code snippet in your answer). I have limited experience with TikZ, but I am eager to experiment with \tikzset and see how it applies to my problem. –  user815423426 Jan 30 '12 at 15:30
    
@egreg , I can see why you questioned that. And yes, if you are not experienced in TikZ it is hard to draw the references. Sorry about that. –  zeroth Jan 30 '12 at 15:52
1  
@intrpc, if no answer is given in the next 20 minutes I will create one! (It is in that time I will eat dinner, and if somebody else is cooking an answer I will not create a new) –  zeroth Jan 30 '12 at 15:54

1 Answer 1

up vote 38 down vote accepted

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}
share|improve this answer
5  
This is a FANTASTIC explanation and tutorial of TikZ styles and scopes. I would vote you up 100 times if I could. –  user815423426 Jan 30 '12 at 20:47
2  
Very nice! One could add that you can keep styles local by applying them to \begin{scope}[<styles>] ... \end{scope} (or, if you loaded \usetikzlibrary{scopes}, just { [<styles] ... }). –  Jake Apr 20 '12 at 7:52
    
@Jake, yes I felt that I had to stop somewhere. And the fact that I talk in scopes should point to the working idea of \begin{scope}. :) I will wait for that question to pop up :) –  zeroth Apr 20 '12 at 8:19
    
@zeroth In the last piece of code, the \tikzset{line width=2mm} does not seem to work for me. Using ` \tikzset{every path/.style={line width=2mm}}` instead overrides the 10mm of the first line to 2mm, so that's not the alternative I'm looking for. –  Adriaan Nov 20 '13 at 16:33
    
@Adriaan, it seems like the line width gets set on entrance to the tikzpicture. However, the principle of the scoping of styles remain the same. line width seems to be a bad choice by me. Thanks for the update. –  zeroth Nov 21 '13 at 9:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.