# Defining a new command name in the correct way

I'd like to define a short command name \gm{a,b} that would mean in LaTeX

\overset{a\to b}\longrightarrow


I tried on my own. The below code only works for \gm{a} (or \gm{b} if changing a into b)

\newcommand{\gm}[1]{\overset{#1\to b}\longrightarrow}


but I can't make it work for both a and b, such that \gm{a,b} would work.

-
Welcome to TeX.SX! A tip: If you indent lines by 4 spaces or enclose words in backticks , they'll be marked as code, as can be seen in my edit. You can also highlight the code and click the "code" button (with "{}" on it). –  Adam Liter Apr 26 at 3:14
You should use \xrightarrow{a\to b} in the first place (requires amsmath). –  egreg Apr 26 at 9:47
@AdamLiter, thanks for informing me. @egreg, thanks. I have never heard about it before. It's simpler than what I have defined. Do you know what the name of command is in this case, if it were a "not" in the middle of the arrow? I mean, for example \rightarrow and \nrightarrow. –  AjmalW Apr 26 at 18:20

If you want to parse over a comma you could do something like the following

\makeatletter
\def\my@gm#1,#2\@nil{\overset{#1\to#2}\longrightarrow}
\newcommand\gm[1]{\my@gm#1\@nil}
\makeatother


This will, of course, depend upon you remembering to use a comma. Omitting the comma will result in an error.

-

While you can make the \gm{a,b} syntax work, as the answer by A. Ellett shows, I think that a first thing to realise is that this syntax goes against a basic mechanism of TeX, namely to group in first instance every list of tokens enclosed in braces (and itself balanced with respect to braces) as a unit. This means that initially a,b will be considered as a unit, which is not what you want. The reason that one can get around this, is that once used for grouping, the braces disappear (the unit recognised is not {a,b}) so that by placing them in a different context and re-parsing (in this second time matching on the comma), a different subdivision can result.

So the syntax you propose to use can only be handled by double parsing (and use of an auxiliary macro for that), and it therefore not a standard way to pass arguments. You can make it work, but it will not look like the syntax of other macro calls, and therefore tend to confuse any human reading the TeX source using it; in the end probably yourself as well.


\newcommand\gm[2]{\overset{{#1}\to{#2}}\longrightarrow}


This requires you to type one more character on each call that the syntax you propose; not a huge price to pay for better readability.

Basic TeX does give you more flexibility of calling syntax, allowing separation of arguments by other things than braces. So it allows delimiting arguments by parentheses and commas (or indeed any characters except braces) as in the calling syntax \gm(a,b), after defining

\def\gm(#1,#2){\overset{{#1}\to{#2}}\longrightarrow}


The characters (, ,, ) are now required in each call, and TeX will scan ahead until finding them, making anything in between the arguments to \gm (but since there is no argument before (, that symbol must immediately follow \gm, which his probably a good thing because it allows warning about calls not using the right syntax). Note that the mechanism is quite primitive and greedy; if you write \gm(a,b,c) the it will not complain about too many arguments, but rather pass a and b,c as two arguments to \gm. Also note that braces can still be used for grouping: \gm({a,b},c) will prevent the first comma from acting as separator, and pass a,b and c as two arguments to \gm (note the vanishing of the braces once the have served). I personally like using such alternative syntax now and then if it is suggestive (you might even opt for \gm(a->b) here). But don't overdo it, since mixing input syntaxes can be confusing.

A final word about my writing {#1} and {#2} in the above definition rather than just #1 and #2. This is a good habit that costs little (it only lengthens the definition, not the calls) and prevents surprises of arguments "falling apart" inside the macros definition (although that is probably not an issue in this particular definition). Remember that TeX will drop any outer braces used in the macro call once the call is parsed, so even if you wrote braces around arguments in the call, the values substituted for #1 and #2 will not contain those.

-
I generally agree with what you've posted (and +1 for that). There are plenty of contexts in LaTeX where what you recommend is flaunted and this convention of reparsing an argument to extract further components is used. Such alternate syntax can improve readability: consider the syntax of pstricks, tikz`, or any of the key parsing packages. I would not generally encourage someone to redefine how arguments are handled; occasionally the semantics can make an alternate argument parsing syntax more desirable; sometimes it's difficult to read a string of concatenated bracketed arguments. –  A.Ellett Apr 26 at 14:23