I am trying to draw a polygon box around a sentence within a paragraph. This box would wrap over multiple lines and terminate exactly at the end of the sentence (i.e. without white space filling the remaining line width). Therefore, the box I'm trying to draw is not a simple rectangle like what is produced by \fbox
. I've tried several different approaches:
-
Using
\fbox
and\parbox
to create a multi-line outline:\fbox{\parbox{\linewidth}{% Nunc sed pede. Praesent vitae lectus. Praesent neque justo, vehicula eget, interdum id, facilisis et, nibh.}}
-
Using TikZ:
\newcommand\mybox[2][]{% \tikz[overlay]\node[fill=blue!20,inner sep=2pt, anchor=text, rectangle, rounded corners=1mm,#1] {#2};\phantom{#2}}
Any help is greatly appreciated.
Here is a simple document that illustrates the above code:
\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}
\fbox{\parbox{\linewidth}{Nunc sed pede. Praesent vitae lectus. Praesent
neque justo, vehicula eget, interdum id, facilisis et, nibh.}} Phasellus
at purus et libero lacinia dictum. Fuscealiquet. Nulla eu ante placerat
leo semper dictum. Mauris metus. Curabitur lobortis. Curabitur
sollicitudin hendrerit nunc. Donec ultrices lacus id ipsum.
\mybox{Class aptent taciti sociosqu ad litora torquent per conubia nostra,
per incepto hymenaeos.} Aenean nonummy turpis id odio. Integer euismod
imperdiet turpis. Ut nec leo nec diam imperdiet lacinia. Etiam eget lacus
eget mi ultricies posuere. In placerat tristique tortor. Sed porta
vestibulum metus. Nulla iaculis sollicitudin pede. Fusce luctus tellus
in dolor. Curabitur auctor velit a sem. Morbi sapien. Class aptent taciti
sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.
Donec adipiscing urna vehicula nunc. Sed ornareleo in leo. In rhoncus leo
ut dui. Aenean dolor quam, volutpat nec, fringilla id, consectetuer vel, pede
\end{document}
Best Answer
Update.
Improved version:
\tikzmark
is used to set marks at the beginning and end of the text that will be boxed. The main command isThe optional argument can be used to pass options to the
\draw
command used internally (by default, just a frame is drawn, but you can fill it, or change the line width, or apply any other desired valid modification):The code must be run three times.