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.

5
  • 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. Jul 10, 2022 at 17:33
  • @JohnKormylo I'm not sure exactly what you mean. In light of your comment and the post tex.stackexchange.com/questions/301432/align-tikz-word-overlays/…, (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, 2022 at 19:38
  • see also this: tex.stackexchange.com/questions/5959/…
    – Tom
    Jul 10, 2022 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 tex.stackexchange.com/questions/59340/… for a rather complete set of alternatives. Jul 11, 2022 at 0:31
  • @JohnKormylo I am familiar with those answers; however, they address entire paragraphs only, not partial ones.
    – DDS
    Jul 11, 2022 at 4:49

1 Answer 1

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

1
  • Many thanks John Kormylo!
    – DDS
    Jul 11, 2022 at 14:22

You must log in to answer this question.

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