在文本背景上绘制 Tikz 突出显示

在文本背景上绘制 Tikz 突出显示

我最近被推荐LaTeX 中的酷炫文本突出显示用于 Tikz 突出显示和下划线。

我已设法修改 Antal 答案以满足我的需求,但似乎无法实现以下目标:我希望将突出显示绘制在文本背景上,而不是像现在这样绘制在文本上。

我准备了一个 MWE 来更好地解释我的意思:

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

\makeatletter

\newcommand{\defhighlighter}[3][]{%
  \tikzset{every highlighter/.style={color=#2, fill opacity=#3, #1}}%
}

\defhighlighter{yellow}{.5}

\newcommand{\highlight@DoHighlight}{
  \fill [ decoration = {random steps, amplitude=1pt, segment length=15pt}
        , outer sep = -15pt, inner sep = 0pt, decorate
        , every highlighter, this highlighter ]
        ($(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-3pt)$) ;
}

\newcommand{\highlight@BeginHighlight}{
  \coordinate (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}{
  \coordinate (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[1][]{%
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight
      \highlight@EndHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight
      \highlight@DoHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \highlight@DoHighlight
        \highlight@BeginHighlight
      \fi
    \end{tikzpicture}%
    \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
  }%
  \SOUL@
}
\makeatother

\begin{document}
\highlight[yellow]{The highlight is drawn over the text}

\bigskip
\begin{tikzpicture}
\node[fill=yellow!50]{the highlight is drawn below text};
\end{tikzpicture}
\end{document}

是否可以像 Tikz 节点的标准填充一样将高亮部分移到文本下方?

答案1

使用此解决方案,我保留 OP 的解决方案,并以 的形式添加另一层\Ohighlight,形式为

\newsavebox\tmpbox
\usepackage{tabto}
\newcommand\Ohighlight[2][]{%
  \tabto*{0in}\edef\tmpx{\TabPrevPos}\tabto*{\tmpx}%    SAVES THE CURRENT x POSITION
  \savebox\tmpbox{%                            SETS THE TEXT, STARTING AT x, IN A SAVED MINIPAGE
  \begin{minipage}[b]{\textwidth}
    \tabto*{\tmpx}#2\strut%
  \end{minipage}}%
  {\color{\highlightcolor!0}\highlight[#1]{#2}}%         STEP #1, BELOW
  \vspace*{\dimexpr-\dp\tmpbox-\ht\tmpbox+\dp\strutbox}% STEP #2, BELOW
  \tabto*{\tmpx}#2\unskip\ %                             STEP #3, BELOW
}

试图完成以下任务:

1)进行原始\highlight操作,同时使文本与突出显示的颜色相同(即不可见);

2)放回到原来的位置(水平放置tabto包装),然后垂直放置在\vspace测量物上minipage

3) 在先前突出显示的位置重新输入清晰的文本,使页面位置保持在应在的位置。

当注意到 和 文本输出之间存在一些字距差异时,我们对此解决方案进行了编辑\highlight。现在,预处理\highlight采用空白颜色,以避免出现双重文本问题。

\raggedbottom这种方法无法跨页面边界工作。在 MWE 中,它可以跨段落边界工作,但我认为,除非使用 粘连线,否则插入页面上的粘连线可能会造成混乱。

事实上,有人注意到一些字距调整问题,促使我修改了我的代码,这留下了一个可能性,即在某些情况下,换行符\highlight可能会与明文文本不同,这显然代表该方法失败了。由于该问题似乎与尾随空格有关,因此\unskip\在明文的最终文本中添加一个(步骤 3.)似乎可以解决字距调整问题,但我不会说它不会以另一种形式出现。

如果要检查字距问题,请将此行编辑{\color{\highlightcolor!0}\highlight[#1]{#2}}%{\color{red}\highlight[#1]{#2}}%,这将产生文本的双击。如果它们不匹配,您将在那里看到它。

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

\makeatletter

\newcommand{\defhighlighter}[3][]{%
  \tikzset{every highlighter/.style={color=#2, fill opacity=#3, #1}}%
}

\def\highlightcolor{yellow}
\defhighlighter{\highlightcolor}{.5}

\newcommand{\highlight@DoHighlight}{
  \fill [ decoration = {random steps, amplitude=1pt, segment length=15pt}
        , outer sep = -15pt, inner sep = 0pt, decorate
        , every highlighter, this highlighter ]
        ($(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-3pt)$) ;
}

\newcommand{\highlight@BeginHighlight}{
  \coordinate (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}{
  \coordinate (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[1][]{%
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight
      \highlight@EndHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight
      \highlight@DoHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \highlight@DoHighlight
        \highlight@BeginHighlight
      \fi
    \end{tikzpicture}%
    \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
  }%
  \SOUL@
}
\makeatother

\newsavebox\tmpbox
\usepackage{tabto}
\newcommand\Ohighlight[2][]{%
  \tabto*{0in}\edef\tmpx{\TabPrevPos}\tabto*{\tmpx}%
  \savebox\tmpbox{%
  \begin{minipage}[b]{\textwidth}
    \tabto*{\tmpx}#2\strut%
  \end{minipage}}%
  {\color{\highlightcolor!0}\highlight[#1]{#2}}%
  \vspace*{\dimexpr-\dp\tmpbox-\ht\tmpbox+\dp\strutbox}%
  \tabto*{\tmpx}#2\unskip\ %
}
\textwidth=5in

\begin{document}
\highlight{The original highlight is drawn over the text.
The original definition works over... 

Multi paragraphs, so my fix should try to as well.}

\bigskip
\begin{tikzpicture}
\node[fill=yellow!50]{with tikz, the highlight is drawn below text but not fancy};
\end{tikzpicture}

Now here is my fix. \Ohighlight{The highlight is drawn under the text}
Does it work multiline?
\Ohighlight[yellow]{The highlight is drawn under the text The highlight is drawn under the text The highlight is drawn under the text The highlight is drawn under the text

A new paragraph}
And more...
\Ohighlight{short}

\end{document}

在此处输入图片描述

下图显示了缩放。

在此处输入图片描述

相关内容