我天真地以为这将是一个简单的修复突出显示文本的方法 - 是的,有ulem.sty
,soul.sty
但它们施加了一些我不想处理的限制。
我认为使用类似的东西会很有用tikzmarks
。事实确实如此,除了阴影矩形,虽然表面上看起来背景层,实际上是叠印了它们应该突出显示的文本。
这能行吗?我错过了什么?(使用 TexLive2022,昨天用 Aquamacs/Skim 更新)。我应该指出,这将始终在单个页面上使用。
\documentclass{article}
\usepackage[papersize={5.5in,8.5in},margin=0.6in]{geometry}
\usepackage{tikzpagenodes}
\usetikzlibrary{backgrounds}
%% |=====8><-----| %%
\newcounter{shadenum}
\NewDocumentCommand{\btshadepar}{}{%
\stepcounter{shadenum}%
\leavevmode
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0pt](bps-\theshadenum){\strut};
\end{tikzpicture}%
}%
\NewDocumentCommand{\etshadepar}{}{%
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0pt,outer sep=-3pt](eps-\theshadenum){\strut};
\begin{scope}{on background layer}
\fill[yellow!20] ([yshift=3pt]bps-\theshadenum.north) rectangle
(bps-\theshadenum.south -| current page text area.east);% first line
\fill[yellow!20] ([yshift=3pt]eps-\theshadenum.north) rectangle
(eps-\theshadenum.south -| current page text area.west);% last line
\fill[yellow!20] ([yshift=3pt]bps-\theshadenum.south -| current page text area.west) rectangle
(eps-\theshadenum.north -| current page text area.east);% body of paragraph
\end{scope}
\end{tikzpicture}%
}%
\begin{document}
\thispagestyle{empty}
This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \tikz{\draw[fill=red] (0,0) rectangle (1ex,1ex);} gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. \[x^2+y^2=z^2\] This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.
\btshadepar This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \tikz{\draw[fill=red] (0,0) rectangle (1ex,1ex);} gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. \[x^2+y^2=z^2\] This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.\etshadepar
\end{document}
更新
我修改了文本以表明代码必须处理的一般问题(显示数学、小tikz
对象)。到目前为止,所有回复(@samcarter_is_at_topanswers.xyz 和 @Qrrbrbirlbel)都完美地解决了这个问题。
答案1
您不需要尝试将 tikzpicture 放入背景中,而是可以将其留在文本上方并进行更改,blend mode
以使文本仍然可见:
\documentclass{article}
\usepackage[papersize={5.5in,8.5in},margin=0.6in]{geometry}
\usepackage{tikzpagenodes}
\usetikzlibrary{backgrounds}
%% |=====8><-----| %%
\newcounter{shadenum}
\NewDocumentCommand{\btshadepar}{}{%
\stepcounter{shadenum}%
\leavevmode
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0pt](bps-\theshadenum){\strut};
\end{tikzpicture}%
}%
\NewDocumentCommand{\etshadepar}{}{%
\begin{tikzpicture}[remember picture,overlay,blend mode=multiply]
\node[inner sep=0pt,outer sep=-3pt](eps-\theshadenum){\strut};
\fill[yellow!20] ([yshift=3pt]bps-\theshadenum.north) rectangle
(bps-\theshadenum.south -| current page text area.east);% first line
\fill[yellow!20] ([yshift=3pt]eps-\theshadenum.north) rectangle
(eps-\theshadenum.south -| current page text area.west);% last line
\fill[yellow!20] ([yshift=3pt]bps-\theshadenum.south -| current page text area.west) rectangle
(eps-\theshadenum.north -| current page text area.east);% body of paragraph
\end{tikzpicture}%
}%
\begin{document}
\thispagestyle{empty}
This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.
\btshadepar This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.\etshadepar
\end{document}
答案2
PGF 层仅适用于图片本身,它仍然放在文本之上(因为您在最后绘制它)。
然后我们在文本前画出来。这里,tikzmark
库确实提供了有用的宏
\iftikzmark{<name>}{<true>}{<false>}
你可以用它测试是否<name>
已经存在(即不是在第一次编译运行时)然后执行适当的参数。
\iftikzmarkoncurrentpage
如果您想检查结尾是否可能在另一页上,该库还提供...
这里我只在段落末尾使用了一个标记,开头不需要标记,我们可以直接从中抽取(0, 0)
。\pgfmark
只需要一个标记,因为我们不用这个标记做任何事情,只是保存它的位置。这样开销就小了一点。
由于或多或少插入了高度和深度\strut
的不可见规则, 因此我将在答案中直接使用这些值。显然,如果您在开头和结尾使用不同的字体(大小),这将不再令人满意。.7\baselineskip
.3\baselineskip
代码
\documentclass{article}
\usepackage[papersize={5.5in,8.5in},margin=0.6in]{geometry}
\usepackage{tikzpagenodes}
\usetikzlibrary{tikzmark}
\newcounter{shadenum}
\NewDocumentCommand{\btshadepar}{O{yellow!20}}{%
\stepcounter{shadenum}%
\leavevmode
\iftikzmark{eps-\theshadenum}{%
\tikz[remember picture,overlay,baseline=0pt]
\fill[#1] (0,+.7\baselineskip) rectangle
(0,+-.3\baselineskip-|current page text area.east)
(0,+-.3\baselineskip-|current page text area.west)
rectangle ([yshift=+.7\baselineskip]perpendicular cs:
horizontal line through={(pic cs:eps-\theshadenum)},
vertical line through={(current page text area.east)})
coordinate (@)
(@-|current page text area.west) rectangle
([yshift=+-.3\baselineskip]pic cs:eps-\theshadenum);
}{}%
}
\NewDocumentCommand{\etshadepar}{}{%
\pgfmark{eps-\theshadenum}}%
\begin{document}
\thispagestyle{empty}
This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.
\btshadepar This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.\etshadepar
\end{document}
输出
答案3
如果您能够切换到 lualatex,您可以使用该lua-ul
包。此包避免了 soul 或 ulem 等类似包的大部分限制。
% !TeX TS-program = lualatex
\documentclass{article}
\usepackage[papersize={5.5in,8.5in},margin=0.6in]{geometry}
\usepackage{lua-ul}
\usepackage{tikz}
\usepackage{luacolor}
\begin{document}
This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \tikz{\draw[fill=red] (0,0) rectangle (1ex,1ex);} gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. \[x^2+y^2=z^2\] This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.
\highLight{ This is a gigantic paragraph that means \emph{absolutely} nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a \tikz{\draw[fill=red] (0,0) rectangle (1ex,1ex);} gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. \[x^2+y^2=z^2\] This is a \textbf{gigantic} paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all. This is a gigantic paragraph that means absolutely nothing at all.}
\end{document}