7

tikz-cd wraps matrix creation as follows

\def\tikzcd@[#1]{%
  \tikzpicture[/tikz/commutative diagrams/.cd,every diagram,#1]%
  % ...
  \matrix[%
    % ...
    ]%
  \bgroup}

As such, there seems no way to name the matrix as created by the tikzcd environment. Is there a way to name the matrix, and still using the tikzcd environment?

The question arises out of the need to name cells of matrices. With the name of a matrix known, the cells could be conveniently referred to as name-1-1, for example.

54690
  • 215
  • 1
  • 4

2 Answers2

13

The name is already set and can be retrived from the macro \tikzcdmatrixname. To further draw over your tikzcd matrix use execute at end picture.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{cd}
\begin{document}

\begin{tikzcd}[execute at end picture={
    \filldraw[red] (\tikzcdmatrixname-1-1) circle[radius=1pt];
    \filldraw[blue] (\tikzcdmatrixname-1-2) circle[radius=1pt];
    \filldraw[green] (\tikzcdmatrixname-2-1) circle[radius=1pt];
    \filldraw[orange] (\tikzcdmatrixname-2-2) circle[radius=1pt];
  }]
  A \ar[r]\ar[d] & B \ar[d] \\
  C \ar[r]       & D \\
\end{tikzcd}

\end{document}

enter image description here

Henri Menke
  • 109,596
7

The author of tikz-cd himself in person (Florêncio Neves) suggested to me to use every matrix/.append style={name=...} (don't forget the append, see Torbjørn T.'s answer here). This way you could name your matrix as you wish.

I also think it's possible to use remember picture and overlay instead of execute at end picture. Here an MWE with both cases:

\documentclass{article}
\usepackage{tikz-cd}

\begin{document}
\begin{tikzcd}[every matrix/.append style={name=mycd}, 
    execute at end picture={
    \filldraw[red] (mycd-1-1) circle[radius=1pt];
    \filldraw[blue] (mycd-1-2) circle[radius=1pt];
    \filldraw[green] (mycd-2-1) circle[radius=1pt];
    \filldraw[orange] (mycd-2-2) circle[radius=1pt];
    }]
    A \ar[r]\ar[d] & B \ar[d] \\
    C \ar[r]       & D 
\end{tikzcd}%

\begin{tikzcd}[every matrix/.append style={name=mycd}, remember picture]
    A \ar[r]\ar[d] & B \ar[d] \\
    C \ar[r]       & D 
\end{tikzcd}%
\begin{tikzpicture}[overlay, remember picture]
    \filldraw[red] (mycd-1-1) circle[radius=1pt];
    \filldraw[blue] (mycd-1-2) circle[radius=1pt];
    \filldraw[green] (mycd-2-1) circle[radius=1pt];
    \filldraw[orange] (mycd-2-2) circle[radius=1pt];
\end{tikzpicture}
\end{document}

enter image description here

CarLaTeX
  • 62,716