[Tex/LaTex] Creating uniformly sized boxes around text

boxesframed

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]?

Best Answer

\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}

Tight frames around text boxes

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}}

Frames around struts

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}}

Frames around custom struts

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}}}

Frames of any height

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}}}}

Frames of any height and width