[Tex/LaTex] Defining operator symbols

amsmathsymbols

I want to replace the circle in the symbol \circledast by a square. But i did not find the right command. It seems that the packages stmaryrd would help. But the package usually re-defines other basic symbols which is not desired.

The amssymb package seems not define such a symbol. Is there anyone know how to define some command like \boxdast or \squaredast? Or how to avoid other changes by introducing packages that already defines \boxdast or \squaredast?

Best Answer

The following example composes \boxast from \Box and * and only needs package amssymb because of \Box:

\documentclass{article}
\usepackage{amssymb}

\makeatletter
\providecommand*{\boxast}{%
  \mathbin{% as \boxplus and \boxtimes
    \mathpalette\@boxit{*}%
  }%
}
\newcommand*{\@boxit}[2]{%
  % #1: math style (\displaystyle, \textstyle, ...)
  % #2: symbol to be boxed that is centered around the math axis
  \sbox0{$\m@th#1\Box$}%
  % Manual correction for font bounding boxes:
  \ifx#1\displaystyle \ht0=\dimexpr\ht0+.05ex\relax \fi
  \ifx#1\textstyle \ht0=\dimexpr\ht0+.05ex\relax \fi
  \ifx#1\scriptstyle \ht0=\dimexpr\ht0+.04ex\relax \fi
  \ifx#1\scriptscriptstyle \ht0=\dimexpr\ht0+.065ex\relax \fi
  \sbox2{$#1\vcenter{}$}% \ht2 is positionn of math axis
  \rlap{%
    \hbox to \wd0{%
      \hfill
      \raisebox{%
        \dimexpr.5\dimexpr\ht0+\dp0\relax-\ht2\relax
      }{$\m@th#1#2$}%
      \hfill
    }%
  }%
  \Box
}
\makeatother

\begin{document}
$A \boxplus B \boxast C^{D \boxast E^{F \boxast G}}$
\end{document}

Result

Remarks:

  • Macro \@boxit assumes that the symbol to be boxed is centered around the math axis as some of the typical boxed symbols are (plus, minus, times, ast). Then the symbol is placed in the middle of the box. (There might be tiny deviations because glyph side bearings are not taken into account.)

  • The reason for \mathpalette is to get the actual math style in order to match the size of the symbol according to the current math style.

  • \m@th removes \mathsurround, because it should add space around the formula, not within, if \mathsurround is set.

  • Update: The font bounding box of \Box is a little too small in the height, therefore I have added a manual correction.

    The following file shows the font bounding boxes for \Box, first the unmodified font bounding box (red), then the corrected bounding box (green). (The larger width of the font bounding box does not matter, because the white space left and right seems to be the same and the symbol remains centered.)

    \documentclass{article}
    \usepackage{amssymb}
    \usepackage{color}
    
    \begin{document}
    \setlength{\fboxsep}{0pt}
    \setlength{\fboxrule}{.1pt}
    \newcommand*{\test}[2]{%
      \fbox{\color{red}$\csname #1style\endcsname\Box$}%
      \fbox{\color{green}%
        $\csname #1style\endcsname\Box$%
        \vphantom{\raisebox{#2}{$\csname #1style\endcsname\Box$}}%
      }%
    }
    \newcommand*{\test}[1]{%
      \fbox{\color{blue}$\csname #1style\endcsname\Box$}%
    }
    \test{display}
    \test{text}
    \test{script}
    \test{scriptscript}
    \end{document}
    

Font bounding boxes