Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 ;-)

share|improve this question
1  
why do you write \tikz before \node? –  Vivi Mar 14 '12 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) –  Andrew Stacey Mar 14 '12 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 ) ! –  Alain Matthes Mar 14 '12 at 9:53
    
@Vivi: That was probably just a copy-and-paste mistake. –  ilpssun Mar 14 '12 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 Mar 14 '12 at 11:04
add comment

1 Answer

up vote 5 down vote accepted

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)

share|improve this answer
    
Sneaky solution! –  Andrew Stacey Mar 14 '12 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; ?? :) –  Alain Matthes Mar 14 '12 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 Mar 14 '12 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. –  Alain Matthes Mar 14 '12 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. –  Andrew Stacey Mar 14 '12 at 12:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.