1

This post is an extension of the question posed in How to Contiguously Shade a Portion of a Paragraph with Color

Consider the code

\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}

\vspace*{10pt}

Here is some text. \tikzmark{start}\textbf{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} 

which produces

enter image description here

I have made bold the color shaded text in a usual way---by using \textbf applied to a specific portion of the text---though it seems to neutralize the use of the \tikzmark command in the first paragraph. (Not sure why.)

I have a document consisting of a few hundred pages which I would, in addition to color shading some of the text, I would like to make such text bold as well. And I would like to avoid, if possible, using \textbf throughout the document many times.

I would like to be able to have another command (an alternative to \tikzmark, say, something like \tikzmarkbold) which does the shading of \tikzmark, but also makes the shaded text bold, automatically.

I would like to have the choice to use either of the two commands in the document.

QUESTION: Is it possible to modify the above code to accomplish this; and if so, how?

Thank you.

3
  • I would probably create a new macro using \newcommand{\textbfhl}[1]{\tikzmark{start}\textbf{#1}\tikzmark{end}} and then mark the text using \textbfhl{....}. Jul 30 at 23:27
  • It is also possible to replace one of the tikzmarks with a tikzpicture. Jul 30 at 23:39
  • 1
    You have to give the tikzmarks unique labels, e.g. \tikzmark{start1}...\tikzmark{end1} for the first paragraph, \tikzmark{start2}...\tikzmark{end2} for the second, and so on. Of course, the revised labels have to be used in the respective tikzpictures, too. But if you want to also allow for page breaks etc. things will probably become more delicate.
    – user277428
    Jul 30 at 23:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.