我知道如何使用 TikZeven and odd
规则,但我需要将其应用于节点文本。比较
我需要将文本打孔出来,以便当我在背景上使用生成的 pdf 时,背景将在字形所在的位置可见。
\documentclass{standalone}
\usepackage{palatino}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\path[use as bounding box] (-.1,-.1) rectangle (2.4,1.1);
\filldraw[fill=blue,even odd rule]
(0,0) rectangle (1,1) (0.5,0.5) circle (0.4cm);
\begin{scope}[xshift=1.3cm]
\filldraw[fill=blue,even odd rule]
% the node text is black (I guess foreground colour)
(0,0) rectangle (1,1) (0.5,0.5) node {as};
\end{scope}
\end{tikzpicture}
\end{document}
我也尝试过node[text opacity=0]
,但是这会使字形消失。
我想我可以将文本导出为 svg,并将tikz2svg
其用作路径,但这很不方便。
答案1
我猜这只是“概念验证”,而不是任何一种实用的解决方案(当然它不实用)。它用低级PDF
文字做了一些相当顽皮的事情:
\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\tikzset{
outline text/.style={
execute at begin node={%
\pgfsetfillopacity{0}%
\pgfsetlinewidth{\pgflinewidth}%
\pgfsetstrokecolor{#1}%
\special{pdf:literal 1 Tr }%
}
}
}
\begin{tikzfadingfrompicture}[name=Aa]
\node [text=transparent!100, font=\Large\bfseries,
minimum size=1cm, fill=transparent!0] {Aa};
\end{tikzfadingfrompicture}
\begin{document}
\begin{tikzpicture}
\path [left color=yellow, right color=red, middle color=purple,
shading angle=45] circle [radius=2cm/3];
\node [fill=blue!20,fit fading=false, draw, minimum size=1cm,
path fading=Aa, font=\Large\bfseries, outline text=black] {Aa};
\end{tikzpicture}
\end{document}
这是一种自动版本,它(也许并不奇怪)需要一些低级黑客攻击。
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{fadings}
\makeatletter
\tikzset{
outline text/.style={
execute at begin node={%
\pgfsetfillopacity{0}%
\pgfsetlinewidth{\pgflinewidth}%
\pgfsetstrokecolor{#1}%
\special{pdf:literal 1 Tr }%
},
},
knockout text fading/.code={%
\tikz@addmode{%
% Interrupt the picture to create a fading.
\pgfinterruptpicture%
\let\tikz@atbegin@node=\relax%
\begin{tikzfadingfrompicture}[name=.]
\node [node contents=, #1,text=transparent!100, fill=transparent!0];%
\xdef\fadingboundingbox{{\noexpand\pgfpoint{\the\pgf@picminx}{\the\pgf@picminy}}%
{\noexpand\pgfpoint{\the\pgf@picmaxx}{\the\pgf@picmaxy}}}%
\expandafter\pgfpathrectanglecorners\fadingboundingbox%
\pgfusepath{discard}%
\end{tikzfadingfrompicture}%
\endpgfinterruptpicture%
% Make the fading happen.
\def\tikz@path@fading{.}%
\tikz@mode@fade@pathtrue%
\tikz@fade@adjustfalse%
\pgfpointscale{0.5}{\expandafter\pgfpointadd\fadingboundingbox}%
\def\tikz@fade@transform{shift={(\the\pgf@x,\the\pgf@y)}}%
}%
\tikzset{#1}%
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[line join=round]
\path [left color=yellow, right color=red, middle color=purple,
shading angle=45] circle [radius=2cm];
\node [outline text=black,
knockout text fading={
rotate=-45,
fill=blue!20, draw, text width=4.5cm, align=center,
font=\sffamily\Large\bfseries,
node contents={The quick brown fox jumps over the lazy dog}
}];
\end{tikzpicture}
\end{document}
答案2
我在TikZ & PGF 手册这可能会对你有所帮助。我已经编辑了我找到的两个例子。第一张图片基于第节中的示例23.4.1 创建衰落,另外两个基于第节中的示例23.5 透明度组在 TikZ 和 PGF 手册中。
不幸的是,第二和第三个示例中的文本仅显示在 Adobe Reader 中(它不适用于 TeXworks 和 Evince)。
您可以下载我在第三个示例中使用的背景图片这里。
\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{fadings, patterns}
\newcommand{\normalfontsize}{\fontsize{10pt}{12pt}\selectfont}
\newcommand{\largefontsize}{\fontsize{1.5cm}{1.5cm}\selectfont}
\begin{document}
\normalfontsize
Example \#1
\largefontsize
\begin{tikzfadingfrompicture}[name=mypath]
\node [text=transparent!20, font=\bfseries] {Aa};
\end{tikzfadingfrompicture}
\begin{tikzpicture}
\fill [black!20] (-1, -1) rectangle (1, 1);
\pattern [pattern=checkerboard,%
pattern color=black!30] (-1, -1) rectangle (1, 1);
\shade[path fading=mypath,%
fit fading=false,%
left color=lime,%
right color=orange] (-1, -1) rectangle (1, 1);
\end{tikzpicture}
\normalfontsize
Example \#2
\largefontsize
\begin{tikzpicture}
\shade [left color=lime, right color=orange] (-1, -1) rectangle (1, 1);
\begin{scope}[transparency group=knockout]
\filldraw[lightgray] (-1, -1) rectangle (1, 1);
\node[opacity=0, font=\bfseries] {Aa};
\end{scope}
\end{tikzpicture}
\normalfontsize
Example \#3
\largefontsize
\begin{tikzpicture}
\node[inner sep=0pt, outer sep=0pt] (background) at (0, 0)%
{\includegraphics[width=2cm, height=2cm]{bg.png}};
\begin{scope}[transparency group=knockout]
\filldraw[lightgray] (-1, -1) rectangle (1, 1);
\node[opacity=0, font=\bfseries] {Aa};
\end{scope}
\end{tikzpicture}
\end{document}