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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityIs 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[2]{\mathbin{{\fboxsep0pt\fbox{$#1\%$}}}}
%\newcommand*\boxmod{\mathpalette\boxmodpalette{}}
\usepackage{graphicx}
\newcommand*\boxmodpalette[2]{\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.
This answer is a continuation of tohecz' answer.
Additionally the following issues are addressed:
Discussion:
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.
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.
Rounded corners can easily be made with tikz
' line join=round
.
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
, ...
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@}[3]{%
\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}[8]{%
% \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@}[5]{%
\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
% add inner sep
\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}[8]{%
\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.