# Vertically center text or math in a framebox

I have the following commands in my code (taken from Creating uniformly sized boxes around text) to manually adjust the height of frameboxes (the reason is that I often put several in a row with different math/text, wanting all of them to be the same size):

\newcommand*{\mystrut}{\rule[-.5\baselineskip]{0pt}{1.5\baselineskip}}
\newcommand*{\mybox}[1]{\framebox{\mystrut #1}}


where I adjust the numbers in front of each \baselineskip depending on how big I want the boxes to be. While this eventually gets me the height of the boxes I want, if I want to center the text vertically in those boxes I have to fiddle around with those two numbers manually. How can I automatically center my text in an intentionally oversized framebox?

For instance, in the following code, is there something I could add to my \mybox command to automatically vertically center the text after everything else has been done (the number chosen for baselineskip was found using some other code to calculate the height of the largest box)?

\baselineskip=23.76836pt
\newcommand*{\mystrut}{\rule[-.5\baselineskip]{0pt}{1.5\baselineskip}}
\newcommand*{\mybox}[1]{\framebox{\mystrut #1}}
\mybox{$\frac{a^2+b^2}{c^2+d^2}$}\mybox{$\frac{\frac{a}{\sqrt{a^2+b^2}}}{\frac{c}{\sqrt{c^2+d^2}}}$}\mybox{$a=b=c=d=e=f=g$}


Output (space below all math items is smaller than space above in each box):

Thanks very much.

There are a variety of ways to approach such things. Here's an example using tikz

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[
my node style/.style={
draw,
outer sep=0pt,
inner sep=4pt,
minimum height=2*23.76836pt}]
\node[my node style] (A) at (0,0) {$\dfrac{a^2+b^2}{c^2+d^2}$};
\node[my node style] (B) [right=0pt of A] {$\dfrac{\frac{a}{\sqrt{a^2+b^2}}}{\frac{c}{\sqrt{c^2+d^2}}}$};
\node[my node style] (C) [right=0pt of B] {$a=b=c=d=e=f=g$};
\end{tikzpicture}

\end{document}


There's also the tcolorbox package: I'll let someone else post that as an answer since I'm not too familiar with it.

• These are great, exactly the kind of things I'm looking for. Thanks very much to both of you. – user3558855 Oct 26 '14 at 22:49

Here is one way where you can adjust the depth and height of \rule yourself to get good looks.

\documentclass{article}

\newcommand*{\mystrut}{\rule[-16pt]{0pt}{36pt}}
\newcommand*{\mybox}[1]{\tabular{|l|}\hline \mystrut#1\\\hline \endtabular}

\begin{document}
\mybox{$\frac{a²+b²}{c²+d²}$}%
\mybox{$\frac{\frac{a}{√{a²+b²}}}{\frac{c}{√{c²+d²}}}$}%
\mybox{$a=b=c=d=e=f=g$}
\end{document}