2017-10-23 Styles in TikZ

Many TeX users learned to love the great TikZ package. I used to be a great fan of Metapost (even back in the good ol’ days of DOS), but when a friend told me about TikZ, I checked it out and instantly fell in love. No more external compilation, no more strange syntax, no more problems with labels etc. Yes, MP has its advantages, like the declarative way of solving linear equations or the Hobby algorithm for finding the nicest curve fitting to a set of points. And yes, with \write18 or LuaTeX (or just plain old makefiles), you don’t have to run a separate program to compile your MP diagrams. But I made my switch, and most of the time I’m really glad I did.

There are a few things, however, which are a bit uncool with TikZ. One of them is that I find it notoriously difficult to define commands to draw repetitive things in my diagrams. (Yes, I know that I can define pics and nodes. The former have limited use, since it appears that they cannot be parametrized, and the latter are a pain to define – been there, done that, believe me it’s true.)

Some time ago, I’ve been giving a lecture on calculus. As I mentioned a few times, after a short affair with reveal.js (through Org-mode) I settled for Beamer. The thing is, I wanted to have nice pictures. And lots of them. So I finally sat down to figure out how to apply the DRY principle to TikZ pictures.

For starters, lots of my pictures involved a (two-dimensional) coordinate system. I decided to write a command to draw it. It’s not very TikZ-ish, but it works quite nice, and it accepts a bunch of key-value style parameters to customize my coordinate axes.

Of course, if one wants to have key-value syntax in LaTeX, one thing immediately comes to mind: the pgfkeys package. However, its documentation, while extensive, does not give an example of how to achieve what I wanted. And that was basically: have some keys, with some default values (note: this is a bit different from what pgfkeys calls a default value of a key; here, the defaults should be used not when the key is present without a value, but when the key is completely absent), which should then be easy to use within some TeX code.

Happily, we have TeX.SE. This answer by Ryan Reich gives a very detailed, complete example. I highly recommend it if you want to use pgfkeys for customizing some LaTeX commands (not necessarily in connection with TikZ). And here’s my code for the coordinate system:

\usepackage{etoolbox}
\pgfkeys{/mbork/.is family,/mbork,
  default/.style={
    xmin=-0.5,
    ymin=-0.5,
    xmax=3,
    ymax=3,
    xlabel={$x$},
    ylabel={$y$},
    zerolabel={$0$},
  },
  xmin/.store in=\mborkxmin,
  ymin/.store in=\mborkymin,
  xmax/.store in=\mborkxmax,
  ymax/.store in=\mborkymax,
  xlabel/.store in=\mborkxlabel,
  ylabel/.store in=\mborkylabel,
  zerolabel/.store in=\mborkzerolabel,
}

\newcommand{\coordsystem}[1][]{%
  \pgfkeys{/mbork,default,#1}%
  \draw[->] (\mborkxmin,0) -- (\mborkxmax,0);
  \draw[->] (0,\mborkymin) -- (0,\mborkymax);
  \ifblank{\mborkxlabel}{}{
    \node[below] at (\mborkxmax,0) {\mborkxlabel};
  }
  \ifblank{\mborkylabel}{}{
    \node[left] at (0,\mborkymax) {\mborkylabel};
  }
  \ifblank{\mborkzerolabel}{}{
    \node[below left] (0,0) {\mborkzerolabel};
  }
}

Notice that I made use of etoolbox’s \ifblank test here.

Then, I wanted to have a consistent look of my function graphs, so I wrote a small TikZ style I used whenever I was drawing a curve. It turned out that it was both easy and effective, so I’ll be definitely using this technique more and more in the future.

The style can be defined in the following way:

\tikzset{graph/.style={very thick,darkgray}}

and used like this:

\draw[graph] (0,0) rectangle (1,1);

As you can see, it’s really nothing magical. (Note that there is another way of defining a style, namely the \tikzstyle command, but it is deprecated.) Also, there is .append style, which adds given options at the end of an existing style (see the manual for more details).

Enjoy!

CategoryEnglish, CategoryBlog, CategoryTeX, CategoryLaTeX