# Nesting tikzpictures, style inheritance and save box

Throughout my document, I need a custom symbol, which I draw using TikZ. I have created a macro for it, so I do not need to copy the picture. I also need to use the symbol within other tikzpictures. The problem here is style inheritance: the style of the outer pictures must not interfere with the symbol produced by the macro.

The answers to this question and this question suggest to use a savebox to overcome this problem. I have tried to do this but, so far, have failed. Here is a minimal working example:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{automata, positioning}

\newsavebox{\blankbox}
\savebox{\blankbox}{\hspace{0.1ex}\tikz[baseline=0.1em]{%
\node [shape=rectangle, anchor=south, draw, inner sep=0pt, minimum width=1ex, minimum height=0.9em] (char) {}}%
\hspace{0.1ex}}

\begin{document}
\begin{tikzpicture}[auto, shorten >=1pt, >=latex]
\node[state] (0) {$\usebox{\blankbox}$};
\node[state, dotted, right=1ex of 0] (1) {$\usebox{\blankbox}$};
\end{tikzpicture}
\end{document}


and its output

As you can see, the style of the right node is inherited by the rectangle.

My question is: how can I fix this while still using the same macro everywhere?

Edit: In the comments Zarko suggests to change the line type in the inner picture manually. This obviously solves the problem in this specific example but what other properties do I need to set as well to "protect" the inner picture? I have tried some (like color, double, decoration, rotate) but (surprisingly to me) those aren't inherited. Does that mean that only dash pattern is inherited?

• Using pics might help. – Claudio Fiandrino May 11 '16 at 15:57
• the style of the outer pictures must not interfere but it will. – percusse May 11 '16 at 16:11
• In such a cases you need in nested images to determine type of lines. In your case ... \node [rectangle, anchor=south, draw, solid, inner sep=0pt, minimum width=1ex, minimum height=0.9em] ... – Zarko May 11 '16 at 17:04
• @ClaudioFiandrino: I tried using a pic but the line pattern still seems to be inherited. – JPW May 12 '16 at 8:19
• I'd consider it an advantage: Line style inheritance, even by saveboxed stuff! The other way around, no inheritance at all, would be much more difficult to overcome, I guess. – AlexG May 12 '16 at 8:34

You cannot. You cannot protect the inner picture by a box. You cannot protect the inner picture by .pic. You cannot protect the inner picture even you externalize it and then include it back.

In fact, you can Change color in finished pdf file.

@Heiko Oberdiek's answer shows that it is indeed a PDF feature to inherit everything. All you can do is to reset every parameters that potentially affects the inner picture.

Fortunately, there is a finite list of parameters and can be found in PDF specification. So good luck.

• So, to be safe, what I basically need to do is to compile a list of TikZ options that map to PDF parameters? This sounds a bit tedious and it is surprising to me that color is not inherited, especially in the light of the question you linked. – JPW Mar 30 '17 at 7:42

If you have the option of switching to another engine, XeTeX will behave as you wish.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{automata, positioning}

\newsavebox{\blankbox}
\savebox{\blankbox}{\hspace{0.1ex}\tikz[baseline=0.1em]{%
\node [shape=rectangle, anchor=south, draw, inner sep=0pt, minimum width=1ex, minimum height=0.9em] (char) {}}%
\hspace{0.1ex}}

\begin{document}
\begin{tikzpicture}[auto, shorten >=1pt, >=latex]
\node[state] (0) {\usebox{\blankbox}};
\node[state, dotted, right=1ex of 0] (1) {\usebox{\blankbox}};
\end{tikzpicture}
\end{document}


• How did you even know that XeLaTeX behaves differently? – Symbol 1 Mar 29 '17 at 0:59
• @Symbol1 know is a bit strong. I suspected it would. I figured if the issue is with the way the PDF itself handles things, then it would make sense that XeTeX (or TeX + whatever) might give a different result, since then we're not producing PDF directly. It seemed unlikely that inheritance would manage to survive the two-stage process. Not impossible, but unlikely. Especially since XeTeX is often problematic for just this reason: TikZ pictures don't look as they should i.e. do with pdfTeX or whatever. If it is a problem there, I figured it might be a solution here. I guessed ;). – cfr Mar 29 '17 at 1:57
• Although I unfortunately do not have this option (arXiv does not support XeTeX), this is an amazing and astonishing answer. However, it feels more like a XeTeX/TikZ bug than like intended behavior to me. – JPW Mar 30 '17 at 7:35
• @JPW I'm not sure it is a bug. More the nature of the beast, I think. Any more than inheritance is a bug in pdfTeX. – cfr Mar 30 '17 at 12:19