[Tex/LaTex] How to redefine the minus sign to be of the same width as plus

computer-modernfontslatin-modernmath-moderesize

The situation

In the standard Computer Modern (and also in Latin Modern) the math-mode minus and plus symbols are, in some font sizes, not of the same width. This causes slight misalignments in formulas. I would like to redefine the math-mode - to have the same width as +, in any font size.

The example

\documentclass[12pt]{standalone}%
%
\begin{document}%
\newlength{\test}%
\begin{tabular}{rll}%
textstyle & \fbox{$-$} & \settowidth{\test}{$-$}\the\test\\
textstyle & \fbox{$+$} & \settowidth{\test}{$+$}\the\test\\
scriptstyle & \fbox{$\scriptstyle-$} & \settowidth{\test}{$\scriptstyle-$}\the\test\\
scriptstyle & \fbox{$\scriptstyle+$} & \settowidth{\test}{$\scriptstyle+$}\the\test\\
scriptscriptstyle & \fbox{$\scriptscriptstyle-$} & \settowidth{\test}{$\scriptscriptstyle-$}\the\test\\
scriptscriptstyle & \fbox{$\scriptscriptstyle+$} & \settowidth{\test}{$\scriptscriptstyle+$}\the\test\\
\end{tabular}%
\end{document}%

compiled example

This shows that the minus is wider than the plus in both 12pt and 6pt math fonts, but has the same width in 8pt.

The solution?

I'll post one below, but I am not happy with it.

Best Answer

Note: I take a very different view of the original question in the following discussions and solutions. And, no, I do not like the idea of making + and/or - active in math mode just for alignment purposes.

This problem exists not only for Computer Modern/Latin Modern, and not only for the + and - pair

With the default Computer Modern family (or the Latin Modern family), the width difference problem is barely visible. However, with other math fonts such as newtxmath, newpxmath and mtpro2, this problem is much more visible and it goes beyond the + and - pair. The following MWE illustrates this point:

\documentclass{article}
\usepackage{mathtools}
% Latin Modern
%\usepackage{lmodern}
% newtxmath
\usepackage{newtxtext}
\usepackage{newtxmath}
% mtpro2
%\usepackage{newtxtext}
%\usepackage[lite]{mtpro2}
\begin{document}
Cases: $f(x) = \begin{cases}
                +1 & x>0 \\
                -1 & x=0 \\
                -1 & x\le0
               \end{cases}$

Stacked subscripts: $\displaystyle \sum_{\substack{i=0 \\ i>0}}$
\end{document}

bad alignment

We may now conclude that the width differences are by design. More importantly, the behavior is unpredictable across different math fonts and across different design/optical sizes. Clearly, any solutions that involve active +, -, = and so on would be ineffective and dangerous.

How can we get mono-width symbols?

Instead of making characters active (and possibly breaking other things), I propose two specially designed commands:

\tabularrel{<arg>}

and

\tabularbin{<arg>}

The names are inspired by “proportional versus tabular figures”. Each of the two commands essentially makes a box of fixed width according to some “standard symbol”, then it puts the actual symbol horizontally centered. Note that \mathpalette is needed to get the correct math style. Of course, this method of aligning tabular-like math can be applied to any math fonts.

Here is a MWE:

\documentclass{article}
\usepackage{mathtools}
% Latin Modern
%\usepackage{lmodern}
% newtxmath
\usepackage{newtxtext}
\usepackage{newtxmath}
% mtpro2
%\usepackage{newtxtext}
%\usepackage[lite]{mtpro2}

% Standard relation and binary symbols
\newcommand*\standardrel{<}% Change this to `=' for mtpro2
\newcommand*\standardbin{+}
% Tabular relation and binary symbols
\makeatletter
\newcommand*\tabularrel[1]{%
  \mathrel{\mathpalette{\@tabularsym\standardrel}{#1}}%
}
\newcommand*\tabularbin[1]{%
  \mathbin{\mathpalette{\@tabularsym\standardbin}{#1}}%
}
\newcommand*\@tabularsym[3]{%
  % #1: standard symbol
  % #2: math style
  % #3: user symbol
  \setbox\z@\hbox{$#2#1\m@th$}%
  \hbox to\wd\z@{\hss$#2#3\m@th$\hss}%
}
\makeatother

\begin{document}
Cases: $f(x) = \begin{cases}
                \tabularbin+1 & x\tabularrel>0 \\
                \tabularbin-1 & x\tabularrel=0 \\
                \tabularbin-1 & x\tabularrel\le0
               \end{cases}$

Stacked subscripts: $\displaystyle \sum_{\substack{i\tabularrel=0 \\ i\tabularrel>0}}$
\end{document}

Voilà!!!

good alignment

For most “one-liner” equations, there are no need for mono-width symbols. However, when typing inside tabular-like environments, e.g., tables, arrays, cases, matrices, etc., horizontal alignment may be crucial.