Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I box content in LaTeX such that all boxes are on the same line and the same height?

I need to represent calculator key presses by the text for the keys to press surrounded by a box. I tried to get away with just the bare key presses (no box) but I got marked down for it because it would appear my assessor is a bit of a pedant.

Anyway, a bit of research turned up the likes of \boxit and \fbox.

I gave them both a go but \boxit produces nigh on identical results to \fbox. The problem is that the height and baseline of the box varies with the glyphs it contains.

Take the following example that represents pressing 5, multiply, x;

\documentclass{article}
\usepackage{fullpage}
\begin{document}
\fbox{5} \fbox{$\times$} \fbox{$x$}
\end{document}

This generates boxes around the content but they're on different baselines and they're all different heights.

Given what I've seen of LaTeX thus far I'd say this is definitely possible but CTAN, news groups and google have turned up nothing useful thus far.

Any hints?

like image 978
Mike Avatar asked Dec 31 '25 18:12

Mike


1 Answers

Using \strut to make the same height.

\def\press#1{\fbox{\hbox to 1em{\strut\hfil#1\hfil}}}
\press{5} \press{$\times$} \press{$x$}

or set any height and depth of your box:

\def\press#1{{\setbox0=\hbox to 1em{\hfil#1\hfil}\ht0=7.5pt \dp0=2.5pt \fbox{\box0}}}
\press{5} \press{$\times$} \press{$x$}
like image 170
Alexey Malistov Avatar answered Jan 03 '26 13:01

Alexey Malistov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!