如何使用 tikz“突出显示”文本/公式?

如何使用 tikz“突出显示”文本/公式?

我尝试构造一个黄色矩形(带有tikz),以便我可以使用它来“突出显示”一段文本。下面是一个最小示例。它已经看起来不错了,但我如何避免单词“dummy”被凸起?所有单词都应位于同一高度。

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[american,ngerman]{babel}
\usepackage{tikz}

\newcommand*{\yellowemph}[1]{%
  \tikz\node[rectangle, fill=yellow, rounded corners, inner sep=0.3mm]{#1};%
}

\begin{document}
Just some \yellowemph{dummy} text to see what happens.
\end{document}

更新

谢谢大家的帮助。下面,我简要概述/比较了建议的方法。不幸的是,版本 3 和 4 覆盖了文本。版本 2(灵魂)不起作用之内数学模式(甚至不能通过将其包装在 中\text{})。我喜欢版本 1。请参阅此页面以了解如何自动确定是否处于数学模式:\newcommand:如何确定是否处于数学模式(然后包括$ $)?

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[american,ngerman]{babel}
\usepackage{tikz}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% version 1: needs $ $ in math mode; not work across line boundaries
\newcommand*{\yellowemph}[1]{%
  \tikz[baseline=(text.base)]\node(text)[rectangle, fill=yellow, rounded
corners, inner sep=0.3mm]{#1};%
}

% version 2: does not seem to work in math mode, not even with $ $
\usepackage{soul}% use \hl{} to highlight

% version 3: does not work across line boundaries
% see https://tex.stackexchange.com/questions/35319/a-boxed-alternative-with-minimal-spacing/35357#35357
\usetikzlibrary{calc, shapes}
\makeatletter
\newcommand*{\@DrawBoxHeightSep}{0.030em}%
\newcommand*{\@DrawBoxDepthSep}{0.025em}%
\newcommand{\@DrawBox}[3][red]{% #1=style, #2=height, #3=depth
    \tikz[overlay,remember picture]{
    \draw[#1]
      ($(bl)+(-0.2em,#2+\@DrawBoxHeightSep)$) rectangle
      ($(br)+(0.2em,-#3-+\@DrawBoxDepthSep)$);}
}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newdimen\@myBoxHeight%
\newdimen\@myBoxDepth%
\newcommand{\MyBox}[2][red]{%
    \settoheight{\@myBoxHeight}{#2}% record height of box
    \settodepth{\@myBoxDepth}{#2}% record height of box
    \tikzmark{bl}#2\tikzmark{br}\@DrawBox[#1]{\@myBoxHeight}{\@myBoxDepth}% draw the box
}
\makeatother

% version 4: text gets overwritten
% see
% https://tex.stackexchange.com/questions/5959/cool-text-highlighting-in-latex
% \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}
\noindent
word $\frac{1}{2}$ $\displaystyle\frac{1}{2}$\\
\yellowemph{word} \yellowemph{$\frac{1}{2}$}
\yellowemph{$\displaystyle\frac{1}{2}$} $\yellowemph{$\displaystyle\frac{1}{2}$}$; Version 1: does not work across lines\\
\hl{word} \hl{$\frac{1}{2}$} \hl{$\displaystyle\frac{1}{2}$}
%$\text{\hl{$\displaystyle\frac{1}{2}$}}$
; Version 2: soul, does not work \emph{within} math mode!\\
\MyBox[fill=yellow, draw=yellow, opacity=0.3]{word} \MyBox[fill=yellow,
draw=yellow, opacity=0.3]{$\frac{1}{2}$} \MyBox[fill=yellow, draw=yellow,
opacity=0.3]{$\displaystyle\frac{1}{2}$}
%$\MyBox[fill=yellow, draw=yellow, opacity=0.3]{$\displaystyle\frac{1}{2}$}$
; Version 3: overwrites text; does not work \emph{within} math mode or across lines\\
\highlight{word} \highlight{$\frac{1}{2}$}
\highlight{$\displaystyle\frac{1}{2}$} $\highlight{$\displaystyle\frac{1}{2}$}$;
Version 4: overwrites text
\end{document}

答案1

您可以为节点命名并将其与该基线对齐:

在此处输入图片描述

笔记:

代码:

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[american,ngerman]{babel}
\usepackage{tikz}

\newcommand*{\yellowemph}[1]{%
  \tikz[baseline=(X.base)] \node[rectangle, fill=yellow, rounded corners, inner sep=0.3mm] (X) {#1};%
}

\begin{document}
Just some \yellowemph{dummy} text to see what happens.
\end{document}

答案2

像彼得的回答一样,但没有为节点添加新名称

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[american,ngerman]{babel}
\usepackage{tikz}

\newcommand*{\yellowemph}[1]{%
\tikz[baseline]\node[rectangle, fill=yellow, rounded corners, inner sep=0.3mm,anchor=base]{#1};%
}
\begin{document}
Just some \yellowemph{dummy} text to see what happens.
\end{document} 

在此处输入图片描述

答案3

这是我目前为此使用的代码。我主要用它突出显示(或淡化)演示文稿的短片段,因此它不适合处理长片段(尽管它可以合理地尝试),并且几乎肯定会因分页符而出错。它的主要缺点是它不会自动调整到文本的大小,因为它不能通过将文本放在框中来工作。相反,它会在文本下方(突出显示,淡化)绘制突出显示。因此,您应该更多地将其视为使用荧光笔突出显示文本,而不是绘制框并填充它。当然,您可以使用更粗的笔,并且有按键来自定义这些内容。

有三种变体:荧光笔、渐变笔和垂直荧光笔。每种笔的工作原理都是标记要标记的部分的开始和结束。然后 TikZ 会从开始到结束绘制荧光笔颜色的粗线。如果位移允许,线条会换行(垂直荧光笔的情况是垂直的)。荧光笔推子是绘制线条的时间。对于荧光笔,线条从起始位置发出,有效地将它们放置在文本下方。对于渐变器,线条从结束位置发出,将它们放置在文本上方。

以下是一个示例:

简单的荧光笔代码

这是代码。我尝试让它在 Beamer 和非 Beamer 上工作。在 Beamer 中,你可以做类似的事情\highlight<2>{only on slide 2}。有两种方法可以调用突出显示。一种是说\highlight{text to be highlighted}另一种是说\hlstart text to be highlighted \hlend。有时后者会提供更多的灵活性(上面的垂直突出显示就是以这种方式完成的)。

这是代码。该amsmath包仅适用于bmatrix环境。上面的示例实际上是使用beamer(并取消注释frame环境)生成的。

\documentclass{article}
%\url{http://tex.stackexchange.com/q/46434/86}
\usepackage{amsmath}
\usepackage{tikz}

\makeatletter

%
% Highlighter code
%

\tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    save picture id=#1,
  },
  save picture id/.code={%
    \edef\pgf@temp{#1}%
    \immediate\write\pgfutil@auxout{%
      \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
  }
}

\def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \@ifundefined{save@pt@#1}{%
    \pgfpointorigin
  }{%
  \pgfsys@getposition{\csname save@pt@#1\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}

\newcounter{highlight}
\newcommand{\hlstart}{\tikz[remember picture with id=hlstart\the\value{highlight},baseline=-0.7ex];\hl@start}
\newcommand{\hlend}{\tikz[remember picture with id=hlend\the\value{highlight},baseline=-0.7ex];\hl@end\stepcounter{highlight}}
\newcommand{\fdstart}{\tikz[remember picture with id=hlstart\the\value{highlight},baseline=-0.7ex];\fd@start}
\newcommand{\fdend}{\tikz[remember picture with id=hlend\the\value{highlight},baseline=-0.7ex];\fd@end\stepcounter{highlight}}
\newcommand{\vlstart}{\tikz[remember picture with id=hlstart\the\value{highlight},baseline=-1em];\vl@start}
\newcommand{\vlend}{\tikz[remember picture with id=hlend\the\value{highlight},baseline=0.3ex];\vl@end\stepcounter{highlight}}

\newcommand{\hl@start}[1][]{%
  \hl@draw{highlighter}{#1}{\the\value{highlight}}}

\newcommand{\hl@end}{}

\newcommand{\fd@start}[1][]{%
  \def\fd@args{#1}}

\newcommand{\fd@end}{\def\@tempa{\hl@draw{fader}}\expandafter\@tempa\expandafter{\fd@args}{\the\value{highlight}}\def\fd@args{}}

\newcommand{\vl@start}[1][]{%
  \vl@draw{highlighter}{#1}{\the\value{highlight}}}

\newcommand{\vl@end}{}


\def\hl@sets{%
  \edef\hl@sx{\the\pgf@x}%
  \edef\hl@sy{\the\pgf@y}%
}
\def\hl@sete{%
  \edef\hl@ex{\the\pgf@x}%
  \edef\hl@ey{\the\pgf@y}%
}

\@ifclassloaded{beamer}{

\def\page@node{
  \path (current page.south east)
      ++(-\beamer@rightmargin,\footheight)
  node[
    minimum width=\textwidth,
    minimum height=\textheight,
    anchor=south east
  ] (page) {};
}

}{

  \def\page@node{
    \path (current page.north west)
    ++(\hoffset + 1in + \oddsidemargin + \leftskip,\voffset + 1in + \topmargin + \headheight + \headsep)
    node[
      minimum width=\textwidth - \leftskip - \rightskip,
      minimum height=\textheight,
      anchor=north west
    ] (page) {};
  }

}

\newcommand{\hl@draw}[3]{%
  \begin{tikzpicture}[remember picture,overlay]%
  \page@node
  \tikzset{#2,highlight=#1,every path/.append style={highlight=#1}}%
  \pgfmathsetlengthmacro{\hl@width}{\the\pgflinewidth - 1pt}%
  \coordinate (hlstart) at (pic cs:hlstart#3);
  \coordinate (hlend) at (pic cs:hlend#3);
  \tikz@scan@one@point\hl@sets(pic cs:hlstart#3)
  \tikz@scan@one@point\hl@sete(pic cs:hlend#3)
  \ifdim\hl@sy=\hl@ey\relax
  \draw (hlstart) -- (hlend);
  \else
  \draw (hlstart) -- (hlstart -| page.east);
  \pgfmathsetlengthmacro{\hl@sy}{\hl@sy -\hl@width}%
  \pgfmathsetlengthmacro{\hl@ey}{\hl@ey +\hl@width}%
  \loop\ifdim\hl@sy>\hl@ey\relax
  \draw (0,\hl@sy -| page.west) -- (0,\hl@sy -| page.east);
  \pgfmathsetlengthmacro{\hl@sy}{\hl@sy -\hl@width}%
  \repeat
  \draw (hlend -| page.west) -- (hlend);
  \fi
  \end{tikzpicture}%
}

\newcommand{\vl@draw}[3]{%
  \begin{tikzpicture}[remember picture,overlay]%
  \page@node
  \tikzset{#2,highlight=#1,every path/.append style={highlight=#1}}%
  \pgfmathsetlengthmacro{\hl@width}{\the\pgflinewidth - 1pt}%
  \coordinate (hlstart) at (pic cs:hlstart#3);
  \coordinate (hlend) at (pic cs:hlend#3);
  \tikz@scan@one@point\hl@sets(pic cs:hlstart#3)
  \tikz@scan@one@point\hl@sete(pic cs:hlend#3)
  \ifdim\hl@sx=\hl@ex\relax
  \draw (hlstart) -- (hlend);
  \else
  \draw (hlstart) -- (hlstart |- page.south);
  \pgfmathsetlengthmacro{\hl@sx}{\hl@sx -\hl@width}%
  \pgfmathsetlengthmacro{\hl@ex}{\hl@ex +\hl@width}%
  \loop\ifdim\hl@sx>\hl@ex\relax
  \draw (\hl@sx,0 |- page.north) -- (\hl@sx,0 |- page.south);
  \pgfmathsetlengthmacro{\hl@sx}{\hl@sx -\hl@width}%
  \repeat
  \draw (hlend |- page.north) -- (hlend);
  \fi
  \end{tikzpicture}%
}

\tikzset{%
  highlight/.default=highlighter,
  highlight/.style={
    color=\pgfkeysvalueof{/tikz/#1 colour},
    line width=\pgfkeysvalueof{/tikz/#1 width},
    line cap=\pgfkeysvalueof{/tikz/#1 cap},
    opacity=\pgfkeysvalueof{/tikz/#1 opacity},
  },
  highlighter colour/.initial=yellow,
  highlighter width/.initial=12pt,
  highlighter cap/.initial=butt,
  highlighter opacity/.initial=1,
  fader colour/.initial=gray,
  fader width/.initial=12pt,
  fader cap/.initial=butt,
  fader opacity/.initial=.5,
}



\@ifclassloaded{beamer}{

%% Beamer variants

\setbeamercolor{highlighted text}{bg=yellow}
\setbeamercolor{faded text}{fg=gray}

\newcommand<>{\highlight}[2][]{%
  \only#3{\hlstart[#1]}#2\only#3{\hlend}}

\newcommand<>{\fade}[2][]{%
  \only#3{\fdstart[#1]}#2\only#3{\fdend}}

\newcommand<>{\vhighlight}[2][]{%
  \only#3{\vlstart[#1]}#2\only#3{\vlend}}

}{

\newcommand{\highlight}[2][]{%
\hlstart[#1]#2\hlend}

\newcommand{\fade}[2][]{%
\fdstart[#1]#2\fdend}

\newcommand{\vhighlight}[2][]{%
\vlstart[#1]#2\vlend}

}

\makeatother

\begin{document}
%\begin{frame}

This is some \highlight{highlighted} text.  With displaystyle maths, we might have to adjust the line width to get \highlight[highlighter width=24pt]{\(\displaystyle \frac12\)}.

\highlight[highlighter cap=round]{For a very long line, we get basic line wrapping.  It's a little creaky around the edges so may not be as robust as it could be.}

Sometimes, though, one wants to \fade[fader colour=white]{fade} some text.

Vertical highlighting is useful in matrices:
%
\[
  \begin{bmatrix}
  1 & \vlstart 2 & 3 \\
  4 & \vlend 5 & 6
  \end{bmatrix}
\]

%\end{frame}
\end{document}

相关内容