Say I have a macro that does something in math mode (say, change the mathalphabet). This macro, \somecommand
, takes one argument. I might want use is like this, feeding an empty argument into it: \(\somecommand{} +x\)
. I also want the plus sign before x
to be a unary operator.
I find that whether a plus or minus sign is considered unary or binary is, while clear in theory, not always obvious upon first inspection:
\documentclass{article}
\newcommand{\zeroArgCommand}{}
\newcommand{\oneArgCommand}[1]{}
\begin{document}
\noindent
\( +x\) \\ % unary: "+x"
\(\zeroArgCommand +x\) \\ % unary: "+x"
\(\zeroArgCommand{} +x\) \\ % binary: " + x"
\(\oneArgCommand{} +x\) \\ % unary: "+x"
\(\oneArgCommand{}{} +x\) \\ % binary: " + x"
\end{document}
In the light of this, I am wondering about the best way of defining a unary plus/minus sign.
Intuitively I would write \(\somecommand{} {+} x\)
or \(\somecommand{} {+ x}\)
, but I am wondering about expert opinion about a method that for sure works in all contexts.
This question was posed because in another thread, a user suggested that defining a \unaryplus
macro is clearer than \somecommand {+} x
. (I think what that commenter had in mind was that the reader might not be familiar with the arity of \somecommand
.)
Addendum: Is there a good way to define a macro \genuineunaryplus
that produces a +
but that in a context <before>\genuineunaryplus<after>
attaches to <after>
as a unary operator but has spacing to <before>
exactly as if the \genuineunaryplus
were not there? That is, a macro such that if the spacing between <B> <A>
is of length/type L, the expression <B>\genuineunaryplus<A>
will produce <B> +<A>
in the output, with the spacing between <B>
and +
exactly like the spacing would be between <B>
and <A>
if the \genuineunaryplus
were not there, namely L?
Answer idea for addendum: David Carlisle's comment below outlines a possible answer for this addendum.
\unaryplus x
where\newcommand{\unaryplus}{{+}}
?\mathord{+}
would be the answer, maybe buried in a macro. Using the same symbol for two different purposes is, in general, wrong. If your+
has always to behave as unary, then use a different name for it.