![tikz:构建幻影路径](https://linux22.com/image/366806/tikz%EF%BC%9A%E6%9E%84%E5%BB%BA%E5%B9%BB%E5%BD%B1%E8%B7%AF%E5%BE%84.png)
我如何构造一条路径(曲线、直线)但不绘制它?
例如我想构造以下路径
\draw[name path = phantom] plot coordinates {
(0, 2)
(2, 5)
(3, 7)
} ;
但不希望显示实际的线条。
答案1
该命令\draw
实际上是的快捷方式\path[draw]
,如果您删除该draw
选项,并只写入
\path
尽管 TikZ 会考虑其尺寸,但您仍会得到一条未绘制的路径。
可能的选择有:
\draw[draw=none]
(这完全等同于一个\path
命令)你说要画一条没有任何颜色的路径,或者
\draw[opacity=0]
你说要绘制一条透明路径。有关差异的详细说明,请参阅: draw=none 和 draw opacity=0 之间的区别 (fill=none 和 fill opacity=0 之间也一样)?
这里是具有各种选项的 MWE,我在tikzpicture
s 周围放了一个框架,只是为了表明,即使没有绘制路径,也会考虑它们的尺寸。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usepackage{framed}
\begin{document}
This is your command:
\begin{framed}
\begin{tikzpicture}
\draw[name path = phantom] plot coordinates {
(0, 2)
(2, 5)
(3, 7)
};
\end{tikzpicture}
\end{framed}
This is with \texttt{\textbackslash path} only:
\begin{framed}
\begin{tikzpicture}
\path plot coordinates {
(0, 2)
(2, 5)
(3, 7)
} ;
\end{tikzpicture}
\end{framed}
\newpage
This is with \texttt{\textbackslash draw[draw=none]}:
\begin{framed}
\begin{tikzpicture}
\draw[draw=none] plot coordinates {
(0, 2)
(2, 5)
(3, 7)
} ;
\end{tikzpicture}
\end{framed}
This is with \texttt{\textbackslash draw[opacity=0]}:
\begin{framed}
\begin{tikzpicture}
\draw[opacity=0] plot coordinates {
(0, 2)
(2, 5)
(3, 7)
} ;
\end{tikzpicture}
\end{framed}
\end{document}
答案2
延伸至列表CarLaTeX。
丢弃层
源代码TDS:tex/generic/pgf/basiclayer/pgfcorelayers.code.tex
揭示了一个被丢弃的特殊层(它没有在 TikZ 手册 3.0.1a 中记录):
\def\pgfdiscardlayername{discard}
\def\pgfonlayer@assert@is@active{%
\ifx\pgfonlayer@name\pgfdiscardlayername
% this special layer name can be used as /dev/null without
% warning.
...
下一个文件将问题的图与圆相交,在中心添加一个节点,并从交点到节点左侧和右侧画一条线:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\path (2, 5) node[draw] (hw) {Hello World};
\draw[name path=phantom] plot coordinates {
(0, 2)
(2, 5)
(3, 7)
};
\draw[name path=circle] (2, 5) circle[radius=2];
\draw[name intersections={of=phantom and circle}]
(intersection-1) -- (hw.south west)
-- (hw.north east) -- (intersection-2)
;
\end{tikzpicture}
\end{document}
现在,我们把问题的情节和节点放在丢弃层上来隐藏它们:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\pgfdeclarelayer{discard}
\begin{pgfonlayer}{discard}
\path (2, 5) node (hw) {Hello World}; % or node[draw]
\path[name path=phantom] plot coordinates { % or \draw
(0, 2)
(2, 5)
(3, 7)
};
\end{pgfonlayer}
\draw[name path=circle] (2, 5) circle[radius=2];
\draw[name intersections={of=phantom and circle}]
(intersection-1) -- (hw.south west)
-- (hw.north east) -- (intersection-2)
;
\end{tikzpicture}
\end{document}
由于丢弃的层未添加到输出文件中,因此 PDF 文件大小从 11696 字节缩小到 1198 字节(两者:pdflatex
带有\pagestyle{empty}
)。