1

I have a tikz-node with some text inside it and a small tikz-drawn symbol as well.

When I apply rounded corners with some value to the outer node, the inner drawing inherits this attribute.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    % Here the outer node is simply drawn as a rectangle, the inner tikz is drawn as expected.
    \node[draw] at (0,0) {This is a Node with a \tikz\draw (0,0) rectangle (.5,.5); rectangle inside.};

    % Here the outer node is correctly drawn with rounded corners.
    % I would expect the inner tikz to render the same as above, but it inherits the rounded corners as well
    \node[draw, rounded corners=1ex] at (0,-2) {This is a Node with a \tikz\draw (0,0) rectangle (.5,.5); rectangle inside.};

    % I do not observe this behaviour with other style attributes.
    % Below the inner tikz is black with normal line width.
    \node[draw=green, very thick] at (0,-4) {This is a Node with a \tikz\draw (0,0) rectangle (.5,.5); rectangle inside.};
\end{tikzpicture}
\end{document}

I am not sure whether this is a bug or a feature. But I would like to stop this propagation, preferably without turning it of explicitply by stating rounded corners=0pt in the inner drawing.

1 Answers1

4

Nesting of tikzpicture environment is not supported, and future versions of tikz will issue a warning to make this clear(er). The reason why you experience these effects is that the pgf keys of the ambient tikzpicture apply to the inner one, i.e. the box in your case. The only secure way that I am aware of that allows you to retain full control over the inner tikzpictures (with reasonable efforts) is, according to this discussion, to use a \savebox to save a "naked" tikzpicture. In this case, the inner tikzpicture won't inherit any key of the ambient tikzpicture because it was "done" outside.

\documentclass{article}
\usepackage{tikz}
\newsavebox\rect
\sbox\rect{\tikz\draw (0,0) rectangle (.5,.5);}
\begin{document}
\begin{tikzpicture}
    % Here the outer node is simply drawn as a rectangle, the inner tikz is drawn as expected.
    \node[draw] at (0,0) {This is a Node with a \usebox{\rect} rectangle inside.};

    % Here the outer node is correctly drawn with rounded corners.
    % I would expect the inner tikz to render the same as above, but it inherits the rounded corners as well
    \node[draw, rounded corners=1ex] at (0,-2) {This is a Node with a
    \usebox{\rect} rectangle inside.};

    % I do not observe this behaviour with other style attributes.
    % Below the inner tikz is black with normal line width.
    \node[draw=green, very thick] at (0,-4) {This is a Node with a
    \usebox{\rect} rectangle (.5,.5); rectangle inside.};
\end{tikzpicture}
\end{document}

enter image description here

In principle, you may think of resetting the the pgf keys one by one. As long as you have only few of them, this may work. However, in general this is, however, very tedious, if not impossible. To give you an example, if you set inner sep to a certain value, it will set inner xsep and inner ysep accordingly. However, in your actual example you may undo rounded corners by using the sharp corners key. That is, the following example leads to the same output as above.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    % Here the outer node is simply drawn as a rectangle, the inner tikz is drawn as expected.
    \node[draw] at (0,0) {This is a Node with a \tikz\draw (0,0) rectangle (.5,.5); rectangle inside.};

    % Here the outer node is correctly drawn with rounded corners.
    % I would expect the inner tikz to render the same as above, but it inherits the rounded corners as well
    \node[draw, rounded corners=1ex] at (0,-2) {This is a Node with a
    \tikz[sharp corners]\draw (0,0) rectangle (.5,.5); rectangle inside.};

    % I do not observe this behaviour with other style attributes.
    % Below the inner tikz is black with normal line width.
    \node[draw=green, very thick] at (0,-4) {This is a Node with a \tikz\draw (0,0) rectangle (.5,.5); rectangle inside.};
\end{tikzpicture}
\end{document}

In general, however, I'd like to argue that nesting tikzpictures means opening Pandora's box, and should thus be avoided. In all concrete examples I've seen so far there was always a way to achieve the desired output without nesting tikzpictures.

  • Actually, I didn't realize this was not supported. In my actual code there is no direct tikz-invocation but rather a macro with a parameter which affects the drawing's color. That won't work as easy with a savebox, right? – David Woitkowski Jan 23 '19 at 15:09
  • @DavidWoitkowski I also added an example with sharp corners. Here you can change the colors at will. However, I'd also like to warn you that by following that route you are opening Pandora's box. (As long as your examples are very simple, you may be able to handle it with small efforts, though.) –  Jan 23 '19 at 15:21