8

I want to draw a box (no rounded corners, keep it simple ^^) around one or multiple words. There are two special things: 1) the box should support line break and 2) the box should not cover multiple lines. I added a figure to make my requirements more clear. I tested fbox and all its derivates, but all of them create a paragraph for the framed word. Thank you!

Illustrative example

1
  • Do you mean linebreaking within the box or such, that the words appear in a new 'box' in the next line?
    – user31729
    Commented Mar 8, 2014 at 17:18

1 Answer 1

13

A variation of this answer to Highlighting arbitrary chunks of text connected to quotes in margins - Can this be done in TeX/LaTeX? . The boxed text can spann multiple lines, and hyphenation is allowed but no page breaks are allowed:

\documentclass{article}
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing}

% The following code contains a variation of the great code by Antal S-Z
% in his answer to https://tex.stackexchange.com/a/6029/3954
%in TeX.SX

\newlength\LineWidth
\setlength\LineWidth{0.4pt}

\colorlet{HLcolor}{black}
\sethlcolor{HLcolor!20}

\makeatletter
\newcommand{\defhighlighter}[3][]{%
  \tikzset{every highlighter/.style={color=white, fill opacity=#3, text opacity=1,#1}}%
}

\defhighlighter{HLcolor!20}{0}

\newcommand\highlight@DoHighlight{%
  \fill [outer sep = -15pt, inner sep = 0pt, decorate
        , every highlighter, this highlighter,draw=none]
        ($(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-2pt)$) ;
  \draw[HLcolor,line width=\LineWidth]  ($(begin highlight)+(0,-2pt)$) -- ($(end highlight)+(0,-2pt)$) ;
  \draw[HLcolor,line width=\LineWidth]  ($(begin highlight)+(0,8pt)$) -- ($(end highlight)+(0,8pt)$) ;
}

\newcommand{\highlight@BeginHighlight}{
  \coordinate (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}{
  \coordinate (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[1][]{%
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight
      \draw[HLcolor,line width=\LineWidth]  ($(begin highlight)+(0,-2pt)+(0,-0.5\pgflinewidth)$) -- ($(begin highlight)+(0,8pt)+(0,0.5\pgflinewidth)$) ;
      \highlight@EndHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight
      \highlight@DoHighlight
      \draw[HLcolor,line width=\LineWidth]  ($(end highlight)+(0,-2pt)+(0,-0.5\pgflinewidth)$) -- ($(end highlight)+(0,8pt)+(0,0.5\pgflinewidth)$) ;
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \highlight@DoHighlight
        \highlight@BeginHighlight
      \fi
    \end{tikzpicture}%
    \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
  }%
  \SOUL@
}
\makeatother

\newcommand\MarkText[1]{\highlight{#1}}

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. \MarkText{Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas.} Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. \MarkText{Donec varius orci} eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.

Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna,
vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse
ut massa. Cras nec ante. Pellentesque a nulla. \MarkText{Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus.} Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.

\end{document}

enter image description here

Simply use \MarkText{text...}; the thicknes of the rules used for the frame can be changed using \LineWidth (default value=0.4pt).

6
  • Awesome, thank you! I up voted also the mentioned answer!
    – strauberry
    Commented Mar 8, 2014 at 18:43
  • Nice, but it breaks in interesting ways with text that contains math elements like "the field $F$". Commented Feb 11, 2015 at 11:44
  • @JoeCorneli I did some tests using in-line math and the highlighting worked as expected without breaking. Could you please give an example in which the code breaks so I can test it? Commented Jun 20, 2015 at 1:14
  • @GonzaloMedina The issue seems to have to do with shifting line boundaries not with the specific content after all. Rerunning pdflatex fixes the problem. To replicate, try entering long made-up words in front of one of the boxes :-) Commented Jun 20, 2015 at 1:43
  • 1
    @JoeCorneli Ah, if rerunning pdflatex fixes the problem, then it's not really a problem. Since overlay,remember picture is used, some internal calculations are performed on the first run to find the proper coordinates, so the code needs at least two runs to stabilize. Commented Jun 20, 2015 at 1:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .