8

If I have a macro \Name that expands to a macro name, I can use that with \csname or the various tools from etoolbox. Can I do the opposite, i.e., get the name of a macro?

My use case is the following: I want to write a command \Mtx that produces a smallmatrix (from mathtools). An optional argument should take a delimiter size, and if it is not provided, the size is determined automatically (this deviates slightly from the syntax mathtools' \DeclarePairedDelimiterX imposes for the commands it defines).

I tried

\DeclareDocumentCommand{\Mtx}{om}{
    \IfNoValueTF{#1}{
        \left(\begin{smallmatrix}#2\end{smallmatrix}\right)
    }{
        #1(\begin{smallmatrix}#2\end{smallmatrix}#1)
    }
}

However, I thought it might be advisable to have a provided \big for #1 replaced by \bigl and \bigr, respectively. Is it advisable?

So, I thought, if there is a command \GetName such that \GetName{\command} expands to command, then I could do:

\DeclareDocumentCommand{\Mtx}{om}{
    \IfNoValueTF{#1}{
        \left(\begin{smallmatrix}#2\end{smallmatrix}\right)
    }{
        \csuse{\GetName{#1}l}(\begin{smallmatrix}#2\end{smallmatrix}\csuse{\GetName{#1}r})
    }
}

Is that possible? I guess someone will point at lacking error handling. But now I'm curious!

2 Answers 2

9

It's \cs_to_str:N.

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentCommand{\Mtx}{om}
  {
    \IfNoValueTF{#1}
      {
        \left(\begin{smallmatrix}#2\end{smallmatrix}\right)
      }
      {
        \use:c {\cs_to_str:N #1 l}(
        \begin{smallmatrix}#2\end{smallmatrix}
        \use:c {\cs_to_str:N #1 r})
      }
  }

\ExplSyntaxOff

\begin{document}

\[
\Mtx{a & b \\ c & d}
\quad
\Mtx[\big]{a & b \\ c & d}
\quad
\Mtx[\Big]{a & b \\ c & d}
\quad
\Mtx[\bigg]{a & b \\ c & d}
\quad
\Mtx[\Bigg]{a & b \\ c & d}
\]

\end{document}

Don't use \DeclareDocumentCommand, unless you know what you're doing.

enter image description here

1
  • 1
    Thank you also for the remark on \Declare vs \NewDocumentCommand.
    – Bubaya
    Feb 1 at 10:55
8

In LuaTeX, we have \csstring primitive. If #1 is \big, then

\csname \csstring #1l\endcsname

is \bigl.

You must log in to answer this question.

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