# Drawing boxes around words

I want to put some small boxes around certain words in a paragraph. The boxes will never contain more than one word.

The \ovalbox command described in the fancybox documentation is close to what I need. However, I found some problems:

• The rounded corners do not line up with the sides perfectly.
• Text to the left and right in the paragraph are pushed away.
• The spacing between lines in the paragraph is increased vertically.

I would like to create boxes which:

• Adjust horizontally to fit the width of the text inside.
• Have rounded or beveled corners.
• Can be filled with a solid color.
• Will not cause any text inside or around the box to move.
• Will not impact the vertical spacing of lines.
-
You can use PSTricks or TikZ. –  Leo Liu Nov 28 '11 at 14:25
Possible Duplicate: a boxed alternative with minimal spacing –  Peter Grill Nov 28 '11 at 16:16
I agree with @peter. I think that the answer from tex.stackexchange.com/a/35346/9390 can be modified to include the proper spacings from the fancybox package to do exactly what you want. –  mattgately Nov 28 '11 at 22:55
add comment

## 1 Answer

You can use TikZ with the overlay option and a correct anchor. That would look like this:

\documentclass{article}
\usepackage{tikz}
\newcommand\mybox[2][]{\tikz[overlay]\node[fill=blue!20,inner sep=2pt, anchor=text, rectangle, rounded corners=1mm,#1] {#2};\phantom{#2}}
\begin{document}
\noindent
this is some text \mybox[fill=blue!20]{box} text\\
this is some text box text
\end{document}


You can specify extra options (like I have done here for the color). By using the overlay option and the text anchor we ensure correct placement and no influence on spacing. The phantom is added to get the normal spacing for the content of the box. This is the result of the example code:

Edit: To show that the vertical spacing is not affected either, consider the following:

\documentclass{article}
\usepackage{tikz}
\newcommand\mybox[2][]{\tikz[overlay]\node[fill=blue!20,inner sep=2pt, anchor=text, rectangle, rounded corners=1mm,#1] {#2};\phantom{#2}}
\begin{document}
\begin{minipage}{0.4\textwidth}
\noindent
this is some text \mybox[fill=blue!20]{box} text\\
this is some text box text
\end{minipage}
\begin{minipage}{0.4\textwidth}
\noindent
this is some text box text\\
this is some text box text
\end{minipage}
\end{document}


Which results in:

-
\def\mybox[#1]#2 would force the "optional" argument, so \newcommand\mybox[2][] would be better. –  Martin Scharrer Nov 28 '11 at 14:27
@MartinScharrer: You are absolutely correct. I have modified the answer to reflect this. The [fill=blue!20] is now of course also optional on the \mybox command. –  Roelof Spijker Nov 28 '11 at 14:33
@wh1t3 And what happens if the boxed word is at the end of line with some cesura ? I think you have a vertically problem and some text are pushed horizontally ! –  Alain Matthes Nov 28 '11 at 14:51
You can try :  \begin{minipage}{4cm} \noindent this is a word boxed \mybox[fill=blue!20]{problem}. \end{minipage}  –  Alain Matthes Nov 28 '11 at 14:56
@Herbert: With vertical spacing I was referring to the vertical space between lines as the OP mentions. The vertical size of the box is indeed dependent on what is inside. If this is unwanted you could add text depth=0pt as an option to the node. This does not fix it entirely though. There are multiple options, the easiest is to set text height=1.5ex. This way you will have uniform box height. –  Roelof Spijker Nov 28 '11 at 16:07
show 5 more comments