# Problem with new command

Since I'm taking a class in linear algebra, I often need to write M(mxn, K) and I find it annoying.

I tried to define a command: \newcommand{\M}[3]{M(#1 \times #2, #3)} but this gives a strange output:

Hoe can I solve this problem? Thanks in advance

• The definition looks ok so there is something else going on. That's why we always ask for a MWE... – campa Oct 28 '20 at 8:47

If you define

\newcommand{\M}[3]{M(#1 \times #2, #3)}


then the input syntax should be

\M{2}{2}{\mathbb{R}}


and not

\M{2,2,\mathbb{R}}


which indeed reproduces the strange output and, possibly, some error messages.

Example:

\documentclass{article}
\usepackage{amsmath,amssymb}

\newcommand{\RR}{\mathbb{R}} % the reals

\newcommand{\M}[3]{M(#1\times #2,#3)}

\begin{document}

Good syntax \verb|\M{2}{2}{\RR}| yields $A\in\M{2}{2}{\RR}$

Bad syntax \verb|\M{2,2,\RR}| yields $A\in\M{2,2,\RR}$

\end{document}


Can you get the, admittedly easier, syntax to work? Yes.

\documentclass{article}
\usepackage{amsmath,amssymb}
%\usepackage{xparse}% not needed for LaTeX 2020-10-01 or later

\newcommand{\RR}{\mathbb{R}} % the reals

\NewDocumentCommand{\M}{>{\SplitArgument{2}{,}}m}{\MLONG#1}
\NewDocumentCommand{\MLONG}{mmm}{M(#1\times #2,#3)}

\begin{document}

Good new syntax \verb|\M{2,2,\RR}| yields $A\in\M{2,2,\RR}$

\end{document}


Where's the secret? The preprocessor \SplitArgument{2}{,} tells LaTeX to read the argument, which should contain two commas, and pass #1 as

{<a>}{<b>}{<c>}


when the argument is <a>,<b>,<c>. So we can feed the transformed argument to \MLONG that reads three standard arguments.

• To be extremely nit-picky amsmath isn't really necessary, is it? – campa Oct 28 '20 at 9:27
• xparse to the rescue! I really need to look into this more. – Alenanno Oct 28 '20 at 9:28
• @Alenanno I added a bit of explanation – egreg Oct 28 '20 at 9:38
• @egreg Thanks, I had already +1'ed anyways :D Just a question: even if the package doesn't need to be specified explicitly anymore, is the documentation still valid for knowing about the various options, commands, etc? – Alenanno Oct 28 '20 at 9:41
• @Alenanno Still a bit vaguely: section 1.8. – egreg Oct 28 '20 at 10:08

As Campa said, you should always provide a MWE

For the moment, have a look at

\documentclass[12pt]{article}
\usepackage[a4paper]{geometry}
\usepackage{mathtools,amsfonts}
\newcommand{\M}[3]{\ensuremath{M(#1 \times #2, #3)}}
\begin{document}
\M{1}{2}{\mathbb{R}}
\end{document}


giving

as expected.

• Is there any real reason for \ensuremath? – egreg Oct 28 '20 at 9:13
• @egreg Not really. But the OP did not give any context. So I guessed. – Denis Oct 28 '20 at 9:16

As the other answers identified, the problem was that you need to write \M{2}{2}{R} to use your macro in the document.

It's understandable that you'd like to avoid writing it that way, since curly braces are difficult to type. egreg showed a way to reduce (but not eliminate) curly braces with modern LaTeX.

If you want to use syntax similar to the way you wrote it in the text of your question, another approach is the old TeX-style definition

\def\M(#1x#2,#3){M(#1 \times #2, \mathbb{#3})}


Then you can write \M(2x2,R) in your document.