我希望在另一个 tikzpicture 的节点内创建 tikzpicture 环境。我遇到的问题是样式由内部 tikzpicture 继承。我如何从外部 tikzpicture 中筛选出内部 tikzpicture?或者这是不可能的?
以下最小示例说明了这个问题:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
% wanted: square centered on line
\begin{tikzpicture}
\draw (0,0) -- (0,1);
\node[fill] at (0,.5) {};
\end{tikzpicture}
% got: square to the right of line
\begin{tikzpicture}[red]
\node[fill] (O) {};
\node[right=of O] {% <- this 'right of' is inherited; how to avoid?
\begin{tikzpicture}
\draw (0,0) -- (0,1);
\node[fill] at (0,.5) {};
\end{tikzpicture}
};
\end{tikzpicture}
\end{document}
答案1
在这种特定情况下,您可以使用anchor=center
恢复默认定位。right
等设置会修改使用的锚点。
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
% wanted: square centered on line
\begin{tikzpicture}
\draw (0,0) -- (0,1);
\node[fill] at (0,.5) {};
\end{tikzpicture}
% got: square to the right of line
\begin{tikzpicture}[red]
\node[fill] (O) {};
\node[right=of O] {% <- this 'right of' is inherited; how to avoid?
\begin{tikzpicture}[anchor=center]
\draw (0,0) -- (0,1);
\node[fill] at (0,.5) {};
\end{tikzpicture}
};
\end{tikzpicture}
\end{document}
在一般情况下,如果你想避免任何设置会影响子tikzpicture
我会将其存储到主之前的保存框中tikzpicture
并插入该框:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
% wanted: square centered on line
\begin{tikzpicture}
\draw (0,0) -- (0,1);
\node[fill] at (0,.5) {};
\end{tikzpicture}
\newsavebox\mybox
\begin{lrbox}{\mybox}
\begin{tikzpicture}
\draw (0,0) -- (0,1);
\node[fill] at (0,.5) {};
\end{tikzpicture}
\end{lrbox}
% got: square to the right of line
\begin{tikzpicture}[red]
\node[fill] (O) {};
\node[right=of O] {% <- this 'right of' is inherited; how to avoid?
\usebox\mybox
};
\end{tikzpicture}
\end{document}