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.




\usepackage{graphicx}% just for the example text

.. \sqbox{Text} .. \sqbox{Very very long Text} .. \sqbox{\scalebox{.1}[10]{Stretched}} .. \sqbox{$\sum\limits_{k=0}^{10} x_k = i$} .. \sqbox{\verb+$%^&+}



Alternatively you can avoid the package and box the content directly:



\usepackage{graphicx}% For example only

.. \sqbox{Text} .. \sqbox{Very very long Text} .. \sqbox{\scalebox{.1}[10]{Stretched}} .. \sqbox{$\sum\limits_{k=0}^{10} x_k = i$} .. 

  • You can really change the box dimensions with \ht,\dp,\wd commands? Please confirm that I read your code correctly, because it would turn boxes into even more powerful weapon! – yo' Jan 27 '12 at 22:38
  • 1
    @tohecz: yes, you can change the box dimensions! However \advance\ht\mybox by <somevalue>, aka. \addtolength doesn't work, just straight assignments. You need to pay attention if you change the width because you only add (or remove) space from the right. It is better to re-box the content with added horizontal space like above. Check the code of my adjustbox package especially \marginbox and \trimbox if you want to see more. – Martin Scharrer Jan 27 '12 at 22:50

Here is the mandatory overkill tikz version. Since it is uses tikz all the flexibility inherent in tikz is available to customize the square:

enter image description here


  • Will not work if the text is very long unless it is placed in a \parbox.



    \settoheight{\@myBoxHeight}{#2}% Record height of box
    \settodepth{\@myBoxDepth}{#2}% Record depth of box
    \settowidth{\@myBoxWidth}{#2}% Record width of box
    \tikz \node [shape=rectangle, shape aspect=1,draw=red,inner sep=2\pgflinewidth, minimum size=\@myBoxSize,#1] {#2};%

\SquareBox[thick, dashed]{long text}
\SquareBox[draw=blue]{longer text}
\SquareBox[draw=blue, thick, fill=yellow]{$e = mc^2$}
\SquareBox[draw=black, thick, fill=yellow!10, rounded corners=2pt]{$\displaystyle \int_{-\dfrac{\pi}{2}}^{\dfrac{\pi}{2}} dx $}
  • 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 should work for you. It does the necessary vertical corrections so that the text is on a baseline (the same vertical level as the surrounding text). You can change the \fboxsep line to get more or less tight box.

Edit: I removed the \smash command, now even constructions like $\left(\Sq{12345}^n\right)$ work.

Edit 2: Added \textSq for text-mode contents and modified \Sq to accept math-mode.



\begingroup% make boxes and lengths local
\setlength{\fboxsep}{0.3ex}% SET ANY DESIRED PADDING HERE
\setbox1=\hbox{#1}% save the contents
\setlength{\@tempdima}{\maxof{\wd1}{\ht1+\dp1}}% size of the box
\setlength{\@tempdimb}{(\@tempdima-\ht1+\dp1)/2}% vertical raise
\raise-\@tempdimb\hbox{\fbox{\vbox to \@tempdima{%
  \vfil\hbox to \@tempdima{\hfil\copy1\hfil}\vfil}}}%


Hello \textSq{1} world! $f(x):=\Sq1+\Sq2$.


enter image description here

  • Needs a slight tweak: It should be \setbox1=\hbox{\ensuremath{#1}} to be able to handle the case of $f(x):=\Sq1+\Sq{x^2}$. – Peter Grill Jan 27 '12 at 1:57
  • @PeterGrill Thanks. As I'm really not a fan of \ensuremath (it is somehow a "non-predictable" command), so I made it into 2 commands. – yo' Jan 27 '12 at 22:36
  • You're solution is good. Don't know what you mean by "non-predictable". Maybe you have been hanging around @egreg? :-) – Peter Grill Jan 27 '12 at 22:52
  • @PeterGrill Well, I want a macro that produces text in text-mode and math in math-mode. The command \ensuremath does something you should never need, because you---the typesetter---is the one who controls the code, and you should not give this control to such commands. – yo' Jan 27 '12 at 23:18
  • @PeterGrill To a specific example: If I defined \Sq using \ensuremath, then someone can come, write \Sq{x^2}+\Sq{y^2} and he would think what a nice math-mode construction he made, however, it is not a math-mode, but two math-modes with a text-mode + in-between. – yo' Jan 27 '12 at 23:34

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





\squarebox{does this look great or what?}


enter image description here

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

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


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

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

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


Below is a solution based on ConTeXt. I define a special \framed for the task, so all the options of \framed work:






\unknown\squarebox[framecolor=red]{A very long text} 

which gives

enter image description here

You can add a offset=<dimen> to the \defineframed command to have some offset between the text and the frame.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.