3

I want to place a tikzpicture in a node/cell of a tikz-cd diagram and then paint some more stuff in the inner tikzpicture. However, it seems that the nesting messes up the center anchor.

Concretely: In the following MWE, why is the blue circle not properly in the center of the node? How can I fix it?

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

Working anchor on the left, nested broken anchor on the right

2

The issue is that the /tikz/commutative diagrams/every cell style sets shape=asymmetrical rectangle which does not have the center anchor centered. The solution is to explicitly set shape=rectangle in the node again:

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

enter image description here

2

I would agree with egreg and definitely not nest tikzpictures. It is also not needed. As you observed, one needs to also explicitly set the shape to rectangle if one wants to add this to single cells. (However, this can be done in the commutative diagram by adding |[draw=black!20,outer sep=0pt,shape=rectangle]| to the cell.) I used \pgfkeys{cross circle/.code args={#1|#2}{... in order to allow users to use the /.list key to loop over several cells. Note also that if you wanted the cross in the background, there would be a much simpler solution with path picture. (AFAIK append after command won't allow you to change the color for the circle.)

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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