2

I redefined the \boxplus and \boxminus commands. But for some reasons, the + sign inside the \boxplus is too large while the - sign in the \boxminus has he correct size. How should I modify the macros to have the two symbols of the right size?

Note that I am also using \boxdot in my document and the box for \boxdot is smaller than that of \boxplus.

enter image description here

\documentclass{article}

\usepackage{amssymb,stmaryrd}


\makeatletter 
\newcommand*{\b@xplus}[1][+]{\ooalign{%
    $\m@th\vcenter{\hbox{$\m@th#1$}}$\cr%
    \hidewidth$\m@th\boxempty$\hidewidth\cr}} 
\renewcommand*{\boxplus}{\mathbin{\b@xplus}} 
\renewcommand*{\boxminus}{\mathbin{\b@xplus[-]}} 
\makeatother

\begin{document}

$a \boxplus b$  % incorrect size

$a \boxminus b$ % correct size

\end{document}
  • 1
    Off topic: why don't you just use \scalebox on normal \boxplus and \boxminus? – user156344 Mar 31 '19 at 8:04
  • What's the reason for the redefinition? As far as I can see, stmaryrd's \boxplus has the same dimensions as +. – egreg Mar 31 '19 at 12:46
  • I am also using \boxdot in my document and the box for \boxdot is smaller than that of \boxplus. (The box used for \boxdot appears to be the same as \boxempty.) – user94293 Mar 31 '19 at 13:27
  • @user94293 -- you should add that reason to the question. – barbara beeton Mar 31 '19 at 14:06
  • 1
    amssymb has all three, \boxplus, \boxminus, and \boxdot, and all three should be the same size, so no scaling should be necessary. – barbara beeton May 21 at 5:00
2

The problem is that the plus sign overshoots the minus sign, while both have the same width, as far as TeX is concerned. The \boxempty symbol of stmaryrd has the same width as both.

You can work around the issue by using a slightly smaller plus, raised by a small amount so it still sits well with respect to the formula axis.

enter image description here

The gray line in the picture shows the formula axis (xcolor is used just for this). The final line shows the overshooting.

\documentclass{article}
\usepackage{amsmath,stmaryrd}
\usepackage{graphicx,xcolor}

\makeatletter
\newcommand{\reducedplus}{\mathpalette\reduced@plus\relax}
\newcommand{\reduced@plus}[2]{%
  \sbox6{$\m@th#1+$}%
  \sbox8{\scalebox{0.875}{\copy6}}%
  \dimen@=\dimexpr(\wd6-\wd8)/3\relax
  \raisebox{\dimen@}{\box8}%
}
\newcommand{\boxoperation}[2][\mathbin]{%
  #1{\mathpalette\box@operation{#2}}%
}
\newcommand{\box@operation}[2]{%
  \ooalign{$\m@th#1\boxempty$\cr\hidewidth$\m@th#1#2$\hidewidth\cr}%
}
\newcommand{\boxplus}{\boxoperation{\reducedplus}}
\newcommand{\boxminus}{\boxoperation{-}}

\begin{document}

\indent\rlap{\color{black!10}$\frac{\hspace{1cm}}{}$}%
$a\boxplus b\boxminus c$

\indent\rlap{\color{black!10}$\scriptstyle\frac{\hspace{1cm}}{}$}%
$\scriptstyle a\boxplus b\boxminus c$

\indent\rlap{\color{black!10}$\scriptscriptstyle\frac{\hspace{1cm}}{}$}%
$\scriptscriptstyle a\boxplus b\boxminus c$

\mbox{}%
{\ooalign{\textcolor{red}{$+$}\cr$-$\cr}}
{\ooalign{\textcolor{red}{$-$}\cr$+$\cr}}
{\ooalign{$+$\cr\textcolor{red}{$-$}\cr}}

\end{document}

On the other hand, you could more simply use the symbols provided by amssymb, which is essentially the same, but simpler, than wipet's answer.

enter image description here

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

\let\amssymbboxplus\boxplus
\let\amssymbboxminus\boxminus

\renewcommand{\boxplus}{\mathbin{\mathop\amssymbboxplus}}
\renewcommand{\boxminus}{\mathbin{\mathop\amssymbboxminus}}

\begin{document}

$a+b-c$

$a\boxplus b\boxminus c$

\end{document}
| improve this answer | |
1

As I said in my comment, you have fallen into two bugs caused by someone else. First bug is in CM fonts, where the minus shape from cmsy10 is smaller than plus shape from cmr10. Second bug is in AMS fonts, where the symbols boxplus, boxminus from msam10 are not centered to math axis. If you use another math fonts (for example Unicode Latin Modern Math) then both bugs are corrected.

If you insist to use these fonts then you can do following:

\mathchardef\xboxplus=\numexpr \boxplus-"2000\relax
\def\boxplus{\mathbin{\mathop{\xboxplus}}}
\mathchardef\xboxminus=\numexpr \boxminus-"2000\relax
\def\boxminus{\mathbin{\mathop{\xboxminus}}}

test: $a + b \boxplus c \boxminus d$

I subtract "2000 from mathcode of \boxplus and \boxminus in order to get math Ord charcter (they are declared as math Bin). I need to do this conversion in order to use these characters in the context \mathop{Ord} which does centering to math axis (appendix G od TeXbook, paragraph 13). Finally, I re-define \boxplus and \boxminus as math Bin object.

The subtraction is necessary as shown from the following code and from the picture with the result.

\mathchardef\xboxplus=\numexpr \boxplus-"2000\relax
\def\boxplusEgreg{\mathbin{\mathop{\boxplus}}}
\def\boxplusWipet{\mathbin{\mathop{\xboxplus}}}

$a + b \boxplusWipet c \boxplusEgreg d$

boxplus

| improve this answer | |
  • The subtraction is not necessary. – egreg May 21 at 10:44
  • @egreg Substraction is necessery as shown in my actual version of the answer. Or, I didn't understand something. – wipet May 21 at 11:58
  • Thank you @wipet! Or equivalently, \renewcommand*{\boxplus}{\mathbin{\mathop{\mathchar"0401}}} – user94293 May 21 at 12:33
  • @wipet Your error is in the braces: it should be \mathbin{\mathop\xboxplus}}, which doesn't create a subformula. See the last part of my answer. – egreg May 21 at 12:41
  • Or equivalently, \renewcommand*{\boxplus}{\mathbin{\mathop\mathchar"2401}} – user94293 May 21 at 12:44

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.