0

If I have this MWE:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz-cd,tikz}
\usetikzlibrary{decorations.pathmorphing}

\DeclareRobustCommand{\twoheads}{\mathbin{\begin{tikzcd}[column sep=1.2em]{\mkern-10mu}\arrow[r,two heads,squiggly]&{\mkern-10mu}\end{tikzcd}}}
\begin{document}
$A\twoheads B$, $a {\scriptstyle \twoheads} v$, $l {\scriptscriptstyle \twoheads} g$
\end{document}

and I would to create a new symbol named \twoheads using \mathbin, or \mathrel, or other commands (\mathclap, etc.), why have I this error?

Undefined control sequence. [$A\twoheads]

What have I forgotten and what are the optimal solutions?

Follow-up or and edit: Looking the link of the very good user @campa I have tried to use the answer of brillant user @David Carlisle \newcommand and tikzcd

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz-cd,tikz}
\usetikzlibrary{decorations.pathmorphing}

\newcommand{\dotheobvious}[1]{#1}
\dotheobvious{\[\mathbin{\begin{tikzcd}[column sep=1.2em,ampersand replacement=\&]{\mkern-10mu}\arrow[r,two heads,squiggly]&{\mkern-10mu}\end{tikzcd}}\]}

\begin{document}
$A\dotheobvious{B}$
\end{document}

1. I have not understood the reason to put [1]{#1};

2. I have understood the reason of ampersand replacement=\&;

3. If I had put \DeclareMathCommand instead of \DeclareRobustCommand why I must to add the \[...\] considering that tikz-cd, in particular, work in text-mode and in math-mode?

4. At the end the last code not work.

Addendum: 2021/07/13: I have followed the suggestion of the @egreg but I have not found a correct solution. This is the MWE with the errors:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz-cd,tikz}
\usetikzlibrary{decorations.pathmorphing}

\newcommand{\squee}[1]{#1}
\squee{\mathbin{\begin{tikzcd}[column sep=1.2em,ampersand replacement=\&]{\mkern-10mu}\arrow[r,two heads,squiggly]\&{\mkern-10mu}\end{tikzcd}}}

\begin{document}
$A\squee B$
\end{document}
1
  • 2
    The error is in \pgf@matrix@last@nextcell@options being undefined. When a TikZ matrix (such as a tikzcd environment) is in the argument to another command you must use ampersand replacement=\& and \& in place of &. Adding \[...\] is of course wrong.
    – egreg
    Jul 11, 2021 at 10:25

1 Answer 1

1

I'm not quite sure how to answer after the bunch of edits, so I try to go step by step... in order to be concise I assume that all the following snippets start with the code

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz-cd,tikz}
\usetikzlibrary{decorations.pathmorphing}

The original question

\DeclareRobustCommand{\twoheads}{%
   \mathbin{%
      \begin{tikzcd}[column sep=1.2em]
      {\mkern-10mu}\arrow[r,two heads,squiggly]&{\mkern-10mu}
      \end{tikzcd}%
   }%
}
\begin{document}
$A\twoheads B$
\end{document}

fails for the reason explained in \newcommand and tikzcd : tikzcd changes catcodes, and that won't work if you put the code in a macro because catcodes are already frozen.


You then try David's code and write

\newcommand{\dotheobvious}[1]{#1}
\dotheobvious{\[\mathbin{\begin{tikzcd}[column sep=1.2em,ampersand replacement=\&]{\mkern-10mu}\arrow[r,two heads,squiggly]&{\mkern-10mu}\end{tikzcd}}\]}
\begin{document}
$A\dotheobvious{B}$
\end{document}

but I think you misunderstood his point: the macro \dotheobvious just uses its argument, but David's point was that using it and passing the tikzcd as macro argument freezes catcodes, thus leading to the error. However, he put the code inside the document, while you tried to do it in the preamble. Of course you get errors, because you are trying to typeset in the preamble.

The $A\dotheobvious{B}$ is just the same as $AB$.


Your third code suffers from the same problem. You have eliminated the \[ and \], but you are still trying to typeset in the preamble, and that will lead to errors.


Long story short: you want

\documentclass[twocolumn]{article}
\usepackage{amsmath}
\usepackage{tikz-cd,tikz}
\usetikzlibrary{decorations.pathmorphing}

\DeclareRobustCommand{\squee}{%
   \mathbin{%
      \begin{tikzcd}[column sep=1.2em,ampersand replacement=\&]
      {\mkern-10mu}\arrow[r,two heads,squiggly]\&{\mkern-10mu}
      \end{tikzcd}%
   }%
}

\begin{document}
Inline math $A\squee B$, or
\begin{align*}
A \squee B
\end{align*}
in display math.
\end{document}

enter image description here

2
  • Great campa :-)...as I can see the problem is the placement of the curly brackets within the command \mathbin. Why do they have to be placed by carving and putting a % comment? Always thankkkkkkkkkkkkkkkk youuuuuuuuuuuuuuuuuuuuuu.
    – Sebastiano
    Jul 14, 2021 at 13:57
  • 1
    @Sebastiano No, the % and the indenting of the code are not necessary here because we are in math mode anyway. I'm just used to write code like that for readability, and to protect line ends with comment signs.
    – campa
    Jul 14, 2021 at 14:02

You must log in to answer this question.

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