2

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:

enter image description here

Hoe can I solve this problem? Thanks in advance

1
  • 4
    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
9

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}

enter image description here

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}

enter image description here

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.

13
  • 1
    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
  • 1
    @Alenanno Still a bit vaguely: section 1.8. – egreg Oct 28 '20 at 10:08
3

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

enter image description here

as expected.

2
  • 1
    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
0

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.

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.