我想显示一张图片(在示例中用红色矩形代替),但只有该图片的部分可见,由文本定义。因此,在此示例中,只有“TEST”后面的图片部分会出现在最终结果中。文本可能必须具有不透明度 = 0,但如何覆盖图片的其余部分?
\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\fill[red] (0,0) rectangle (10,5);
\node[font=\Huge] at (5,2.5) {TEST};
\end{tikzpicture}
\end{document}
答案1
这是使用该库的建议fadings
。请注意,我使用浅灰色背景来显示图片的大小。
代码:
\documentclass[margin=5mm]{standalone}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{fadings}
\newsavebox\picturebox
\begin{document}
\savebox\picturebox{\includegraphics[width=10cm]{picture}}
\begin{tikzfadingfrompicture}[name=fading text]
\node[
text=transparent!100,
minimum width=\wd\picturebox+2\pgflinewidth,
minimum height=\ht\picturebox+\dp\picturebox+2\pgflinewidth,
font=\bfseries\fontsize{95}{95}\selectfont,
fill=white
] {TEST};
\end{tikzfadingfrompicture}
\begin{tikzpicture}
%\clip(-5cm,-50pt)rectangle(5cm,50pt);
\node(p){\usebox\picturebox};
\fill[path fading=fading text,fit fading=false,
lightgray!10% <- maybe you want to change the color to white
](p.south west) rectangle (p.north east);
\end{tikzpicture}
\end{document}
如果我取消注释,\clip(-5cm,-50pt)rectangle(5cm,50pt);
我会得到
答案2
你是指这样的事情吗?
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\node[font=\Huge] (A) at (5,2.5) {TEST};
\begin{scope}[on background layer]
\clip(A.south west) rectangle (A.north east);
\fill[red] (0,0) rectangle (10,5);
\end{scope}
\end{tikzpicture}
\end{document}