# Row spacing and arrow length in tikz-cd

In the source below, how should I modify the macro Func, which uses tikz-cd, so that it reproduces the output from the macro func, which uses just the array environment?

In particular, how should I:

1. Control the row spacing with the tikz-cd version to robustly reproduce the row spacing from the array version?
2. How do I shorten the arrows in the tikz-cd version so as to match the lengths in the array version (and position the nodes in the tikz-cd version so they are positioned the same way as the "X", "Y" and "x", "f(x)" items in the array version?

As to (2), I tried to include option shorten=1em in each \ar command, but that produces weird results; and I tried to include instead scalearrow={0.5}{start}{end} but that creates a tikzcd error.

Note that I want to make the necessary modifications only within the macro Func, so that they do not affect other instances of the tizkcd environment!

Aim: Although the array version works well (especially since I also have variants of it to allow different kinds of vertical alignment then the items have different lengths), I want to try a tikz-cd version so as to facilitate additional "decoration" with tikz constructs.

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz-cd}
\usetikzlibrary{arrows.meta,decorations.markings}
\tikzset{
>={Straight Barb[scale=0.8]},
commutative diagrams/arrow style=tikz
}
\newcommand{\from}{\protect\colon}

% version using array:
\newcommand{\func}[6][]{%
\arraycolsep=0pt
\begin{array}[#1]{rlcl}
{#2} \from\, &{#3}&\,\to\,    &\,{#4}\\
&{#5}&\,\mapsto\,&\,{#6}
\end{array}}

% version using tikz-cd:
\newcommand{\Func}[6][]{%
\begin{tikzcd}[
ampersand replacement=\&,
row sep=-4pt,
column sep = small,
/tikz/column 1/.append style = {nodes = {anchor=base east}}
]
{#2 \from #3} \ar[r] \& {#4}\\
{#5} \ar[r, mapsto] \& {#6}
\end{tikzcd}}

\begin{document}
$\func{f}{X}{Y}{x}{f(x)}$

$\Func{f}{X}{Y}{x}{f(x)}$
\end{document}


• Why trying to do it with tikz-cd? May 20 at 15:50
• I'm not sure to understand what you want. If you want to achieve the exact same result produced with array and \to arrows, then use Computer Modern Rightarrow instead of Straight Barb and adjust lengths locally instead of options to the environment. To shorten the arrows use the shorten key, and to extend them (as you surly need in the mapsto arrow) give a negative value to the key. May 20 at 16:31

I'm not sure what you are really trying to achieve here, but if you are just trying to match the arrows \to and \mapsto to that in the tikz-cd fashion, then you may consider changing them to \tikz\draw[->](0,0)--(.3,0); and \tikz\draw(0,0)edge[commutative diagrams/mapsto](.3,0);, respectively. (In the code below I modified your \func.)

\documentclass{article}
\usepackage{amsmath}

\usepackage{tikz-cd}
\usetikzlibrary{arrows.meta,decorations.markings}
\tikzset{
>={Straight Barb[scale=0.8]},
commutative diagrams/arrow style=tikz
}
\newcommand{\from}{\protect\colon}

% version using array:
\newcommand{\func}[6][]{%
\arraycolsep=0pt
\begin{array}[#1]{rrll}
{#2} \from\, &{#3}& \;\tikz\draw[->](0,0)--(.3,0);\, &\,{#4}\\
&{#5}& \;\tikz\draw(0,0)edge[commutative diagrams/mapsto](.3,0);\,&\,{#6}
\end{array}}

% version using tikz-cd:
\newcommand{\Func}[6][]{%
\begin{tikzcd}[
ampersand replacement=\&,
row sep=-4pt,
column sep = small,
/tikz/column 1/.append style = {nodes = {anchor=base east}}
]
{#2 \from #3} \ar[r] \& {#4}\\
{#5} \ar[r, mapsto] \& {#6}
\end{tikzcd}}

\begin{document}
$\func{f}{X}{Y}{x}{f(x)}$

$\Func{f}{X}{Y}{x}{f(x)}$

$\func{f}{X}{YYYYYYYYY}{x}{f(x)}$

$\Func{f}{X}{YYYYYYYYY}{x}{f(x)}$
\end{document}


On the other hand, since the arrow length in the tikz-cd version depends on the width of the entries, to match the arrow length, you may need to changing the column width to a fixed value. However, it would be hard to have an appropriate value that works in all situations.

• I want the arrows in the tikz-cd version to be the same as those in my non-tikz-using original func, and the vertical alignments in the tikz-cd version to be the same as those in my original func. Your Func does not do that. May 20 at 14:52
• @murray Actually I didn’t modify your \Func, I was just changing the arrows in your \func to match that of the tikz-cd version. I don’t think there is a practical way for tikz-cd to use fixed-width arrows since this is not what tikz-cd was designed for. Why do you insist on using tikz-cd for this propose? May 20 at 15:02
• You *did modify my array-using func by including tikz commands in them. I'm asking about using tikz-cd for the reason I stated in the "Aim" paragraph I already had added to my OP. May 21 at 0:23
• @murray Could you please be more specific about additional decoration with TikZ constructs? If you let us know what you want explicitly perhaps we can find a way to achieve it directly. May 21 at 7:01