3

Is there any possibility of “unsetting” a PGF/TikZ key? By “unsetting” I mean to remove the key from the list of keys and make PGF/TikZ behave as if it was never specified. To clarify, consider this minimal working example:

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{shapes.misc}

\begin{document}
\begin{tikzpicture}
  \tikz\node[draw=red, circle] {Short 1};
  \node[draw, text width=3cm] (r1) {
    \tikz\node[draw=blue, circle] {Short 2};
  };
\end{tikzpicture}
\end{document}

In the resulting output, I would expect the two circles to be of equal size (except for a tiny difference because of “1” vs. “2”). However, the second (blue) circle inherits the text width property from the parent node. Now, I would like to have that (blue) circle behave as if text width was never specified for it. How do I achieve this?

What’s working, of course, is manually specifying a different text width, but that’s not an option for me because I would like to have dynamic node sizes inside another node. Using minimum width/height on the outer node instead of text width does not produce satisfactory results for my eventual use-case either (which is, BTW, to have nodes of the same size, some containing text, some containing other nodes) – I could manage to use that, but it would mean significant overhead of creating additional nodes, as far as I see it.

The question might, of course, be generalized into something along the lines of “How do I put a tikzpicture inside a TikZ node that does not inherit the keys set for the parent node?”

I hope to have made myself clear and that there is an easy answer to my question ;-)

6
  • 1
    why do you write \tikz before \node?
    – Vivi
    Commented Mar 14, 2012 at 8:20
  • Nesting TikZ environments like this is not supported and can lead to strange behaviour (such as what you are seeing here). I strongly recommend that you find a different way to achieve what you want. (For more on nesting, see tex.stackexchange.com/a/46792/86 and the questions linked to that one) Commented Mar 14, 2012 at 9:39
  • Yes it's preferable to avoid the nesting TikZ environments ! Finally I asked bad questions ( do as I say and not as I do ) ! Commented Mar 14, 2012 at 9:53
  • @Vivi: That was probably just a copy-and-paste mistake.
    – ilpssun
    Commented Mar 14, 2012 at 10:46
  • @AndrewStacey Thanks for the pointer. I didn’t know nesting pictures would cause so much trouble – so far it has worked rather well for me ;-) It seems, though, that using a 1×1-matrix is a better way for future diagrams if I only need one level of nesting (cf. tex.stackexchange.com/questions/1003/…).
    – ilpssun
    Commented Mar 14, 2012 at 11:04

1 Answer 1

6

If you really want to use \tikz inside the node you need to add text width= inside the blue node. If you don't do that text width=5cm is inherited by the last node.

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{shapes.misc}

\begin{document}
\begin{tikzpicture}
  \node[draw=red, circle] {Short 1}; % the first \tikz is not useful and it's a bad idea
  \node[draw, text width=5cm] (r1) {%
    \tikz \node[draw=blue, circle,text width=] {Short 2};%
  };
\end{tikzpicture}
\end{document}

enter image description here

If the code is more complex, you need to save the included environment tikzpicture inside a box. (see the link given by Andrew)

5
  • Sneaky solution! Commented Mar 14, 2012 at 10:16
  • @AndrewStacey What do you mean by sneaky ? my dictionary gives me some strange translation. It's fine or bad ? sneaky is like snaky ? convoluted, winding, twisted, wavy, undulatory, circling, snake-like, serpentine, vermiform, vermicular, tortuous, sinuous, involved, intricate, complicated, perplexed, labyrinth, labyrinthine, peristaltic, kinky, knotted; ?? :) Commented Mar 14, 2012 at 10:21
  • This solution is actually so simple, I feel bad about asking the original question. It works, of course, although I could have sworn I tried that to no avail before (probably, though, I just tried it with the minimum size key, which requires a value).
    – ilpssun
    Commented Mar 14, 2012 at 10:49
  • It's not so obvious. You have only one line in the manual that says : Setting ⟨dimension⟩ to an empty string causes the automatic line breaking to be disabled. Commented Mar 14, 2012 at 11:00
  • 4
    In this context, "Sneaky" is meant positively: it means I'm impressed by the solution. Something is "sneaky" if it is unexpected. A "sneak" (bad term) is someone who goes around quietly listening to things they oughtn't to know (Sam called Gollum a "sneak" in LOTR). The implication is that to get this solution, you had to "sneak around" in the depths of TikZ/PGF learning things that are kept from view. In this case, that's clearly not a bad thing to do, whence it is a term of pure praise. Commented Mar 14, 2012 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .