3

I don't use tikz very often. When I do, I'm usually pretty happy with the result (and with how useful that manual is). But I always think the code I produced to get there could be optimized, as in the end it's full of redundancies resulting from my somewhat clumsy copy&paste approach. This is the one thing I haven't yet managed to find in the manual, with the exception of \pgfsetarrows, which was a step forward.

What follows is a good example. If I now decided to change a parameter that affects an entire group of elements, a lot of search&replacing would be needed. Consider e.g. the font and color settings for the text elements, or the anchor settings. What ways are there to remove these redundancies, like by defining styles ›glocally‹ for a certain group of elements? -- One way is of course to save, say, my \bfseries\color{hks15k} in a macro of its own, and simply calling that instead. But first I'd like to find out if tikz provides more official ways of doing these things.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz,sourcesanspro,textcomp}
\definecolor{hks89k}{cmyk}{0,.1,.3,.35}%
\definecolor{hks15k}{cmyk}{0.1,1,.8,0}

\begin{document}
\begin{figure}\footnotesize\sffamily
\begin{tikzpicture}[x=\textwidth,y=5\baselineskip, text height=.75em, text depth=.15em]
\useasboundingbox (0,-0.65) rectangle (1,1.65);

\draw (0,1)
node[anchor=south west] {\color{hks89k}15\rlap{\textrightarrow}}
node[anchor=north west] {IV\rlap{\enskip\textbf{Klöden 1824}}}
rectangle (11/46,0.65)

+(0.25em,0.35)
node[anchor=south west] {\color{hks89k}26\rlap{\textrightarrow}}
node[anchor=north west] {III}
rectangle (0.5\textwidth-0.25em,0.65)

+(0.25em,0.35)
node[anchor=south west] {\color{hks89k}38\rlap{\textrightarrow}}
node[anchor=north west] {II}
rectangle (34/46,0.65)

+(0.25em,0.35)
node[anchor=south west] {\color{hks89k}50\rlap{\textrightarrow}}
node[anchor=north west] {I}
rectangle (46/46,0.65);


\draw (0,0)
node[anchor=north west] {\color{hks89k}11x\rlap{\textrightarrow}}
node[anchor=south west] {IV\rlap{\enskip\textbf{Köhler 1856}}}
rectangle (.5\textwidth-0.25em,0.35)

+(0.25em,-0.35)
node[anchor=north west] {\color{hks89k}7x\rlap{\textrightarrow}}
node[anchor=south west] {III}
rectangle (10/16,0.35)

+(0.25em,-0.35)
node[anchor=north west] {\color{hks89k}6x\rlap{\textrightarrow}}
node[anchor=south west] {IIb}
rectangle (12/16,0.35)

+(0.25em,-0.35)
node[anchor=north west] {\color{hks89k}5x\rlap{\textrightarrow}}
node[anchor=south west] {IIa}
rectangle (14/16,0.35)

+(0.25em,-0.35)
node[anchor=north west] {\color{hks89k}4x\rlap{\textrightarrow}}
node[anchor=south west] {Ib}
rectangle (15/16,0.35)

+(0.25em,-0.35)
node[anchor=north west] {\color{hks89k}3,5x\rlap{\textrightarrow}}
node[anchor=south west] {Ia}
rectangle (16/16,0.35);


\draw (0,0.5) node {0\%\rlap{\quad \color{hks89k}schlechter}}
(20/100,0.5) node {20\%}
(40/100,0.5) node {40\%}
(60/100,0.5) node {60\%}
(80/100,0.5) node {80\%}
(100/100,0.5) node {\llap{\color{hks89k}besser\quad}100\%};

\pgfsetarrows{-latex}    
\draw (20/100,0.61) .. controls +(2/200,0.19) and +(3/200,-0.23) .. +(up:0.8) node[anchor=south] {\bfseries\color{hks15k}IV};
\draw (40/100,0.61) .. controls +(1/200,0.38) and +(2/200,-0.28).. +(up:0.8) node[anchor=south] {\bfseries\color{hks15k}III};
\draw (60/100,0.61) .. controls +(4/200,0.4) and +(1.5/200,-0.2) .. +(up:0.8) node[anchor=south] {\bfseries\color{hks15k}II};
\draw (80/100,0.61) .. controls +(3/200,0.30) and +(1/200,-0.1) .. +(up:0.8) node[anchor=south] {\bfseries\color{hks15k}I};

\draw (20/100,0.39) .. controls +(-1.2/200,-0.32) and +(-3.5/200,0.23) .. +(down:0.8) node[anchor=north] {\bfseries\color{hks15k}IV};
\draw (40/100,0.39) .. controls +(-2.4/200,-0.3) and +(-1.5/200,0.2) .. +(down:0.8) node[anchor=north] {\bfseries\color{hks15k}IV};
\draw (60/100,0.39) .. controls +(-1.5/200,-0.24) and +(-1/200,0.21) .. +(down:0.8) node[anchor=north] {\bfseries\color{hks15k}III};
\draw (80/100,0.39) .. controls +(1/200,-0.23) and +(2/200,0.42) .. +(down:0.8) node[anchor=north] {\bfseries\color{hks15k}IIa};
\end{tikzpicture}
\end{figure}
\end{document}
  • 2
    Define macros and style to reduce the number of characters. – kiss my armpit Mar 29 '13 at 10:43
  • That's one way, for sure. But reducing characters isn't really what I'm after. If I were allowed to phrase my question less specifically, I would probably ask »is there a more elegant way to the same result than the way I've taken?«. For that's what it boils down to, basically. – Nils L Mar 29 '13 at 15:11
  • 2
    You need to use styles. Instead of \pgfsetarrows you could use \tikzset{-latex, every node/.append style={font=\bfseries\color{hks15k}}} and you would only need to have I and so on in the nodes. You should also use named nodes, then you can reference them with their names and do not need to input their border coordinates manually. – Qrrbrbirlbel Mar 29 '13 at 15:48
  • You can also use \foreach for the lines after \pgfsetarrows{-latex} – hpesoj626 Mar 29 '13 at 23:35