Drawing boxes around words
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:
My solution needs no special macros. No TikZ, no PSTricks. Only \pdfliteral
primitive is used. And you can define \def\pdfliteral#1{\special{pdf:literal #1}}
if \pdfliteral
isn't available (for example in XeTeX).
\def\mybox#1{\leavevmode \setbox0=\hbox{#1}%
\dimen0=\wd0 \edef\posxA{\expandafter\ignorept\the\dimen0 \space}%
\hbox{\kern3pt\pdfliteral{q .8 .8 1 rg .8 .8 1 RG .9963 0 0 .9963 0 0 cm 1 j 1 J 6 w
0 0 m 0 5 l \posxA 5 l \posxA 0 l 0 0 l B Q}%
\box0 \kern3pt}%
}
{\lccode`\?=`\p \lccode`\!=`\t \lowercase{\gdef\ignorept#1?!{#1}}}
This is a \mybox{test} of my \mybox{box}.
\bye
This kind of boxes can be done with extrude
option from tcolorbox
. A little example adapted from tcolorbox
documentation:
\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcbox{\mybox}[1][]{enhanced, colframe=blue, colback=blue!15,
frame style={opacity=0.25}, interior style={opacity=0.25},
nobeforeafter, tcbox raise base, shrink tight, extrude by=1mm, #1}
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut \mybox[extrude bottom by=2cm]{purus} elit, vestibulum ut, \mybox{placerat} ac,
\mybox[extrude top by=2mm]{adipiscing} vitae, \mybox[extrude left by=5mm, extrude
bottom by=2mm]{felis}. Curabitur dictum gravida mauris. \mybox{Nam} arcu libero,
nonummy eget, consectetuer id, vulputate a, magna.
\end{document}