坐标/节点命令不能与 \draw to 一起使用吗?

坐标/节点命令不能与 \draw to 一起使用吗?

我想在两点之间画一条曲线,但如果不使用 --,我就无法定义坐标。这是故意的还是我做错了什么?

因此,为了更好地解释。如果我使用以下代码:

\draw ($ (A) - (-0.5,0.5) $) -- ($ (A) - (5,10) $) coordinate[pos=0.4] (E);

我得到一行使用:

\draw[color=red] (0,0) -- (E.center);

但如果我使用这个(我想要的)则不行:

\draw ($ (A) - (-0.5,0.5) $) to [out=270,in=0] ($ (A) - (5,10) $) coordinate[pos=0.4] (E);

难道我做错了什么?

完整代码:

\usepackage{tikz}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\title{TikZ-test}

\tikzset{decorate sep/.style 2 args=
{decorate,decoration={shape backgrounds,shape=circle,shape size=#1,shape sep=#2}}}

\begin{tikzpicture}
\node (A) at (-5,-0.25){};
\node (B) at (5,-0.25){};
\node (C) at (-1.2,1){};
\node (D) at (1.2,1){};

\draw[line width=1mm] (A.center) -- (B.center);

\draw[line width=1mm] (C) -- (D);
\draw[line width=1mm] (0,1) -- (0,2);
\draw[line width=1mm] (A) -- (-1,0) -- (1,0) -- (B);
\draw[line width=1mm,fill=white] (0,0) circle (1cm);

\draw ($ (A) - (-0.5,0.5) $) to [out=270,in=0] ($ (A) - (5,10) $) coordinate[pos=0.4] (E);

\draw ($ (B) - (0.5,0.5) $) to [out=270,in=0] ($ (B) - (5,10) $);

\draw[color=red] (0,0) -- (E.center);
\end{tikzpicture}

答案1

pos语法不适用于任意曲线,但您当然可以使用decorations.markings它来使其工作。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[mark pos/.style args={#1 with #2}{postaction={decorate,
decoration={markings,mark=at position #1 with {#2}}}}]
\node (A) at (-5,-0.25){};
\node (B) at (5,-0.25){};
\node (C) at (-1.2,1){};
\node (D) at (1.2,1){};

\draw[line width=1mm] (A.center) -- (B.center);

\draw[line width=1mm] (C) -- (D);
\draw[line width=1mm] (0,1) -- (0,2);
\draw[line width=1mm] (A) -- (-1,0) -- (1,0) -- (B);
\draw[line width=1mm,fill=white] (0,0) circle (1cm);

\draw[mark pos=0.4 with {\coordinate (E);}] ($ (A) - (-0.5,0.5) $) to [out=270,in=0] ($ (A) - (5,10) $);

\draw ($ (B) - (0.5,0.5) $) to [out=270,in=0] ($ (B) - (5,10) $);

\draw[color=red] (0,0) -- (E.center);
\end{tikzpicture}
\end{document}

在此处输入图片描述

笔记您还可以在使用时将节点放置在路径内pos=0.5。(对于直线,--您可以更草率。).decorations.markings始终有效,也适用于更复杂的路径(除非路径具有高曲率)。

答案2

您犯了一个简单的语法错误。

可以将coordinates或放置nodes在“至路径”操作中,但为此,您必须将它们放置在操作的两个坐标之间(参见 Tikz 手册 3.1 第 161 页)

我引用:

路径上的节点. 可以添加节点由 to 操作构建的路径。为此,您需要指定 to 关键字和坐标之间的节点(如果有 操作,这些是第一位的)。(a)到节点 {x} (b) (通常)就像你写的那样(a)--节点 {x} (b),即把节点放在了上。

截屏

\documentclass[border=5mm]{standalone} 
\usepackage{tikz} 
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\begin{document} 



%\title{TikZ-test}

\tikzset{decorate sep/.style 2 args=
{decorate,decoration={shape backgrounds,shape=circle,shape size=#1,shape sep=#2}}}

\begin{tikzpicture}
\node (A) at (-5,-0.25){};
\node (B) at (5,-0.25){};
\node (C) at (-1.2,1){};
\node (D) at (1.2,1){};

\draw[line width=1mm] (A.center) -- (B.center);

\draw[line width=1mm] (C) -- (D);
\draw[line width=1mm] (0,1) -- (0,2);
\draw[line width=1mm] (A) -- (-1,0) -- (1,0) -- (B);
\draw[line width=1mm,fill=white] (0,0) circle (1cm);

\draw ($ (A) - (-0.5,0.5) $) to [out=270,in=0] coordinate[pos=0.4] (E)($ (A) - (5,10) $) ;

\draw ($ (B) - (0.5,0.5) $) to [out=270,in=0] ($ (B) - (5,10) $);

\draw[color=red] (0,0) -- (E.center);
\end{tikzpicture}

\end{document}

相关内容