我最近被提及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
ing,同時使文字與突出顯示的顏色相同(也稱為不可見);
2)備份到原來的位置(水平通過tabto
包)和垂直通過\vspace
測量minipage
;
3) 在先前突出顯示的內容上以清晰的形式重新輸入文本,從而將頁面位置保留在應有的位置。
當注意到輸出\highlight
和明文文字之間存在一些字距差異時,對此解決方案進行了編輯。預製\highlight
現在以空白顏色完成,以避免雙線文字問題。
這種方法不能跨頁面邊界工作。在 MWE 中,它可以跨段落邊界工作,但我想,除非\raggedbottom
使用了,否則在頁面上插入的膠水可能會弄亂它。
事實上,注意到一些字偶距問題促使對我的程式碼進行編輯,這使得在某些情況下,換行符\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}
下圖顯示了縮放。