2

Based on \usepackage{xcolor} you can box text using the commmand \fcolorbox{declared-color-frame}{declared-color-background}{text}.

Colored boxes with colored frames and linebreaks can be created by the following code of Gonzalo Medinas answer to Draw box with colored background and linebreaks which adjusts to the text width? (Note: I changed the original \colorbox to \fcolorbox, added a color to the frame and added another box MyDBox.)

\usepackage{varwidth}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{lipsum}

\newcommand\MyCBox[1]{%
  \fcolorbox{blue}{SkyBlue}{\begin{varwidth}{\dimexpr\linewidth-2\fboxsep}#1\end{varwidth}}}

\newcommand\MyDBox[1]{%
  \fcolorbox{red}{RedOrange}{\begin{varwidth}{\dimexpr\linewidth-2\fboxsep}#1\end{varwidth}}}

My Question: How to create a colored box around text with a colored frame that fits in the "normal" text without breaks regardless of whether it is one word, a number of words or a few lines of words. As this question doesnt seem very clear to myself, I hope the difference between the two examples will show what I mean.

Example1

enter image description here

Ideally, the colored box with a colored frame would blend in as smooth as colored text (\textcolor{}{}) does. It wouldnt exceed the default textwidth, it could start with any word of a line, it could be directly followed by neutral text or another \MyDBox etc.

Example2

enter image description here

1

1 Answer 1

3

One option using the soul package (a variation of my answer to Highlighting arbitrary chunks of text connected to quotes in margins - Can this be done in TeX/LaTeX?):

\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}

\definecolor{HLcolor}{RGB}{124,18,18}
\sethlcolor{HLcolor!20}

\makeatletter
\newdimen\SOUL@dimen %new
\def\SOUL@ulunderline#1{{%
    \setbox\z@\hbox{#1}%
    \SOUL@dimen=\wd\z@ %new
    \dimen@i=\SOUL@uloverlap
    \advance\SOUL@dimen2\dimen@i %\dimen@ exchanged too
    \rlap{%
        \null
        \kern-\dimen@i
        \SOUL@ulcolor{\SOUL@ulleaders\hskip\SOUL@dimen}% new
    }%
    \unhcopy\z@
}}
\makeatother

\newcommand\MyDBox[2][HLcolor!20]{\sethlcolor{#1}\hl{#2}}

\begin{document}

Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. \MyDBox{Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla.} Donec varius orci eget risus \MyDBox[cyan!30]{Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.} 

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum \MyDBox[orange!30]{gravida mauris. 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. 

\end{document}

enter image description here

A framed variation allowing to control the color for background and the frame:

\documentclass{article}
\usepackage{xparse}
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}

% 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{1.1pt}

\definecolor{HLcolor}{RGB}{124,18,18}
\sethlcolor{HLcolor}

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

\defhighlighter[fill=olive!15]{HLcolor}{.5}

\newcommand{\highlight@DoHighlight}{
  \fill [outer sep = -15pt, inner sep = 0pt, 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

\DeclareDocumentCommand\MyDBox{O{HLcolor!15}O{HLcolor}m}{%
  \colorlet{HLcolor}{#2}
  \highlight[#1]{#3}%
}

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. \MyDBox{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. \MyDBox[yellow!15]{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. \MyDBox[cyan!20][cyan]{Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam.} Duis eget orci sit amet orci dignissim rutrum.

\end{document}

enter image description here

1
  • Unfortunately, I cannot upvote the answer twice.
    – Philip
    May 30, 2014 at 12:52

You must log in to answer this question.

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