# Creating uniformly sized boxes around text

I want to put individual words in to boxes next to each other. I've used the \framebox command, but this has produced different sized boxes around each word. How could I get all the boxes to be the same size around, e.g. [This] [is] [an] [example]?

-
Do you mean different height/depth or width or both? –  Stephan Lehmke Dec 11 '12 at 18:00
@StephanLehmke width isn't really a concern, height is :) –  Danger Fourpence Dec 11 '12 at 18:13

\framebox puts a frame around the box that you give it, and in TeX, boxes fit tightly around their object. So two \framebox around two different text boxes, say "This" and "example" will have different sizes, as can be seen in the MWE and resulting output below:

\documentclass{article}

\newcommand*{\mybox}[1]{\framebox{#1}}

\begin{document}
\mybox{This} \mybox{is} \mybox{an} \mybox{example}
\end{document}


Hence to get the frames to a fixed size, you need to fix the size of the boxes you give it.

## Increase the "height" of the boxes

The most common desire is to set the total height of the boxes. Total height, because what we commonly refer to the "height" is actually the sum of two lengths: the height of the box, that for text depends on ascenders, and its depth, that for text depends on descenders.

If you want to create frames that are taller than the original boxes, it's easy: you simply put inside the boxes an invisible element (i.e. of zero width) of the desired height and depth. Such an element is very common in typography and has a name: a strut.

The default strut in LaTeX, \strut, has a height of 70% of the distance between 2 baselines (\baselineskip) and a depth of 30% of the same length. So changing our earlier \mybox definition (line 3) to include a strut yields the following:

\newcommand*{\mybox}[1]{\framebox{\strut #1}}


If that height doesn't suit you, you simply need to define your own strut. Let's say we want a depth of only 5% of the baseline skip and a height of 195% of it (note that the last argument of \rule is the total height of the rule, not just its height):

\newcommand*{\mystrut}{\rule[-0.05\baselineskip]{0pt}{2\baselineskip}}
\newcommand*{\mybox}[1]{\framebox{\mystrut #1}}


## Increase or decrease the "height" of the boxes

So the previous technique with struts works in most desired cases, but the particular example I choose also shows the limits of this technique (look closely at the bottom borders): the boxes will only fit exactly around the struts if the struts are the tallest and deepest elements. If the remaining content is taller or deeper than your custom strut, due for example to the "p" in our example, the box will accommodate that extra height.

Now what if you want a framed box that might be smaller than your content? Well, you simply make TeX believe that your content is the size you target (say a height of 15% of the baseline skip, and a depth of 5% of it), using a \raisebox:

\newcommand*{\mybox}[1]{%
\framebox{\raisebox{0pt}[0.15\baselineskip][0.05\baselineskip]{%
#1}}}


## Set the width of the boxes

Finally, if you also want to set the width of the boxes, you can include the content in boxes of fixed width with horizontal stretch or shrink glue on each size (\hss):

\newcommand*{\mybox}[1]{%
\framebox{\raisebox{0pt}[0.15\baselineskip][0.05\baselineskip]{%
\hbox to 0.8cm{\hss#1\hss}}}}


-
+1 for the very detailed answer. Please, don't use the minimal class for examples, see this question to know why. –  egreg Dec 11 '12 at 21:43
@egreg Changed. I wasn't aware of the minimal issue, thanks for pointing it out! –  Xavier Dec 11 '12 at 22:12

The reason here is that the contents has different heights/depths. One way around this would be to use a \strut within all boxes as shown in the following example (I've created a command \Fbox which adds \strut to the argument of \fbox):

\documentclass{article}
\newcommand{\Fbox}[1]{\fbox{\strut#1}}
\setlength{\fboxsep}{1pt}% Just for this example
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
Here is some regular text. \fbox{This} \fbox{is} \fbox{an} \fbox{example}. \par
Here is some regular text. \Fbox{This} \Fbox{is} \Fbox{an} \Fbox{example}. \par
\end{document}

-
Strut high boxes will bump into the lines above and below, with high chances that \lineskip comes into action: the \fboxsep and \fboxrule add to the height and depth of such a box. –  egreg Dec 11 '12 at 21:46

Not to take any merit away from Xavier's nice and very detailed answer, here is a possibly quicker method. The main problem in this kind of jobs is to avoid increasing the interline space, so I reduce (uniformly) the padding around the text, still using the strut for having uniform height and depth.

\documentclass{article}

\newcommand{\ffbox}[1]{%
{% open a group for a local setting
\setlength{\fboxsep}{-2\fboxrule}% the rule will be inside the box boundary
\fbox{\hspace{1.2pt}\strut#1\hspace{1.2pt}}% print the box, with some padding at the left and right
}% close the group
}

\begin{document}
Here is some regular text. \fbox{This} \fbox{is} \fbox{an} \fbox{example}.

% The following line is just for showing that the lines are still equally spaced
\leavevmode\smash{\llap{\vrule height 0pt  depth\baselineskip\kern.5pt}}%
Here is some regular text. \ffbox{This} \ffbox{is} \ffbox{an} \ffbox{example}.

Here is some regular text. \ffbox{This} \ffbox{is} \ffbox{an} \ffbox{example}.

\end{document}


-