6

I am trying to learn TikZ by reading the manual (p.34). I know my understanding must be completely off the rails, since nothing is working as I think I should; but I'm not sure what I misunderstood.

What I understood from the manual is this: let's say that you want to draw many circles of the form

\draw[red, very thick, radius = 1] (0, 0) circle;

Instead of writing red, very thick, radius = 1, you could pack all of this into just one word, via TikZ style, like so

myCircle/.style = {red, very thick, radius = 1};

and now you are able to draw circles faster, since you can just write

\documentclass{book}
\usepackage{tikz}
\usepackage{parskip}

\begin{document}

\begin{tikzpicture}

    `myCircle/.style = {red, very thick, radius = 1};`
    \draw[myCircle] (0, 0) circle;
    \draw[myCircle] (1, 2) circle;
    \draw[myCircle] (3, 4) circle;

\end{tikzpicture}

\end{document}

However, this does not work; it does not even compile.

This is perhaps even more confusing (for me): in the manual, they do something similar with help lines, which apparently is a predefined thing (a style?) in the TikZ package. Now the following does compile, but it does not work to make the grid lines blue and very thick.

\documentclass{book}
\usepackage{tikz}
\usepackage{parskip}
\begin{document}

\begin{tikzpicture}

     help lines/. style = {blue, very thick}
     \draw[help lines] (-4, -4) grid (4, 4);

\end{tikzpicture}

\end{document}

Could you please help me see where my understanding is wrong? Thanks.

  • 2
    Your style definition should be in a \tikzset command. – Ulrike Fischer Jul 13 at 14:21
  • @UlrikeFischer And that goes outside of the TikZ picture environment, right? But then what is the point of the style definition inside the TikZ environment? Is it only used to modify styles that were already defined outside of the TikZ environment? – Ovi Jul 13 at 14:23
  • @UlrikeFischer I tried putting the line \tikzset {myCircle/. style = blue, very thick, radius = 1} before my TikZ environment and then I put \draw[myCircle] (-4, -4) circle; in my TikZ environment, but it did not work. – Ovi Jul 13 at 14:27
  • 3
    As @UlrikeFischer is saying, it should be a style definition. You only need to add [...] around them: \begin{tikzpicture}[myCircle/.style = {red, very thick, radius = 1}] and \begin{tikzpicture}[help lines/. style = {blue, very thick}]. The examples in the pgfmanual do have these brackets. – user121799 Jul 13 at 14:38
8

You have a couple (4?) of problems.

  1. if you want to define the style for one picture, you need to put it into the optional argument of the picture, in square brackets [ ];
  2. The style is terminated by , (or the end of the argument), not ;
  3. In a tikzset, the style has the scope determined by the normal TeX rules.
  4. You can't have a space between the / and the .style handler.

This works:

\documentclass{book}
\usepackage{tikz}
\usepackage{parskip}

\begin{document}

\begin{tikzpicture}[
    myCircle/.style = {red, very thick, radius = 1}, % end with a comma, NOT semicolon!
    ]

    \draw[myCircle] (0, 0) circle;
    \draw[myCircle] (1, 2) circle;
    \draw[myCircle] (3, 4) circle;
\end{tikzpicture}

\tikzset{help lines/.style = {blue, very thick}} %% no spaces between / and .style!

\begin{tikzpicture}
     \draw[help lines] (-4, -4) grid (4, 4);
\end{tikzpicture}

\end{document}

enter image description here

  • Thanks for the example! I wasn't able to fix it just based on the comments alone. – Ovi Jul 13 at 14:46
  • 5
    It is often easier to show than explain. – John Kormylo Jul 13 at 14:58

Your Answer

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

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