5

In a test file such as;

\documentclass{article}
\begin{document}

\begin{itemize}
    \item Short line of text
    \item Comparatively long line of text to illustrate that latex is wrapping my text to a preset margin when I have not highlighted it.
\end{itemize}

\end{document}

the text wraps with the preset margin definition. No problem.

However, if the text in either the bullet or subsequent sub-bullet lines are highlighted with \colorbox{colour}{text} then wrapping does not occur. I am using;

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[usenames, dvipsnames]{color}

to modify text colours and my custom margin definitions are made by;

\usepackage[margin=1in,includefoot,includehead]{geometry}

Here is an example of the highlighting I wish to perform.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[usenames, dvipsnames]{color}

\usepackage[margin=1in,includefoot,includehead]{geometry}

\begin{document}

\begin{itemize}
    \item Short line of text
    \item \colorbox{yellow}{Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.}
        \begin{itemize}
            \item \colorbox{yellow}{The problem persists with sub-bullet points too. I assume this is an issue with the coloring command and not my margin definitions.}
        \end{itemize}
\end{itemize}

\end{document}

This is what the issue looks like, if I have not explained myself appropriately;

Text not wrapping when highlighted

Please note that if I continue writing in the same line after the highlighted section, the text will wrap as expected.

2

3 Answers 3

7

\colorbox can't wrap around text, unless an additional \parbox inside of \colorbox is used. I've provided a command for this applying \parbox and reducing the available space by two \fboxsep spaces, otherwise it will overflow again into the margin.

I suggest to use tcolorbox rather (not easier to use, but more versatile)

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[usenames, dvipsnames]{color}

\usepackage[most]{tcolorbox}

\tcbset{enhanced jigsaw, sharp corners,box align=center,boxrule=0pt}

% A special box for this
\newtcolorbox{highlightbox}[1][]{colback=yellow,enhanced jigsaw, sharp corners,box align=center,boxrule=0pt,boxsep=0pt,#1}

\usepackage[margin=1in,includefoot,includehead]{geometry}

\newcommand{\wrapcolorbox}[2][]{%
  \colorbox{#1}{\parbox{\dimexpr\linewidth-2\fboxsep}{#2}}
}

\begin{document}

\begin{itemize}
    \item Short line of text
    \item \wrapcolorbox[green]{Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.}
    \item \begin{tcolorbox}[colback=yellow]
        Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.
        \end{tcolorbox}
        \begin{itemize}
        \item \begin{highlightbox}
            The problem persists with sub-bullet points too. I assume this is an issue with the coloring command and not my margin definitions.
          \end{highlightbox}
       \item \wrapcolorbox[green]{Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.}
        \end{itemize}
\end{itemize}

\end{document}

enter image description here

1
  • Thank you, this has been most helpful! The only issue I am now running into is continuing in plain text on the same line as highlighted text. I appreciate that the text we're customising is not being 'highlighted' in the conventional sense, but rather a box is being drawn around the lines on which our chosen text is.
    – user99335
    Commented Feb 26, 2016 at 18:34
1
\item {\color{yellow}Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.}

should wrap at the margin.

0

You could use \soul package as described in here

1
  • It is much better if you provide an example of how to use the package in this specific context instead of only adding an link to an external website. Highlighting with the soul package only works with "Postscript aware output drivers" according to the manual (section 2, footnote 5).
    – epR8GaYuh
    Commented Jun 10, 2018 at 19:50

You must log in to answer this question.

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