Style rows/columns of a `tikz-cd` diagram

tikz-cdtikz-pgftikz-styles

Apparently, the tikz keys row n/column n don't work in tikz-cd diagrams:

\documentclass{scrartcl}
\usepackage{tikz-cd, tikz}

\begin{document}
    \[
        \begin{tikzcd}[row 1/.style={nodes={font=\color{red}}}]
            A \rar \dar & B \dar \\
            C \rar & D
        \end{tikzcd}
    \]
    \[
        \begin{tikzpicture}
            \matrix[matrix of math nodes, row 1/.style={nodes={font=\color{red}}}]{
                A & B \\
                C & D \\
            };
        \end{tikzpicture}
    \]
\end{document}

I would have expected the first line of the first equation show up in red, like in the second. As you can see, this is not the case.

How can I achieve what I want, without applying the style to every node and every arrow?

Edit: The style for the nodes I can control with a tikzcdset before the diagram. But how about the arrows?

Edit 2: If possible, I'd like to have all arrows within row 1 red. If that's not possible, I'd be fine with all arrows starting in or defined in row 1.

Best Answer

Nodes:

To change the style of a row use nodes={row 1/.style=red}

enter image description here

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

\begin{document}
    \[
        \begin{tikzcd}[nodes={row 1/.style=red}]
            A \rar \dar & B \dar \\
            C \rar & D
        \end{tikzcd}
    \]
\end{document}

The other TikZ matrix options work similarly:

enter image description here

\begin{tikzcd}[nodes={every odd column/.style=red}]
    A \rar \dar & B \dar \rar & X \dar \\
    C \rar & D \rar & Y
\end{tikzcd}

Arrows: [Note—there was an error in the earlier code.]

The arrows are more difficult. The reason is that the \arrow command (or its shortcuts) does nothing to the diagram at the point it is called. Initially, there is an empty <paths> string, and every time an \arrow command is encountered,

\path[⟨options⟩] (⟨source node⟩) to (⟨target node⟩); 

is appended to <paths>. So to change the arrow colors for arrows in a certain row (say, row <r>), we have to get into the path of the arrows. The code is somewhat complicated, requiring some inserted code with \pgfextra, and then comparing the y-coordinate of the current <source node> with that of the <r>-1 entry (located at (\tikzcdmatrixname-<r>-1)).

This is all put into a tikzcdset with a style called rowarrows (with colarrows defines similarly):

\tikzcdset{rowarrows/.style args={#1/#2}{arrows={to path={\pgfextra{\pgfsetstrokecolor{black}}
        (\tikztostart)--(\tikztotarget) let \p1=(\tikztostart), \p2=(\tikzcdmatrixname-#1-1), \p3=(\tikztotarget) 
        in \pgfextra{\ifthenelse{\equal{\y1}{\y2}\AND\equal{\y3}{\y2}}
        {\pgfsetstrokecolor{#2}}{}}(\tikztostart)--(\tikztotarget)}}},
    colarrows/.style args={#1/#2}{arrows={to path={\pgfextra{\pgfsetstrokecolor{black}}
        (\tikztostart)--(\tikztotarget) let \p1=(\tikztostart), \p2=(\tikzcdmatrixname-1-#1), \p3=(\tikztotarget) 
        in \pgfextra{\ifthenelse{\equal{\x1}{\x2}\AND\equal{\x3}{\x2}}
        {\pgfsetstrokecolor{#2}}{}}(\tikztostart)--(\tikztotarget)}}}}

Then when you call \begin{tikzcd}[rowarrows=1/red], the result is

enter image description here

Note the syntax rowarrows=<row number>/<color>.

Combining this with the node solution from above:

\begin{tikzcd}[rowarrows=1/red, nodes={row 1/.style=red}]
    A \rar \dar & B \dar \rar & X \dar \\
    C \rar & D \rar & Y
\end{tikzcd}

enter image description here

You can mix and match node colors and arrow colors:

\begin{tikzcd}[rowarrows=2/blue, nodes={row 2/.style=red}]
    A \rar \dar & B \dar \rar & X \dar \\
    C \rar & D \rar & Y
\end{tikzcd}

enter image description here

Coloring arrows in a column is similar:

enter image description here

\[\begin{tikzcd}[colarrows=1/red, nodes={column 1/.style=red}]
    A \rar \dar & B \dar \rar & X \dar \\
    C \rar \dar & D \dar \rar & Y \dar \\
    E \rar \dar & F \dar \rar & Z \dar \\
    G \rar & H \rar & W
\end{tikzcd}\hspace{2cm}
\begin{tikzcd}[colarrows=3/red, nodes={column 2/.style=red}]
    A \rar \dar & B \dar \rar & X \dar \\
    C \rar \dar & D \dar \rar & Y \dar \\
    E \rar \dar & F \dar \rar & Z \dar \\
    G \rar & H \rar & W
\end{tikzcd}\]

One shortcoming (at the moment) is that it is only possible to color the arrows in one row or in one column. Not both.

Here is the complete code:

\documentclass{article}
\usepackage{tikz-cd, ifthen}
\usetikzlibrary{calc} % needed for "let" operation

\tikzcdset{rowarrows/.style args={#1/#2}{arrows={to path={\pgfextra{\pgfsetstrokecolor{black}}
        (\tikztostart)--(\tikztotarget) let \p1=(\tikztostart), \p2=(\tikzcdmatrixname-#1-1), \p3=(\tikztotarget) 
        in \pgfextra{\ifthenelse{\equal{\y1}{\y2}\AND\equal{\y3}{\y2}}
        {\pgfsetstrokecolor{#2}}{}}(\tikztostart)--(\tikztotarget)}}},
    colarrows/.style args={#1/#2}{arrows={to path={\pgfextra{\pgfsetstrokecolor{black}}
        (\tikztostart)--(\tikztotarget) let \p1=(\tikztostart), \p2=(\tikzcdmatrixname-1-#1), \p3=(\tikztotarget) 
        in \pgfextra{\ifthenelse{\equal{\x1}{\x2}\AND\equal{\x3}{\x2}}
        {\pgfsetstrokecolor{#2}}{}}(\tikztostart)--(\tikztotarget)}}}}

\begin{document}
\[\begin{tikzcd}[rowarrows=1/red, nodes={row 1/.style=red}]
    A \rar \dar & B \dar \rar & X \dar \\
    C \rar & D \rar & Y
\end{tikzcd}\hspace{2cm}
\begin{tikzcd}[rowarrows=2/red, nodes={row 1/.style=red}]
    A \rar \dar & B \dar \rar & X \dar \\
    C \rar & D \rar & Y
\end{tikzcd}\]

\[\begin{tikzcd}[colarrows=1/red, nodes={column 1/.style=red}]
    A \rar \dar & B \dar \rar & X \dar \\
    C \rar \dar & D \dar \rar & Y \dar \\
    E \rar \dar & F \dar \rar & Z \dar \\
    G \rar & H \rar & W
\end{tikzcd}\hspace{2cm}
\begin{tikzcd}[colarrows=3/red, nodes={column 2/.style=red}]
    A \rar \dar & B \dar \rar & X \dar \\
    C \rar \dar & D \dar \rar & Y \dar \\
    E \rar \dar & F \dar \rar & Z \dar \\
    G \rar & H \rar & W
\end{tikzcd}\]

\end{document}
Related Question