下面的代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
This is some text: \tikzmarknode{Foo}{foo}.
And this is some text in between that the filling partially covers
And this is another node:\tikzmarknode{Bar}{bar}
\begin{tikzpicture}[remember picture, overlay]
\draw[draw=none, fill=red] (Foo.north west)
-- (Bar.south west)
-- (Bar.south east)
-- (Foo.north east)
-- cycle;
\end{tikzpicture}
\end{document}
即使我降低填充不透明度,彩色层仍然会位于文本上方。
我怎样才能将其放在文本下方?
答案1
这展示了如何使用普通的旧 tikzmark 来执行此操作。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
\draw[draw=red, thick] (pic cs:Foo) -- (pic cs:Bar);
\end{tikzpicture}%
This is some text: \tikzmark{Foo}
And this is some text in between that the filling partially covers
And this is another node:\tikzmark{Bar}
\end{document}
这显示了如何使用保存框执行此操作。坐标(相对于原点)是全局保存的,尽管\pgfsyspdfmark
在使用保存框之前不会写入辅助文件。无论如何,这需要运行两次。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newsavebox{\tempbox}
\begin{document}
\savebox{\tempbox}{\begin{minipage}{\textwidth}
This is some text: \tikzmarknode{Foo}{foo}.
And this is some text in between that the filling partially covers
And this is another node:\tikzmarknode{Bar}{bar}
\end{minipage}}%
\noindent\begin{tikzpicture}[remember picture, overlay]
\draw[draw=none, fill=red] (Foo.north west)
-- (Bar.south west)
-- (Bar.south east)
-- (Foo.north east)
-- cycle;
\end{tikzpicture}\usebox\tempbox
\end{document}
答案2
只有整个 TikZ 图片的位置才会保存到辅助文件中,节点的尺寸只有在构建后才知道。
这是一种仅在所有节点都已构建(然后已知)后才构建 TikZ 图片的方法。通过将此 TikZ 图片添加到背景 shipout 挂钩,它将被放置在普通文本后面。
代碼(出發)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\NewDocumentEnvironment{tikz-bg}{O{} +b}{%
\AddToHookNext{shipout/background}{%
\put(0,0){%
\begin{tikzpicture}[remember picture,overlay,#1]%
#2%
\end{tikzpicture}}}}{}
\begin{document}
This is some text: \tikzmarknode{Foo}{foo}.
And this is some text in between that the filling partially covers
And this is another node:\tikzmarknode{Bar}{bar}
\begin{tikz-bg}
\fill[red] (Foo.north west) -- (Bar.south west)
-- (Bar.south east) -- (Foo.north east) -- cycle;
\end{tikz-bg}
\end{document}
该tikzmark
库支持保存节点(即它在 TikZ 图片中的位置及其锚点):
每个节点都需要事先知道需要使用哪个节点,save node
而TikZ图片需要的节点save nodes to file
。
由于某种原因,所有内容都向下移动,可能是某些内容未正确保存或保存在正确的位置。评论中的 hack 似乎是解决潜在问题的一种解决方法。
代码(已保存的节点)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\tikzset{
every tikzmarknode picture/.style=save nodes to file,
restore nodes from file,
save node/.append style={anchor=center}% hack
}
\begin{document}
\tikz[remember picture, overlay]
\fill[red] (Foo.north west) -- (Bar.south west)
-- (Bar.south east) -- (Foo.north east) -- cycle;%
This is some text: \tikzmarknode[save node]{Foo}{foo}.
And this is some text in between that the filling partially covers
And this is another node: \tikzmarknode[save node]{Bar}{bar}
\end{document}
答案3
这是一个简单的解决方案,使用blend mode=mutliply
samcarter_is_at_topanswers.xyz 评论中的建议。当用黑色文本覆盖图像时,此方法效果很好,因为黑色 ( color=0
) 与任何其他颜色的乘积仍然是黑色。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
This is some text: \tikzmarknode{Foo}{foo}.
And this is some text in between that the filling partially covers
And this is another node:\tikzmarknode{Bar}{bar}
\begin{tikzpicture}[remember picture, overlay, blend mode=multiply]
\draw[draw=none, fill=red] (Foo.north west)
-- (Bar.south west)
-- (Bar.south east)
-- (Foo.north east)
-- cycle;
\end{tikzpicture}
\end{document}
以下是输出结果