1

The following macro produces an arrow with a name between two objects. It has three parameters.

\newcommand{\rname}[3]{
        \begin{tikzcd}[ampersand replacement=\&]
            {#1} \arrow[r, "{#2}"] \& {#3}
        \end{tikzcd}
    }

However, I would like to have only one parameter, the name of the arrow:

\newcommand{\rname}[1]{
        \begin{tikzcd}[ampersand replacement=\&]
              \arrow[r, "{#1}"] \& 
        \end{tikzcd}
    }

But this does not work with tikz. How can I fix it? Here is a compilable example:

\documentclass{article}

\usepackage{tikz-cd}

\newcommand{\rname}[3]{
    \begin{tikzcd}[ampersand replacement=\&]
        {#1} \arrow[r, "{#2}"] \& {#3}
    \end{tikzcd}
}

\begin{document}
    $\rname{A}{f}{B}$
   %$A\rname{f}B$ I would like it to function like this.
\end{document}
1

1 Answer 1

1

Your one-parameter attempt nearly works, you'll just need to make sure that the cells aren't completely empty, e.g. an empty {} will do the job:

\documentclass{article}

\usepackage{tikz-cd}

\newcommand{\rname}[1]{
    \begin{tikzcd}[ampersand replacement=\&]
        \arrow[r, "{#1}"] \& {}
    \end{tikzcd}
}

\begin{document}
    $A\rname{f}B$
\end{document}

However if you just want to add some text above the arrow, using tikzcd seems a bit overkill. I would suggest something like

\documentclass{article}

\usepackage{amsmath}

\newcommand{\rname}[1]{
    \xrightarrow{~~#1~~}
}

\begin{document}
    $A\rname{f}B$
\end{document}
5
  • Thank you. But if I do it like this, then there are extra spaces between the letters and the arrow. How can I remove these?
    – Shimrod
    Oct 3, 2022 at 19:06
  • 1
    @Shimrod The extra space is because your A and B are not part of the tikzcd. That's a consequence of the one parameter syntax you wanted. If you want correct spacing, use the 3 parameter macro. Oct 3, 2022 at 19:07
  • Ok, I will use that. Thank you.
    – Shimrod
    Oct 3, 2022 at 19:09
  • @Shimrod Please see my edit, I suggested a second, much simpler solution Oct 3, 2022 at 19:13
  • That's great! Finally I know how to add spacing to the \xrightarrow with a name so that it has correct length. Thank you!
    – Shimrod
    Oct 3, 2022 at 19:22

You must log in to answer this question.

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