Nodes:
To change the style of a row use nodes={row 1/.style=red}
\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:
\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
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}
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}
Coloring arrows in a column is similar:
\[\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}