2

I would like to define a macro in LaTeX, say \mymap, which on empty input just returns the function name, i.e., "mymap"; and on input "a" returns (the name of) the evaluation of the function in "a", i.e. "mymap(a)".

To this end I tried the following macro definition:

\usepackage{xifthen}
\newcommand{\mymap}[1]{
     \ifthenelse{\isempty{#1}}
     {\operatorname{mymap}}
     {\operatorname{mymap}(#1)}
}

The problem is that (following the LaTeX convention) whenever I write the macro name alone, LaTeX takes the following non-empty character as argument. For example if I code:

Let \mymap be a map

the result is:

Let mymap(b)e a map

instead of the desired:

Let mymap be a map

So my questions are:

  1. Is there a way to force a LaTeX macro to take arguments only when they are bracketed?
  2. What would be the best way to obtain a macro with the desired behavior?
  • But isn't typing \mymath(b) easier? – egreg Oct 25 '19 at 15:30
4

Use an optional argument.

However, spaces get tricky. Here I use \xspace, which I would prefer not using.

\documentclass{article}
\usepackage{xifthen,amsmath,xspace}
\newcommand{\mymap}[1][]{%
     \ifthenelse{\isempty{#1}}%
     {\ensuremath{\operatorname{mymap}}\xspace}
     {\ensuremath{\operatorname{mymap}(#1)}}%
}
\begin{document}
\scshape
Let \mymap be a map, whereas \mymap[x] shows its argument.
\end{document}

enter image description here

Without \xspace, you are forced to use the syntax \mymap{} or \mymap[] to achieve the function-name case.

David comments that, to his mind, it is preferable to force the specification of math mode explicitly, in which case the space problem goes away and the need for \ensuremath is alleviated:

\documentclass{article}
\usepackage{xifthen,amsmath}
\newcommand{\mymap}[1][]{%
     \ifthenelse{\isempty{#1}}%
     {\operatorname{mymap}}
     {\operatorname{mymap}(#1)}%
}
\begin{document}
\scshape
Let $\mymap$ be a map, whereas $\mymap[x]$ shows its argument.
\end{document}
|improve this answer|||||
  • if you didn't use \ensuremath (which is a relatively evil command) the input would be $\mymap$ and then there would be no issue with spaces, and you wouldn't be tempted to use \xspace (which is a relatively evil command) – David Carlisle Oct 25 '19 at 13:32
  • @DavidCarlisle While I agree about the "evil"ness of \ensuremath and \xspace, I used the former because the OP did not specify which mode (text/math) it might be used in. Your alternative is perhaps the best (I will note it in an edit), but it will force the OP to type two extra chars, which may seem undesirable to the OP. – Steven B. Segletes Oct 25 '19 at 13:39
  • no need to specify text or math, it was implicit in the example: the original could only be math (as \operatorname is a math command). – David Carlisle Oct 25 '19 at 13:45
  • @DavidCarlisle What I meant was, it was not clear the context in which the OP wished to use it: always as an inline text notation, or as part of a larger math equation. – Steven B. Segletes Oct 25 '19 at 13:49
  • well you get +1 anyway for using a [] optional argument not an xparse g argument (which is an absolutely evil construct:-) – David Carlisle Oct 25 '19 at 13:51

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.