我想将 tikzpicture 放置在 tikz-cd 图的节点/单元中,然后在内部 tikzpicture 中绘制更多内容。但是,嵌套似乎会弄乱锚点center
。
具体来说:在下面的 MWE 中,为什么蓝色圆圈没有正确地位于节点的中心?我该如何修复它?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{cd}
\newcommand{\boxedBWithAnchors}{
\begin{tikzpicture}[baseline=(B.base)]
\node[draw=black!20] (B) {B};
\draw[draw=black!20] (B.south west) to (B.north east);
\draw[draw=black!20] (B.north west) to (B.south east);
\draw[draw=blue] (B.center) circle (1pt);
\end{tikzpicture}
}
\begin{document}
\,\boxedBWithAnchors
\begin{tikzcd}
\boxedBWithAnchors
\end{tikzcd}
\end{document}
答案1
问题是/tikz/commutative diagrams/every cell
样式设置shape=asymmetrical rectangle
没有将锚点居中。解决方案是在节点中再次center
明确设置:shape=rectangle
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{cd}
\newcommand{\boxedBWithAnchors}{
\begin{tikzpicture}[baseline=(B.base)]
\node[shape=rectangle,draw=black!20] (B) {B};
\draw[draw=black!20] (B.south west) to (B.north east);
\draw[draw=black!20] (B.north west) to (B.south east);
\draw[draw=blue] (B.center) circle (1pt);
\end{tikzpicture}
}
\begin{document}
\,\boxedBWithAnchors
\begin{tikzcd}
\boxedBWithAnchors
\end{tikzcd}
\end{document}
答案2
我同意 egreg 的观点,绝对不会使用 nest tikzpicture
。这也是不必要的。正如您所观察到的,rectangle
如果要将其添加到单个单元格,还需要明确将形状设置为。(但是,这可以在交换图中通过添加|[draw=black!20,outer sep=0pt,shape=rectangle]|
到单元格来完成。)我使用\pgfkeys{cross circle/.code args={#1|#2}{...
来允许用户使用/.list
键循环遍历多个单元格。还要注意,如果您想要在背景中使用十字,那么使用 会有一个更简单的解决方案path picture
。(据我所知,append after command
不允许您更改圆圈的颜色。)
\documentclass[border=3.14mm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{cd}
\newcommand{\boxedBWithAnchors}{
\begin{tikzpicture}[baseline=(B.base)]
\node[draw=black!20] (B) {B};
\draw[draw=black!20] (B.south west) to (B.north east);
\draw[draw=black!20] (B.north west) to (B.south east);
\draw[draw=blue] (B.center) circle (1pt);
\end{tikzpicture}
}
\pgfkeys{cross circle/.code args={#1|#2}{
\draw[black!20]
(\tikzcdmatrixname-#1-#2.north west) -- (\tikzcdmatrixname-#1-#2.south east);
\draw[black!20]
(\tikzcdmatrixname-#1-#2.south west) -- (\tikzcdmatrixname-#1-#2.north east);
\draw[draw=blue] (\tikzcdmatrixname-#1-#2.center) circle (1pt);}}
\begin{document}
\boxedBWithAnchors
\begin{tikzcd}[execute at end picture={\pgfkeys{cross circle={1|1}}
}]
|[draw=black!20,outer sep=0pt,shape=rectangle]|\text{B}
\end{tikzcd}
\end{document}