# Setting the name of matrix in tikz-cd

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.

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={
}]
A \ar[r]\ar[d] & B \ar[d] \\
C \ar[r]       & D \\
\end{tikzcd}

\end{document}


• +1! Where can it be found in tikz-cd documentation? I searched but I didn't find it. Dec 8, 2017 at 9:21
• @CarLaTeX I was reading the source and found \global\let\tikzcdmatrixname\tikzlastnode in there i.e. it is not documented. The execute at end picture trick on the other hand is documented. Dec 8, 2017 at 9:22
• I'll write to the authors asking to document \tikzcdmatrixname, thank you! Dec 8, 2017 at 9:24
• Nice the increasing string sizes: 3,4,5,6 to produce the diagonal alignment of node code. -) Mar 1, 2019 at 14:51
• This solution just saved me a lot of editing. Thank you! Feb 16, 2020 at 0:27

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={
}]
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]