Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is it possible to make a symbol which is a % inside a square box? I would like to use it to represent the function f(n,m) = n-(n % m). As in n symbol m.

Or... is there a better idea for this notation?

share|improve this question

2 Answers 2

This should be a reasonable option:

enter image description here





    A = B \boxmod C _ {B \boxmod C}


What does it do? \mathbin makes it a binary operator like + is, \fboxsep0pt makes the following \fbox tight, and for \mathpalette see this amazing explanation by egreg.

Needed to say, if non boxed percent sign has no meaning, you can use directly that one: \newcommand*\opmod{\mathbin{\%}}. No boxing, so no need for \mathpalette in that case.

share|improve this answer
Thank you for this. For maths such as $i \boxmod r$ it seems too large however. Is it possible to shrink it a little (and maybe centre it too then)? –  Raphael Feb 14 '14 at 16:52
Here you go, I hope it works, can't test now. –  yo' Feb 14 '14 at 17:07
Thank you. If you make it \scalebox{0.6} then this doesn't look too bad for my particular document. Is the space to the left and right of the box also adjustable? –  Raphael Feb 14 '14 at 17:12
@Raphael Well, you mean inside or outside of the box? Inside -- see Heiko's answer which is more complicated but better. Outside -- it depends on what you want to do. It's a binary operator and should have a spacing as such. –  yo' Feb 14 '14 at 17:14
I meant outside but maybe you are right that the spacing is OK as it is. Thanks again. –  Raphael Feb 14 '14 at 17:15
up vote 17 down vote

This answer is a continuation of tohecz' answer.


Additionally the following issues are addressed:

  1. Square
  2. Rule width dependent on size.
  3. Rounded corners
  4. Boxed symbol is shrinked, it has the height of an uppercase letter and is put on the base line.
  5. Equal inner margin without additional side bearings and putting side bearings outside the box.


  1. The bounding box of % is not a square. Additional margins can be added to increase the frame to a square. Or the symbol can be resized. In this case the symbol is not far from a square, thus a little distorting does hurt less than different inner margins.

  2. A scale factor for the rule width adaptation is calculated by comparing the height of the symbol in normal size with the height of the symbol in the current size.

  3. Rounded corners can easily be made with tikz' line join=round.

  4. Shrinking and moving a box can be easily done with \resizebox, \scalebox and \raisebox. The dimensions of the box are available inside the arguments of these commands through \width, \height, ...

  5. Getting rid of the side bearings is the tough part, because TeX does not know the size of the side bearings. It uses only the official glyph bounding box that includes the side bearings.

    LuaTeX could be tried to look into the internal font data. With some luck, the size of the side bearings can be obtained.

The following file can be used to find the sizes for the side bearings for the different math sizes manually1:


      \kern#2\wd0 %
      \kern#3\wd0 %
    \copy0 %
%  \vbox{\baselineskip=0pt \lineskip=0pt \def\,{\vskip.1pt}%

  \test {.065} {.07}
        {.065} {.07}
        {.07}  {.075}
        {.085} {.09}

Side bearings

The red bars indicate the side bearings,the blue box would be the character's visual bounding box. The "official" bounding box is marked with black bars. The argument values of \test are the left and right side bearings for the four math styles. They are expressed as factor for the width of the character.

Now we can put the pieces together (see the result at the top of the answer):


  % remove side bearings
    \kern-#4\wd0 %
    \unhcopy0 %
    \kern-#5\wd0 %
  % resize to square
  % shrink and put on math axis
  % scale \fboxrule
  % add inner sep
        inner sep=\fboxsep,
        line width=\fboxrule,
        line join=round,

      {\%}   {.8}
      {.065} {.07}
      {.07}  {.075}
      {.085} {.09}%


  $ X \boxmod Y$\\
  $ r \boxmod i ^ {r \boxmod i ^ {r \boxmod i}}$\\
  $ n - (n \boxmod m)$


1 The font sizes are usually the same for \displaystyle and \textstyle. Thus it can be simplified by omitting the case \dislaystyle, unless more complicate math expressions are used instead of the percent symbol.

share|improve this answer
Thank you. Could you add an example of the simple expression $i \boxmod r$ to the answer so I can see what you have in mind please? I am worried \boxmod is too large at the moment to look good in a maths formula. –  Raphael Feb 14 '14 at 17:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.