# Make a postfix operator with suppressable spacing?

I would like to create a postfix unary operator that normally leaves \medmuskip between the operand and postfix operator. It's easy enough to do this using, for example,

\newcommand*{\true}{\mskip\medmuskip\mathsf{true}}

so that x$A \true x$x yields

However, the extra twist is that I would like to use a group to suppress the spacing, just as is possible for binary operators (such as \otimes). In other words, x$A{\true}x$x should yield . Bonus points for making x$\true$x (note the absence of {}) yield .

Here is a skeleton to play with:

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

\newcommand*{\true}{\mskip\medmuskip\mathsf{true}}

\begin{document}
\begin{tabular}{ll}
\verb#x$A \true x$x#  & x$A \true x$x \\
\verb#x$A{\true}x$x# & x$A{\true}x$x \\
\verb#x$\true$x#    & x$\true$x
\end{tabular}
\end{document}
-
What do you think to use \DeclareMathOperator{\true}{true} or \DeclareMathOperator{\true}{\medspace true}. –  Sigur Jan 11 '13 at 22:07
@Sigur This inserts \thinmuskip, not \medmuskip –  egreg Jan 11 '13 at 22:23
Yes, this is almost, but not quite, what I am looking for. If I use \DeclareMathOperator and write x$A \true x$x, I get a space after the operator, i.e., it acts as a binary operator. I've edited my question to clarify what I am after. –  Henry DeYoung Jan 11 '13 at 22:23

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

\newcommand*{\true}{\relax\ifnum\lastnodetype>0 \mskip\medmuskip\fi\mathsf{true}}

\begin{document}
\begin{tabular}{ll}
\verb#x$A \true$x#  & x$A \true$x \\
\verb#x$A{\true}$x# & x$A{\true}$x \\
\verb#x$\true$x#    & x$\true$x
\end{tabular}
\end{document}
-
Thanks, this does just what I am looking for! I'd like to understand how this solution works, though. Could you add an explanation of \lastnodetype (or point me to a reference) and why \relax is needed? –  Henry DeYoung Jan 11 '13 at 22:29
it's an etex extension (so texdoc etex page 7 has the details) it tells you something about the previous thing on the curent list {} starts a new list and if you are at the start of a list it is -1 it would be 11 for glue or 15 for a math node etc you need \relax (or make \true robust) to stop bad expansion in a table cell when tex is looking for \omit (\multicolumn) –  David Carlisle Jan 11 '13 at 22:34
@HenryDeYoung Watch out that $\left(\true\right) will insert the space, but I think that you don't need such a strange concoction. Other than this, it's a very clever answer and I don't think one can do better (without LuaTeX). – egreg Jan 11 '13 at 22:42 Yes You need explicit {} as in$\left({\true}\right) to zap the space unfortunately almost all math constructs show as 15 so you can not distinguish the mathopen form \left( –  David Carlisle Jan 11 '13 at 22:52