11

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.

  • 2
    i'm not surprised that the 12pt plus and minus have different widths, because the plus is in cmr12, which has a slightly narrowed aspect ratio, and the minus is scaled from cmsy10, since cmsy12 doesn't exist. but i am surprised that the 6pt widths are different, since both cmr6 and cmsy6 were among the "original 75" fonts defined with metafont. i will investigate further when time permits. – barbara beeton Apr 10 '17 at 21:19
  • 1
    \mathrlap{+}\phantom{-} ? – John Kormylo Apr 11 '17 at 3:12
  • 1. John, this is worse than my solution (see below), because it does not even bother to center the plus in the width of the minus. 2. Anyway, defining a symbol that is a minus with the width of a plus is not that hard (again, see below), but I don't like the drawbacks of redefining the minus character. Does anyone know whether it is instead possible to automatically modify the font files such that the width of the minus is changed, without redefining the minus character in latex? – wea0 Apr 18 '17 at 10:57
  • So, am I stuck with either having a - which does not possess the same width as +, or having a - that does not work in superscripts? Is there any way to define a new command which exhibits \mathbin spacing and simultaneously works when used as a superscript without having to be enclosed in parentheses? – wea0 Jul 29 '17 at 0:13
4

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.

| improve this answer | |
3

My attempt at a solution

\newlength{\widthplus}%
\newcommand{\minuspalette}[2]{\settowidth{\widthplus}{$#1+$}\mathbin{\makebox[\widthplus]{$#1#2$}}}%
\newcommand{\goodminus}{\mathpalette{\minuspalette}{\mathchar"2200}}% use \mathchar"2200 instead of - to avoid infinite recursion
{\catcode`-13\gdef-{\goodminus}}% make - active temporarily, so that it can be redefined
\mathcode`-"8000% make - active in math mode

I simply define a box that has the same width as a plus sign, but will be filled with a minus glyph. I do not shrink the symbol, only make its bounding box smaller. Via \mathpalette the definition is automatically correct for all math sizes. Then I make - active (but only temporarily), such that it can be redefined. And finally I make - permanently active in math-mode only (not to mess with text-mode hyphens), such that the new definition is used there.

What I don't like about it

Precisely because minus is now active in math-mode I can no longer use it as a single superscript or subscript, such as in $e^-$. It can now only be used if extra braces are put around the -. This is inconvenient and breaks many things in my existing project. Can anyone think of a solution which achieves the same effect as mine, but without this downside?

| improve this answer | |

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.