# Using one key to set another key (in pgfkeys)

When I make a TikZ picture, I sometimes use absolute coordinates, and when I do, I find Martin Scharrer's showgrid TikZ library very handy. (It lays "graph paper" underneath my canvas, and puts numbers along the edges.)

Here's a bit of code to illustrate that. (Note that the showgrid library has to be downloaded from Martin's website.)

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{showgrid} % http://latex.scharrer-online.de/general/wiki/showgrid

\begin{document}

\begin{tikzpicture}[show grid=all]
\draw (3,2) -- +(45:3);
\end{tikzpicture}

\end{document}


What I would like is to have a custom environment that takes a draw guides option. When draw guides is set, various debugging aids will be drawn (such as the names of nodes), and show grid=all will be activated.

The code below shows my attempt to do this. It doesn't work, and I think the problem might be to do with the fact that the show grid key resides in the /tikz/ directory, but draw guides resides in the /wickerson/ directory. I've tried to cd into that directory at the opportune time, but it doesn't seem to have helped.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{showgrid} % http://latex.scharrer-online.de/general/wiki/showgrid

\pgfkeys{/wickerson/.cd,
draw guides/.code = {%
% here do some other stuff, like enabling
% the printing of node names
\pgfkeys{/tikz/.cd,show grid=all}}
}

\newenvironment{wickersonpicture}[1][]{%
\pgfkeys{/wickerson/.cd,#1}
% do some stuff
\begin{tikzpicture}
}{%
\end{tikzpicture}
}

\begin{document}

\begin{wickersonpicture}[draw guides]
\draw (3,2) -- +(45:3);
\end{wickersonpicture}

\end{document}

• I believe that the show grid=all key must be set for the tikzpicture: If you change your \pgfkeys{/tikz/.cd,show grid=all} line to \tikzset{every picture/.append style={show grid=all}}, it works as desired. (Note that \tikzset{...} is equivalent to \pgfkeys{/tikz/.cd,...}). Martin himself might be able to provide a more qualified answer, though. – Jake May 28 '13 at 16:39
• @Jake Aha! Right, so my problem, in vague terms, was that I wasn't "attaching the key to the tikzpicture". Well, that seems to have fixed it. Do feel free to make an answer if you so wish. – John Wickerson May 28 '13 at 16:42
• I believe the simplest trick is to change a bit the environement: \newenvironment{wickersonpicture}[1][]{% \begin{tikzpicture}[/wickerson/.cd,#1] }{% \end{tikzpicture} }  should work. Of course supposing that you have a given set of keys under your personal path and the user is not allowed to enter keys from other paths (but with since there's also a custom environment the supposition should be valid). – Claudio Fiandrino May 28 '13 at 16:42
• @ClaudioFiandrino Ah yes. The problem with that is that, in my real code, I pass lots of keys to my wickersonpicture environment. It would only make sense to pass the draw guides key to the tikzpicture environment. – John Wickerson May 28 '13 at 16:46
• @JohnWickerson: Is you wickersonpicture environment only a wrapper for the tikzpicture environment, or does other stuff happen in \begin{wickersonpicture}? – Jake May 28 '13 at 16:58

## 1 Answer

As has been recently discussed in one of your previous questions, it is not that straightforward to go up in the family trees of pgfkeys hence we need to be extra careful when filtering different keys that belong to different key families.

If you are writing a package for TikZ please always leave a hook for power users to tweak things directly be it a node, coordinate or tikzpicture itself. An example;

\documentclass[tikz]{standalone}
\usetikzlibrary{showgrid}

\pgfkeys{/wickerson/tikz options/.code={
\tikzset{every picture/.prefix style={#1}}}
}

\newenvironment{wickersonpicture}[1][]{%
\pgfkeys{/wickerson/.cd,#1}
% do some stuff
\begin{tikzpicture}
}{%
\end{tikzpicture}
}

\begin{document}

\begin{wickersonpicture}[tikz options={show grid=all,red}]
\draw (3,2) -- +(45:3);
\end{wickersonpicture}

\end{document}


We love Tikz, it's nice, it's relatively powerful and amazingly well-documented. However in my opinion there is another reason that led to the wide adoption. The use-cases and structuring. I think Tantau's structuring ability is often overlooked. There are hundreds of options available and each library is very well separated. Anyone who tried to write a package would appreciate how difficult it is to come up with a consistent scheme as the functionality increases. So if the added value of some options are not unambiguously distinct, it is good practice to leave the TikZ options to TikZ without chewing them first with a pre-code.

• +1 for Tantau's structuring ability. The clear and canonical design (beautiful like an algebra – he is a professor for theoretical computer science :-) amazes me again and again. – Daniel May 28 '13 at 20:43
• Thanks @percusse, you explain that well. I like the idea of leaving hooks for powerusers, that makes a lot of sense. However, I would still prefer the user-facing syntax to be [draw guides, tikz options={red}] rather than [tikz options={show grid=all, red}] -- is that possible? The reason is that draw guides abbreviates a whole load of other 'drawing aids' besides the grid. – John Wickerson May 29 '13 at 7:27
• @JohnWickerson I can only think of filtering the wickerson key family. The main problem is that you change directory before #1 and even we change the directory back to tikz for draw guides martin's code will still assume the wickerson directory later in the code. – percusse May 30 '13 at 10:14