5

I would like to define custom text operators in math mode, such as "if", "then", "catch". These operators should always be preceded and succeeded by some space. Surrounding my operators with \mathrel brings me already quite close to what I want to achieve; however, when there is an equals sign (=) before an operator, there is no space between the two.

MWE:

\documentclass{minimal} \usepackage{amsmath} \begin{document} \newcommand{\relname}[1]{\mathrel{\operatorname{#1}}} \newcommand{\ite}[3]{\relname{if} #1 \relname{then} #2 \relname{else} #3} $\ite{a}{b}{c}$ (good) $x = \ite{a}{b}{c}$ (bad --- insufficient space after "=") % https://tex.stackexchange.com/questions/680197/no-spacing-after-binary-operator-after-math-operator \renewcommand{\relname}[1]{\mathop{}\!\mathrel{\operatorname{#1}}} $\ite{a}{b}{c}$ (bad --- too much space before "if") $x = \ite{a}{b}{c}$ (good) % https://tex.stackexchange.com/questions/46402/defining-a-prefix-operator-with-space-before-its-argument \renewcommand{\relname}[1]{\mathinner{\operatorname{#1}}} $\ite{a}{b}{c}$ (too cramped) $x = \ite{a}{b}{c}$ (too cramped) \end{document} 

Operator spacing.

2 Answers 2

4

Your problem is that the composed math text "if ... then ... else" is not encapsulated to {...}, i.e. its spacing with neighboring objects can be bad.

Simply, put the body od your \ite macro into {...}:

\def\relname#1{\mathrel{\rm#1}} \def\ite#1#2#3{{\relname{if} #1 \relname{then} #1 \relname{else} #3}} $\ite{a}{b}{c}$ (good) $x = \ite{a}{b}{c}$ (good) \bye 

Alternatively define:

\def\ite#1#2#3{\mathopen{}\relname{if} #1 \relname{then} #1 \relname{else} #3\mathclose{}} 

if you want to allow line breaking inside the formula.

5
  • Thank you for teaching me about using { ... } for grouping! Do you know a nice resource where I can read more about this? Commented May 12 at 15:02
  • (Also thanks for showing a plain TeX solution to this problem.) Commented May 12 at 15:20
  • I just found that {...} prevents line breaking inside the brackets completely (source). This is very unfortunate, because it produces overfull lines. Do you have any ideas how to adapt your solution to allow line breaks? Commented May 14 at 7:47
  • @MichaelF OK, I'v added alternative solution. Commented May 14 at 17:15
  • thanks a lot for your \mathopen{}...\mathclose{} solution! It is symmetric, it is beautiful ... and it works perfectly. Thanks again for following up on this. Commented May 19 at 14:14
4

You shouldn't use \operatorname, which is not a substitute for \mathrm, see What's the difference between \mathrm and \operatorname?

The next problem is that TeX never adds spaces between two consecutive relation symbols, more precisely, Rel atoms. You can cure it by adding a separator.

I choose \mathclose{}, because TeX adds no space between Rel and Close, but adds the standard space between Close and Rel.

\documentclass{article} \usepackage{amsmath} \newcommand{\relname}[1]{\mathrel{\mathrm{#1}}} \newcommand{\rsep}{\mathclose{}} \newcommand{\ite}[3]{\relname{if} #1 \relname{then} #2 \relname{else} #3} \begin{document} $\ite{a}{b}{c}$ (good) $x = \rsep \ite{a}{b}{c}$ (good) $\ite{a}{b}{c}$ (good) $x = \rsep \ite{a}{b}{c}$ (good) \end{document} 

output

You shouldn't use minimal for examples. It's not meant for this, see Why should the minimal class be avoided?

8
  • Just in case the text and math fonts employed in the document have distinct looks, might it be preferable to use \textup instead of \mathrm to render "if", "then", and "else"? Commented May 12 at 14:27
  • Thank you for clarifying the use of minimal. Even if I understand that there is a difference between mathrm and operatorname, would you care to explain why mathrm is preferable here? Commented May 12 at 15:15
  • Regarding your rsep solution, I do not like to introduce additional commands if it's not absolutely necessary. wipet's solution solves the problem without additional commands, so I went with that. Commented May 12 at 15:19
  • @Mico, thanks for your suggestion, but in all document classes I use, I do not see any difference between \textup and \mathrm. Commented May 12 at 15:21
  • 1
    @MichaelF I think that you should type x=(\ite{a}{b}{c}) or the formula would be very ambiguous. If you want to adopt wipet's solution, remember that \rm has been deprecated in LaTeX for 30 years. Commented May 12 at 16:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.