# How to make contextual framed letters get proportional size automatically?

I am creating a question sheet using a method (I don't know the exact name, maybe bubble sheet format) as follows:

My question is how to improve the appearance such that the framed letters get proportional size based on the context. If the frame letter must work as an exponent, it must be smaller. If it works as a lower index in integral or sum, it also must be smaller.

Here is my code snippet after "normalization" :-)

\documentclass[dvipsnames,dvips,cmyk]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{bera}

\fboxsep=2pt
\fboxrule=0.8pt

\newcommand{\boxy}[1]{\ensuremath{\,\fbox{\;\,{\color{red}\bf#1}\,\;}\,}}

\newcommand{\ltr}[1]{{\color{red}\bf#1}}

\begin{document}
\noindent%
Each letter \ltr{A}, \ltr{B}, \ltr{C}, etc in the equations represents a numeral (from 0 to 9) or the minus sign ($-$).

$\int_{\boxy{A}}^{\boxy{B}}f(x)\,\textrm{d}x=\frac{1-\boxy{P}}{\boxy{Q}+\boxy{R}}$

$-2 x^{\boxy{XYZ}}-\boxy{W}\sqrt{\boxy{V}+1}$

$\sum_{i=\boxy{A}}^{\boxy{BC}}=\tan\left(\boxy{D}-\boxy{E}\right)$
\end{document}
-

use the \text command, it takes the correct font setting:

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\fboxsep=2pt
\fboxrule=0.8pt

\newcommand\boxy[1]{\text{\,\fbox{\;\,{\color{red}$\mathbf{#1}$}\,\;}\,}}

\newcommand{\ltr}[1]{{\color{red}\bf#1}}

\begin{document}
\noindent%
Each letter \ltr{A}, \ltr{B}, \ltr{C}, etc in the equations
represents a numeral (from 0 to 9) or the minus sign ($-$).
%
$\int\limits_{\boxy{A}}^{\boxy{B}}f(x)\,\textrm{d}x=\frac{1-\boxy{P}}{\boxy{Q}+\boxy{R}}$
%
$-2 x^{\boxy{XYZ}}-\boxy{W}\sqrt{\boxy{V}+1}$
%
$\sum_{i=\boxy{A}}^{\boxy{BC}}=\tan\left(\boxy{D}-\boxy{E}\right)$
\end{document}
-
Wow, I'm surprised! Why does this work? – Hendrik Vogt Dec 28 '10 at 8:58
@Hendrik: \text expands to \text@ which expands to a \mathchoice which uses \everymath to set the appropriate style. – TH. Dec 28 '10 at 9:03
@Hendrik: it uses the definitions from mathcoice: \let\f@size#2\selectfont – Herbert Dec 28 '10 at 9:18
@TH.: Ah, \everymath is something I'm not well-acquainted with. No wonder I didn't get it. – Hendrik Vogt Dec 28 '10 at 9:41
thanks for giving me the simplest or most compact (hopely) solution. – xport Dec 28 '10 at 10:11

You'll want to use \mathpalette: Replace your definition of \boxy with

\makeatletter
\newcommand{\boxy}[1]{\ensuremath{\mathpalette\boxy@helper{#1}}}
\newcommand{\boxy@helper}[2]{\,\fbox{\;\,{\color{red}$\m@th#1\mathbf{#2}$}\,\;}\,}
\makeatother

Note that I made another change to your macro: I use \$s and \mathbf in the \fbox, and I added \m@th to ensure that you don't get additional horizontal space.

-
Might be worth adding \m@th as well. – TH. Dec 28 '10 at 8:34
@TH.: Thanks! Hope that what I wrote is correct. – Hendrik Vogt Dec 28 '10 at 8:52
thanks for the solution. Even though your solution and Herbert's work, Herbert's is simpler than yours. So I choose Herbert's. :-) – xport Dec 28 '10 at 10:10
@xport: Good choice! – Hendrik Vogt Dec 28 '10 at 10:12