3

Consider the code:

\documentclass{book}
\usepackage{xcolor,soul}
\colorlet{soulred}{red!25}

\begin{document} \thispagestyle{empty} \LARGE

\begingroup \sethlcolor{soulred}% Here is some text. \hl{Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text.}% \endgroup \end{document}

producing the output

enter image description here

QUESTION: I would like to have no white space showing between the red highlighting; but rather, have the displayed highlighted text appear as a "color-filled polygon." What might be the best way to accomplish this; and how? My MWE may not be the best way to start. Remark: I know that if I wanted the entire paragraph "blocked off", I could use the mdframed package, but I don't know how to extend it to a specific portion(s) of a given paragraph. I compiled the code with pdflatex. Thank you.

DDS
  • 8,816
  • 4
  • 9
  • 36
  • It would seem that soul goes to some effort to put those white gaps between the lines. It doesn't increase the line spacing, but \hl{...} does redefine \strut and \rule. – John Kormylo Jul 10 '22 at 17:33
  • @JohnKormylo I'm not sure exactly what you mean. In light of your comment and the post https://tex.stackexchange.com/questions/301432/align-tikz-word-overlays/301433#301433, (figuring that I might be able to adjust the thickness of the highlighting and eliminate the undesired white-line space)---I am unfortunately, unable to "break" the highlighting to resemble an ordinary paragraph. Perhaps you might consider elaborating a little more in the form of an answer. Thank you for the information you provided. – DDS Jul 10 '22 at 19:38
  • see also this: https://tex.stackexchange.com/questions/5959/cool-text-highlighting-in-latex – Tom Jul 10 '22 at 22:33
  • I was trying to find a simple solution by reducing the line spacing or increase the size of the highlighted area, but nothing I tried worked. See https://tex.stackexchange.com/questions/59340/how-to-highlight-an-entire-paragraph for a rather complete set of alternatives. – John Kormylo Jul 11 '22 at 00:31
  • @JohnKormylo I am familiar with those answers; however, they address entire paragraphs only, not partial ones. – DDS Jul 11 '22 at 04:49

1 Answers1

3

This uses \tikzmark to locate the start and end of the highlighted area. Each \tikzmark requires a unique name. These are stored on the aux file, so it takes two runs to locate them.

Note that the tikzpicture comes before the text.

\documentclass{book}
\usepackage{xcolor}
\colorlet{soulred}{red!25}
\usepackage{tikzpagenodes}
\usetikzlibrary{tikzmark,calc}

\newlength{\markheight}

\begin{document} \thispagestyle{empty} \LARGE

\begin{tikzpicture}[overlay, remember picture] \coordinate (A) at (pic cs:start); \coordinate (B) at (pic cs:end); \pgfextracty{\markheight}{\pgfpointdiff{\pgfpointanchor{B}{center}}{\pgfpointanchor{A}{center}}}% difference in baselines \ifdim\markheight<\baselineskip% same line \fill[color=soulred] ($(A)+(-2pt,\ht\strutbox)$) rectangle ($(B)+(2pt,-\dp\strutbox)$); \else \ifdim\markheight<3\baselineskip \fill[color=soulred] ($(A)+(-2pt,\ht\strutbox)$) rectangle ($(A -| current page text area.east)+(2pt,-\dp\strutbox)$); \fill[color=soulred] ($(B -| current page text area.west)+(-2pt,\ht\strutbox)$) rectangle ($(B)+(2pt,-\dp\strutbox)$); \else \fill[color=soulred] ($(A)+(-2pt,\ht\strutbox)$) rectangle ($(A -| current page text area.east)+(2pt,-\dp\strutbox)$); \fill[color=soulred] ($(A -| current page text area.west)+(-2pt,-\dp\strutbox)$) rectangle ($(B -| current page text area.east)+(2pt,\ht\strutbox)$); \fill[color=soulred] ($(B -| current page text area.west)+(-2pt,\ht\strutbox)$) rectangle ($(B)+(2pt,-\dp\strutbox)$); \fi \fi \end{tikzpicture}

Here is some text. \tikzmark{start}Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text. Here is some text.\tikzmark{end}

\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120