3

I'm trying to write some macros for making tikz-cd commutative diagrams. In tikz-cd you can give your arrows options like "bend left" or "dotted", in a comma-delimited list:

\documentclass{article}

\usepackage{tikz-cd}

\begin{document} 

\[ \begin{tikzcd}
    A \arrow[bend left, dotted]{r}{f^*} & B 
\end{tikzcd} \]

\end{document}

So that works fine! But since I want to write macros, I'd like to do things like put those parameters in a string. So I tried the following:

\documentclass{article}

\usepackage{tikz-cd}
\newcommand{\arrowformatting}{bend left, dotted}

\begin{document} 

\[ \begin{tikzcd}
    A \arrow[\arrowformatting]{r}{f^*} & B 
\end{tikzcd} \]

\end{document}

But this breaks; tikz-cd interprets the entire string as one argument and ignores the comma, giving the error

! Package pgfkeys Error: I do not know the key '/tikz/bend left, dotted' and I am going to ignore it. Perhaps you misspelled it.

How do I define a string \arrowformatting such that tikz-cd will actually interpret the comma correctly?

1

This will work, but the cure may be worse than the sickness, if you limit yourself to typing it in directly each time.

\documentclass{article}

\usepackage{tikz-cd}
\newcommand{\arrowformatting}{bend left, dotted}

\begin{document} 

\[ \begin{tikzcd}
    A \expandafter\arrow\expandafter[\arrowformatting]{r}{f^*} & B 
\end{tikzcd} \]

\end{document}

Perhaps this alternative would suit, creating \xarrow, which expands the next token before issuing an \arrow:

\documentclass{article}

\usepackage{tikz-cd}
\edef\arrowformatting{bend left, dotted}
\def\xarrow{\expandafter\arrow\expandafter}

\begin{document} 

\[ \begin{tikzcd}
    A \xarrow[\arrowformatting]{r}{f^*} & B 
\end{tikzcd} \]

\[ \begin{tikzcd}
    A \xarrow{r}{f^*} & B 
\end{tikzcd} \]

\end{document}

enter image description here

If this latter method proves problematic in the absence of optional arguments (and I don't think it should), here is another form that should be even safer. However, it requires the syntax

A \xarrow\arrowformatting{r}{f^*} & B

without brackets around \arrowformatting:

\documentclass{article}

\usepackage{tikz-cd}
\edef\arrowformatting{\expandafter[bend left, dotted]}
\def\xarrow{\expandafter\arrow}

\begin{document} 

\[ \begin{tikzcd}
    A \xarrow\arrowformatting{r}{f^*} & B 
\end{tikzcd} \]

\[ \begin{tikzcd}
    A \xarrow{r}{f^*} & B 
\end{tikzcd} \]
\end{document}
  • 1
    Thanks! I can see that fixes the example I gave, and I'll try things out tonight to see if it works out with the macro I want to write (and update the question if something else breaks I guess). – djc Apr 10 '14 at 18:38
  • @djc You should be safe if using \xarrow only if optional arguments are present (since optional arguments are text), but I don't know enough of tikz to know of any cases where it would break if you used \xarrow when no optional argument is present. For it to break, the first item after the [ or { would have to be a macro, and even then, it it is likely to work. – Steven B. Segletes Apr 10 '14 at 18:42
  • @djc See revision for even safer alternative. – Steven B. Segletes Apr 10 '14 at 18:49
  • Okay, I've been playing around with things and now I'm stuck on what I wanted to do next, which was to make \arrowformatting be something that produced output from string manipulation commands (rather than a fixed string like I was trying to get to work in my question). I guess I was assuming that would just work, but apparently I really don't understand how tex manipulates strings. Anyway, is it better to post a new question or update this one? – djc Apr 11 '14 at 2:13
  • @djc Without knowing more details, I couldn't really say if it would be better to revise the question or make a new one. I will not be offended whatever you choose. It is up to you. – Steven B. Segletes Apr 11 '14 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.