Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.
share|improve this question
2  
You can use PSTricks or TikZ. –  Leo Liu Nov 28 '11 at 14:25
1  
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

1 Answer 1

up vote 13 down vote accepted

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:

TikZ text box

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:

TikZ text box vertical spacing

share|improve this answer
2  
\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
1  
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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