Set Arrow Length TikZ-cd – How to Set Absolute Length of Arrows in TikZ-cd for Diagrams

tikz-cd

If you want to change the lengths of arrows in a tikzcd diagram, there are lots of options:

  • Append "shorten => 10pt" or "shorten >= 15pt" to the arrow description.
  • Change the row/column separation using [{column sep = 15pt, row sep = 20pt}] at the start of the tikzcd.
  • Change individual column/row separations using [-15pt] or [5pt] before the appropriate & or // .

I am not asking about any of these.

If I want the absolute length of all (or one) of my arrows to be say 20pt, how can I do that?

That is, so all (or one in particular) of my arrows is 20pt long. Note that none of the above options do this.

Best Answer

Here is a possibility. Define a new arrow type called myarrow, which is a white arrow decorated with a special arrowhead. The arrowhead is a Butt Cap of a given length followed by a standard arrowhead (shifted to keep it centered). The length is controlled globally by \myarrowlength. You can then use myarrow for a single arrow as in the first example using \arrow[r, myarrow], or for all arrows as in the second example using \begin{tikzcd}[arrows=myarrow]

enter image description here

\documentclass{article}

\usepackage{tikz-cd}
\usetikzlibrary{decorations.markings}

\newcommand{\myarrowlength}{10pt}

\tikzset{mytip/.tip={Butt Cap[black, length=\myarrowlength, sep=-1.6pt]>[black]},
    myarrow/.style={white, decoration={transform={xshift=.5*\myarrowlength}, markings, mark=at position .5 with {\arrow{mytip}}}, postaction=decorate}}

\begin{document}

\begin{tikzcd}
A\arrow[dr]\\
B\arrow[u]\arrow[r, myarrow] & C
\end{tikzcd}
\qquad
\begin{tikzcd}[arrows=myarrow]
A\arrow[dr]\\
B\arrow[u]\arrow[r] & C
\end{tikzcd}

\end{document}

Update:

To add labels to the arrows, the white arrow must get a label. This is now included in the definition, along with a default empty label. For example, myarrow="h"' will place the label h on the "right" side. Use {} for more complicated labels, e.g., myarrow={"h"', text=red}.

enter image description here

\documentclass{article}

\usepackage{tikz-cd}
\usetikzlibrary{decorations.markings}

\newcommand{\myarrowlength}{10pt}

\tikzset{mytip/.tip={Butt Cap[black, length=\myarrowlength, sep=-1.6pt]>[black]},
    myarrow/.style={white, text=black, #1, decoration={transform={xshift=.5*\myarrowlength}, markings, mark=at position .5 with {\arrow{mytip}}}, postaction=decorate},
    myarrow/.default={}}

\begin{document}

\begin{tikzcd}
A\arrow[dr]\\
B\arrow[u]\arrow[r, myarrow="h"'] & C
\end{tikzcd}
\qquad
\begin{tikzcd}[arrows=myarrow]
A\arrow[dr]\\
B\arrow[u]\arrow[r] & C
\end{tikzcd}

\end{document}
Related Question