2

I need a background shade in the running text of middle of a paragraph, I tried through \usepackage{tikz}, I am getting background but text is not breaking and running beyond the text width.

How to fix automate breaking in a paragraph with shade?

MWE:

\documentclass{book}

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

\renewcommand\hl[1]{%
    \tikz[baseline,%
      decoration={amplitude=1pt,segment length=15pt},%
      outer sep=-15pt, inner sep = 0pt%
    ]%
   \node[decorate,rectangle,fill=gray!50,anchor=text]{#1};%
}%


\begin{document}

In the \hl{following chapters} of the book we a \hl{In the following chapters of the book \protect\ensuremath{\alpha \beta \gamma} we are going to be fitting statistical models to data. The script or set of R commands necessary for their analyses, data sets in CSV (comma-separated values) format, and the R workspace containing the R objects created for each chapter can be found on the companion web site. ]\hl{The R packages needed to fit the models in each chapter are indicated at the start of each chapter} and can be loaded with the specified script.}
\end{document}
2
  • What is the reason for renewing the orginal \hl macro? If it is just for the color, you can use \colorlet{mygray}{gray!50} \sethlcolor{mygray}. Jul 8, 2022 at 8:22
  • @JasperHabicht, \ensuremath{} is not working, when using \ensuremath in the \hl{} command shading is turned off from that position
    – RCV
    Jul 8, 2022 at 8:34

1 Answer 1

5

The soul package uses a complicated algorithm for underlining and highlighting to allow for line breaking. If you replace the \hl macro, you essentially overwrite this sophisticated approach.

If you only want to change the color of the shading, you should use the macro \sethlcolor provided by the package. Note that you cannot insert modified colors like gray!50 directly but you need to define a custom color with this value first.

Please take care that you properly set the curly brackets in your code. You can try to place stuff inside a box if it would conflict with highlighting otherwise:

\documentclass{book}

\usepackage{soul, xcolor}

\colorlet{mygray}{gray!50}
\sethlcolor{mygray}

\begin{document}

In the \hl{following chapters} of the book we a 
\hl{In the following chapters of the book \mbox{\protect\ensuremath{\alpha \beta \gamma}} 
we are going to be fitting statistical models to data. The script or set of R commands necessary 
for their analyses, data sets in CSV (comma-separated values) format, and the R workspace 
containing the R objects created for each chapter can be found on the companion web site.} 
\hl{The R packages needed to fit the models in each chapter are indicated at the start of 
each chapter} and can be loaded with the specified script.

\end{document}

enter image description here

1
  • +1: In many ways a role model for a good answer. Jul 8, 2022 at 8:43

You must log in to answer this question.

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