如何停止将圆角继承到嵌入节点

如何停止将圆角继承到嵌入节点

我有一个 tikz 节点,里面有一些文本,还有一个小的 tikz 绘制的符号。

当我将rounded corners某个值应用到外部节点时,内部图形会继承该属性。

\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}

我不确定这是错误还是功能。但我想阻止这种传播,最好不要通过rounded corners=0pt在内部绘图中明确说明来关闭它。

答案1

tikzpicture不支持环境嵌套,并且未来版本tikz将会发出警告,以更清楚地说明这一点。您之所以会遇到这些效果,是因为环境的 pgf 键tikzpicture适用于内部环境,即您案例中的盒子。据我所知,唯一可以让你完全控制内部tikzpictures(通过合理的努力)的安全方法是,根据这次讨论,使用 来\savebox保存“裸” tikzpicture。在这种情况下,内部tikzpicture不会继承环境的任何键tikzpicture,因为它是在外部“完成”的。

\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}

在此处输入图片描述

原则上,您可以考虑逐个重置 pgf 键。只要您只有几个键,这种方法可能有效。但是,一般来说,这非常繁琐,甚至不可能。举个例子,如果您设置inner sep为某个值,它将相应地设置inner xsepinner ysep但是,在您的实际示例中,您可以使用键撤消圆角sharp corners。也就是说,以下示例导致与上述相同的输出。

\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}

但总的来说,我想说嵌套tikzpictures 意味着打开潘多拉魔盒,因此应该避免。到目前为止,在我见过的所有具体示例中,总有一种方法可以在不嵌套tikzpictures 的情况下实现所需的输出。

相关内容