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?

This should be a reasonable option: \documentclass{article}

%\newcommand*\boxmodpalette{\mathbin{{\fboxsep0pt\fbox{$#1\%$}}}}
%\newcommand*\boxmod{\mathpalette\boxmodpalette{}}

\usepackage{graphicx}
\newcommand*\boxmodpalette{\mathbin{\raisebox{0.1\height}{\scalebox{0.8}{{\fboxsep0pt\fbox{$#1\%$}}}}}}
\newcommand*\boxmod{\mathpalette\boxmodpalette{}}

\begin{document}

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

\end{document}


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.

• 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 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.

Discussion:

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:

\documentclass{standalone}
\usepackage{color}

\makeatletter
\newcommand*{\test@}{%
\hbox{%
\sbox0{$\color{yellow}#1\%\m@th$}%
\vrule
\rlap{%
\color{red}%
\rule[-\dp0]{\wd0}{\dimexpr\ht0+\dp0\relax}%
}%
\rlap{%
\color{blue}%
\kern#2\wd0 %
\rule[-\dp0]{\dimexpr\wd0-#2\wd0-#3\wd0\relax}{\dimexpr\ht0+\dp0\relax}%
\kern#3\wd0 %
}%
\copy0 %
\vrule
}%
}
\newcommand*{\test}{%
%  \vbox{\baselineskip=0pt \lineskip=0pt \def\,{\vskip.1pt}%
\mbox{%
\test@\displaystyle{#1}{#2}\,%
\test@\textstyle{#3}{#4}\,%
\test@\scriptstyle{#5}{#6}\,%
\test@\scriptscriptstyle{#7}{#8}%
}%
}
\makeatother

\begin{document}
\test {.065} {.07}
{.065} {.07}
{.07}  {.075}
{.085} {.09}
\end{document} 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):

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\makeatletter
\newcommand*{\squareboxed@}{%
\sbox0{$#1#2\m@th$}%
% remove side bearings
\sbox0{%
\kern-#4\wd0 %
\unhcopy0 %
\kern-#5\wd0 %
}%
% resize to square
\dimen@=\dimexpr\ht0+\dp0-\wd0\relax
\ifdim\dimen@=\z@
\else
\sbox0{\resizebox{\totalheight}{\height}{\unhcopy0}}%
\fi
% shrink and put on math axis
\sbox2{$#1\vcenter{}$}%
\sbox0{%
\raisebox{\ht2}{%
\scalebox{#3}{%
\raisebox{.5\dimexpr\dp0-\ht0\relax}{\unhcopy0}%
}%
}%
}%
% scale \fboxrule
\sbox2{$1$}%
\sbox4{$#11$}%
\fboxrule=\dimexpr\fboxrule*\ht4/\ht2\relax
\fboxsep=.5\dimexpr#4\wd0+#5\wd0\relax
\kern\fboxsep
\sbox2{$#1X$}%
\resizebox{!}{\ht2}{%
\raisebox{\depth}{%
\tikz[
inner sep=\fboxsep,
line width=\fboxrule,
line join=round,
baseline=(X.base),
]
\node[draw](X){\copy0}%
;%
}%
}%
\kern\fboxsep
}
\newcommand*{\squareboxed}{%
\mathchoice
{\squareboxed@\displaystyle{#1}{#2}{#3}{#4}}%
{\squareboxed@\textstyle{#1}{#2}{#3}{#4}}%
{\squareboxed@\scriptstyle{#1}{#2}{#5}{#6}}%
{\squareboxed@\scriptscriptstyle{#1}{#2}{#7}{#8}}%
}

\newcommand*{\boxmod}{%
\mathbin{%
\squareboxed
{\%}   {.8}
{.065} {.07}
{.07}  {.075}
{.085} {.09}%
}%
}
\makeatother

\begin{document}

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

\end{document}


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.

• 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