4

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.

10

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

  • 1
    +1! Where can it be found in tikz-cd documentation? I searched but I didn't find it. – CarLaTeX Dec 8 '17 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. – Henri Menke Dec 8 '17 at 9:22
  • I'll write to the authors asking to document \tikzcdmatrixname, thank you! – CarLaTeX Dec 8 '17 at 9:24
  • Nice the increasing string sizes: 3,4,5,6 to produce the diagonal alignment of node code. -) – Sigur Mar 1 at 14:51
5

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

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.