如何用颜色连续阴影化段落的一部分

如何用颜色连续阴影化段落的一部分

考虑以下代码:

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

产生输出

在此处输入图片描述

问题:我希望红色突出显示之间不显示空白;而是让显示的突出显示文本显示为“彩色填充多边形”。实现此目的的最佳方法是什么?如何实现?我的 MWE 可能不是最好的开始方式。评论:我知道如果我想“封锁”整个段落,可以使用包mdframed,但我不知道如何将其扩展到给定段落的特定部分。我用编译了代码pdflatex。谢谢。

答案1

这用于\tikzmark定位突出显示区域的开始和结束。每个区域都\tikzmark需要一个唯一的名称。这些存储在辅助文件中,因此需要运行两次才能找到它们。

请注意,tikzpicture 位于文本之前。

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

演示

相关内容