# Text in squared box

I'd like to bot a square box around some text. I'd like to denote a labelled object of size 1, in combinatorics this is often symbolised by something like \boxed{1}. It would be nicer if the box was a square. Is there an adequate command for this?

-
So even if your text is not "square", you want to place a square box around it? – Werner Jan 26 '12 at 21:08
Short answer: yes. Long answer: You are right, that's a problem for beauty. But the square is used in graph theory for leaves of trees and I want to adopt this. – lumbric Jan 26 '12 at 21:22

Here a solution based on my collectbox package. This makes it support verbatim and other special text.

It doesn't change the baseline of the content, which is what you normally want.

\documentclass{article}

\usepackage{collectbox}

\makeatletter
\newcommand{\sqbox}{%
\collectbox{%
\@tempdima=\dimexpr\width-\totalheight\relax
\ifdim\@tempdima<\z@
\fbox{\hbox{\hspace{-.5\@tempdima}\BOXCONTENT\hspace{-.5\@tempdima}}}%
\else
\ht\collectedbox=\dimexpr\ht\collectedbox+.5\@tempdima\relax
\dp\collectedbox=\dimexpr\dp\collectedbox+.5\@tempdima\relax
\fbox{\BOXCONTENT}%
\fi
}%
}
\makeatother

\usepackage{graphicx}% just for the example text
\begin{document}

\end{document}

-
looks nice, you just need to do the vertical correction (the "raise value" is -0.5*(myBoxSize-1.2*myBoxHeight+1.2*myBoxDepth)) – yo' Jan 26 '12 at 23:17
@tohecz: not sure what you mean. Is that related to why I needed the 1.2 fudge factor? – Peter Grill Jan 26 '12 at 23:19
I think that it is better to align the boxes by their baseline and not by their bottom. Check the line \setlength{\@tempdimb}{(\@tempdima-\ht1+\dp1)/2} in my code, it deals with the vertical correction. I was just suggesting you the same correction, because obviously your answer uses tikz with all its features ;) – yo' Jan 26 '12 at 23:28
@tohecz: It does not appear as if it is necessary, the contents appeared to be centered already. – Peter Grill Jan 27 '12 at 2:15
@PeterGrill: You can set the key text width. In this case you don't need a \parbox. – Marco Daniel Jan 27 '12 at 9:53

This is a solution that uses pstricks the great Herbert Voss offered some years ago:

\documentclass[12pt]{article}
\usepackage{pstricks}

\usepackage{auto-pst-pdf} % for easy pdf-output - otherwise comment this out

\def\bBox#1#2{\makebox[#1]{#2}}
\def\bhBox#1#2{\parbox[c][#1][c]{#1}{\makebox[#1]{#2}}}
\def\bhpBox#1#2{\parbox[c][#1][c]{#1}{\centering #2}}

\begin{document}
%\psset{framesep=0}
\psframebox{\bBox{1.3cm}{A}} und \psframebox{\bBox{1.3cm}{BBBBB}}

\psframebox{\bhBox{1.3cm}{A}} und \psframebox{\bhBox{1.3cm}{BBBBB}}

\psframebox{\bhpBox{1.3cm}{A}} und \psframebox{\bhpBox{1.3cm}{BBBBB}}

\end{document}

-

tohecz just beat me to it. Here is an alternative version:

\documentclass[letterpaper,11pt]{article}
\usepackage{calc}

\newlength{\spacer}
\newsavebox{\mybox}

\newcommand{\squarebox}[1]{%
\sbox\mybox{#1}%
\setlength{\spacer}{0.5\wd\mybox-0.5\ht\mybox}%
\fbox{\parbox{\wd\mybox}{\vspace{\spacer}\usebox\mybox\vspace{\spacer}}}%
}

\begin{document}

\squarebox{does this look great or what?}

\end{document}


-
the text in the box is a bit vertically mal-positioned: \squarebox{boxed text \_\_}\_\_ baseline – yo' Jan 26 '12 at 22:31