4

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.

2
  • Can you explain what outcome you want for the arrows? Are you looking for a way to make all the arrows in row 1 red? Or only the arrows within row 1 (i.e., from a row 1 cell to another row 1 cell)?
    – Sandy G
    May 17, 2022 at 12:06
  • @SandyG See my Edit.
    – Bubaya
    May 17, 2022 at 12:31

2 Answers 2

4

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}
4
  • Ah, that's where the row n key belonged. How did you find out? Do you also have a solution for the arrows?
    – Bubaya
    May 17, 2022 at 12:32
  • @Bubaya: The arrows were a lot harder, but I have a solution now.
    – Sandy G
    May 19, 2022 at 4:00
  • Wow, I'm deeply impressed by your TikZ magic! May I suggest that you replace your row1arrows by a generic two-argument row n arrow style? Tried it and it worked, but I didn't want to edit your answer.
    – Bubaya
    May 19, 2022 at 8:38
  • 1
    @Bubaya: Finally got around to implementing your suggestion. Note: there was an error in the earlier code—it would color the wrong row in subsequent diagrams. Only the first diagram would work. This was due to using tikz@f@1-#1-1 to identify the row position, but the first 1 refers to the diagram number. It needs to be replaced with \tikzcdmatrixname-#1-1, which took me a long time to find. It doesn't seem to be documented anywhere. Anyway, fixed now. The whole solution has been updated.
    – Sandy G
    May 23, 2022 at 22:40
3

With

\tikzcdset{arrow style=tikz,
           arrows =  {>=Straight Barb, very thick, gray}
           }

For color of nodes you can change their color only locally:

Complete MWE

\documentclass[margin=3mm, varwidth]{standalone}
\usepackage{amsmath}
\usepackage{tikz-cd}

\tikzcdset{
    arrow style=tikz,
    arrows =  {>=Straight Barb, very thick, gray}
}

\begin{document}
\[
    \begin{tikzcd}
    \color{red}A \rar \dar & \color{red}B \dar \\
        C \rar & D
    \end{tikzcd}
\]
\end{document}

enter image description here

3
  • hello Zarko ! I'm gonna edit your answer cause of an obvious missed copy/paste :) However, for a reason I ignore, i cannot repaste your image.
    – JeT
    May 17, 2022 at 12:19
  • SandyG's answer shows that the nodes' color can be changed not only globally.
    – Bubaya
    May 17, 2022 at 12:32
  • @Bubaya As you can see here you can apply styles locally or globally. Both answers are solution to your problem.
    – JeT
    May 17, 2022 at 12:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .