3

I want to define a command, call it \dotted, that adds a period at the right place next to certain other math characters*: to the right of function-like tokens, i.e. Ord and Op, and to the left of operator-like ones, i.e., Bin and Rel. This "meta-operator" should of course preserve the spacing of the original "operator". I came up with the following preliminary solution:

\documentclass{article}

\newcommand{\dottedord}[1]{\mathord{#1.}}
\newcommand{\dottedop}[1]{\mathop{#1.}}
\newcommand{\dottedbin}[1]{\mathbin{.#1}}
\newcommand{\dottedrel}[1]{\mathrel{.}#1}

\begin{document}
\[x = \dottedord{\sigma}(y) \qquad x = \sigma(y)\]
\[x = \dottedop{\log}(y) \qquad x = \log(y)\]
\[x = y \dottedbin{+} z \qquad x = y + z\]
% This case is special...
\[x \dottedrel{=} y \qquad x \mathrel{.=} y \qquad x = y\]
\end{document}

Equation output

How can I unify the four commands into one, dispatching on the type of the argument -- is there something like \mathchoice for math types? And why do I need to place the argument of \dottedrel outside \mathrel to get satisfactory spacing?

If there's something more fundamental to improve, I'd be happy to hear about that as well, of course.

*This is Julia's syntax for vectorization or "broadcasting", which can be applied to all functions and operators, but that's not really relevant here.

  • 1
    Possibly useful to you, if you will be applying the operator to a limited set of symbols: tex.stackexchange.com/a/370253/61644 – Davislor May 13 '19 at 21:39
  • 1
    Another approach might be to pass in the math class as an option, e.g. \broadcast[ord]{\sigma} and use \csname to generate the \mathord command from that. – Davislor May 13 '19 at 21:41
  • Although this is almost the opposite of what you asked, you could also define \arrayplus, \arrayassign, etc. – Davislor May 13 '19 at 22:37
  • @Davislor Thanks, but all of that is not really better than simply \mathord{\sigma.} or \mathop{.+}, in my opinion. The question you linked to seems to be asking the same thing, but -- though I don't really understand the expl3 stuff -- it still seems to check a fixed, given list of symbols... – phipsgabler May 14 '19 at 8:54
4

You can use the \binrel@ test defined by amsmath (precisely by amsbsy) in order to distinguish between \mathrel and \mathbin. If the test doesn't return one of these, you can define your own for \mathop and others; see this answer of mine for details about \binrel@, the idea for \phg@ordop is similar.

If you don't have to support operators with limits this should suffice.

\documentclass[a4paper]{article}
\usepackage{amsmath}
%\usepackage{amsbsy}% loaded by amsmath

\makeatletter
\DeclareRobustCommand{\dotted}[1]{%
  \begingroup
  \binrel@{#1}%
  \ifx\binrel@@\mathbin \mathbin{.{#1}}\else
  \ifx\binrel@@\mathrel \mathrel{.}#1\else
  \phg@ordop{#1}\fi\fi
  \endgroup
}
\def\phg@ordop#1{%
  \sbox\z@{\thinmuskip=0mu$#1a$}%
  \sbox\tw@{\thinmuskip=1000mu$#1a$}%
  \ifdim\wd\tw@>\wd\z@
    % operator
    \mathop{{#1}.}%
  \else
    #1.
  \fi
}
\makeatother

\begin{document}

\begin{gather*}
x = \dotted{\sigma}(y) \qquad x = \sigma(y) \\
x = \dotted{\log}(y) \qquad x = \log(y) \\
x = \dotted{\log} y \qquad x = \log y \\
x = y \dotted{+} z \qquad x = y + z \\
x \dotted{=} y \qquad x = y
\end{gather*}

\end{document}

enter image description here

| improve this answer | |
  • 1
    With even better spacing! This is great. – phipsgabler May 14 '19 at 11:59

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.