knots:\strand 命令似乎对 \clip 命令有奇怪的效果

knots:\strand 命令似乎对 \clip 命令有奇怪的效果

我正在绘制一个结点图,并使用命令来帮助阴影区域。但是,环境命令\clip似乎对有一些奇怪的影响。\strandknot\clip

例如,正确的结果应该是(我们称之为图 1):期望结果

% Diagram 1
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{knots}
\usetikzlibrary{hobby}
\begin{document}
    \definecolor{skyblue}{RGB}{60,120,234}
    \scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
        \pgfdeclarelayer{foreground}
        \pgfsetlayers{main,foreground}
        \begin{pgfonlayer}{foreground}
            \begin{scope}
                \clip (-2,3) .. (0,2) .. (0.4,1)
                            .. (0,0) .. (-0.4,-1)
                            .. (0,-2) .. (2,-3)
                            .. (4.8,0) .. (2,3)
                            .. (0,2) .. (-0.4,1) -- (-2,3);
                \clip (-2,-3) .. (0,-2) .. (0.4,-1)
                            .. (0,0) .. (-0.4,1)
                            .. (0,2) .. (2,3)
                            .. (4.8,0) .. (2,-3)
                            .. (0,-2) .. (-0.4,-1) -- (-2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}
            \begin{scope}
                \clip (2,3) .. (0,2) .. (-0.4,1)
                            .. (0,0) .. (0.4,-1)
                            .. (0,-2) .. (-2,-3)
                            .. (-4.8,0) .. (-2,3)
                            .. (0,2) .. (0.4,1) -- (2,3);
                \clip (2,-3) .. (0,-2) .. (-0.4,-1)
                            .. (0,0) .. (0.4,1)
                            .. (0,2) .. (-2,3)
                            .. (-4.8,0) .. (-2,-3)
                            .. (0,-2) .. (0.4,-1) -- (2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}    
            \draw (-2.2,0) node[scale=3] {$+$};
            \draw (2.2,0) node[scale=3] {$-$};
            \draw (5,2.5) node[scale=3] {$M$};
        \end{pgfonlayer}
        \begin{knot}[
                        consider self intersections,
                        clip width=10,
                        clip radius=0.5cm,
                        ignore endpoint intersections=false,
                        flip crossing/.list={6,14}
                    ]
            \strand[very thick,black,closed] 
                    (0.4,1) .. (0,2) .. (-2,3) 
                            .. (-4.8,0) .. (-2,-3) 
                            .. (0,-2) .. (0.4,-1) 
                            .. (0,0) .. (-0.4,1) 
                            .. (0,2) .. (2,3) 
                            .. (4.8,0) .. (2,-3) 
                            .. (0,-2) .. (-0.4,-1) .. (0,0);
        \end{knot}
    \end{tikzpicture}}
\end{document}

\clip请注意 \strand

\clip但是,如果我切换包含和的代码片段的顺序\strand

% Diagram 2
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{knots}
\usetikzlibrary{hobby}
\begin{document}
    \definecolor{skyblue}{RGB}{60,120,234}
    \scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
        \pgfdeclarelayer{foreground}
        \pgfsetlayers{main,foreground}
        \begin{knot}[
                        consider self intersections,
                        clip width=10,
                        clip radius=0.5cm,
                        ignore endpoint intersections=false,
                        flip crossing/.list={6,14}
                    ]
            \strand[very thick,black,closed] 
                    (0.4,1) .. (0,2) .. (-2,3) 
                            .. (-4.8,0) .. (-2,-3) 
                            .. (0,-2) .. (0.4,-1) 
                            .. (0,0) .. (-0.4,1) 
                            .. (0,2) .. (2,3) 
                            .. (4.8,0) .. (2,-3) 
                            .. (0,-2) .. (-0.4,-1) .. (0,0);
        \end{knot}
        \begin{pgfonlayer}{foreground}
            \begin{scope}
                \clip (-2,3) .. (0,2) .. (0.4,1)
                            .. (0,0) .. (-0.4,-1)
                            .. (0,-2) .. (2,-3)
                            .. (4.8,0) .. (2,3)
                            .. (0,2) .. (-0.4,1) -- (-2,3);
                \clip (-2,-3) .. (0,-2) .. (0.4,-1)
                            .. (0,0) .. (-0.4,1)
                            .. (0,2) .. (2,3)
                            .. (4.8,0) .. (2,-3)
                            .. (0,-2) .. (-0.4,-1) -- (-2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}
            \begin{scope}
                \clip (2,3) .. (0,2) .. (-0.4,1)
                            .. (0,0) .. (0.4,-1)
                            .. (0,-2) .. (-2,-3)
                            .. (-4.8,0) .. (-2,3)
                            .. (0,2) .. (0.4,1) -- (2,3);
                \clip (2,-3) .. (0,-2) .. (-0.4,-1)
                            .. (0,0) .. (0.4,1)
                            .. (0,2) .. (-2,3)
                            .. (-4.8,0) .. (-2,-3)
                            .. (0,-2) .. (0.4,-1) -- (2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}    
            \draw (-2.2,0) node[scale=3] {$+$};
            \draw (2.2,0) node[scale=3] {$-$};
            \draw (5,2.5) node[scale=3] {$M$};
        \end{pgfonlayer}
    \end{tikzpicture}}
\end{document}

结果就变成了(我们称之为图 2): 坏形象 正如您所见,阴影并不适合曲线。

即使我使用代码来生成图 1(其中\clip位于 之前),如果代码之前有其他命令,\strand则仍会出现错误。可以通过重复以下代码来查看\strand\clip图 1两次:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{knots}
\usetikzlibrary{hobby}
\begin{document}
    \definecolor{skyblue}{RGB}{60,120,234}
    \scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
        \pgfdeclarelayer{foreground}
        \pgfsetlayers{main,foreground}
        \begin{pgfonlayer}{foreground}
            \begin{scope}
                \clip (-2,3) .. (0,2) .. (0.4,1)
                            .. (0,0) .. (-0.4,-1)
                            .. (0,-2) .. (2,-3)
                            .. (4.8,0) .. (2,3)
                            .. (0,2) .. (-0.4,1) -- (-2,3);
                \clip (-2,-3) .. (0,-2) .. (0.4,-1)
                            .. (0,0) .. (-0.4,1)
                            .. (0,2) .. (2,3)
                            .. (4.8,0) .. (2,-3)
                            .. (0,-2) .. (-0.4,-1) -- (-2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}
            \begin{scope}
                \clip (2,3) .. (0,2) .. (-0.4,1)
                            .. (0,0) .. (0.4,-1)
                            .. (0,-2) .. (-2,-3)
                            .. (-4.8,0) .. (-2,3)
                            .. (0,2) .. (0.4,1) -- (2,3);
                \clip (2,-3) .. (0,-2) .. (-0.4,-1)
                            .. (0,0) .. (0.4,1)
                            .. (0,2) .. (-2,3)
                            .. (-4.8,0) .. (-2,-3)
                            .. (0,-2) .. (0.4,-1) -- (2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}    
            \draw (-2.2,0) node[scale=3] {$+$};
            \draw (2.2,0) node[scale=3] {$-$};
            \draw (5,2.5) node[scale=3] {$M$};
        \end{pgfonlayer}
        \begin{knot}[
                        consider self intersections,
                        clip width=10,
                        clip radius=0.5cm,
                        ignore endpoint intersections=false,
                        flip crossing/.list={6,14}
                    ]
            \strand[very thick,black,closed] 
                    (0.4,1) .. (0,2) .. (-2,3) 
                            .. (-4.8,0) .. (-2,-3) 
                            .. (0,-2) .. (0.4,-1) 
                            .. (0,0) .. (-0.4,1) 
                            .. (0,2) .. (2,3) 
                            .. (4.8,0) .. (2,-3) 
                            .. (0,-2) .. (-0.4,-1) .. (0,0);
        \end{knot}
    \end{tikzpicture}}

    \scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
        \pgfdeclarelayer{foreground}
        \pgfsetlayers{main,foreground}
        \begin{pgfonlayer}{foreground}
            \begin{scope}
                \clip (-2,3) .. (0,2) .. (0.4,1)
                            .. (0,0) .. (-0.4,-1)
                            .. (0,-2) .. (2,-3)
                            .. (4.8,0) .. (2,3)
                            .. (0,2) .. (-0.4,1) -- (-2,3);
                \clip (-2,-3) .. (0,-2) .. (0.4,-1)
                            .. (0,0) .. (-0.4,1)
                            .. (0,2) .. (2,3)
                            .. (4.8,0) .. (2,-3)
                            .. (0,-2) .. (-0.4,-1) -- (-2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}
            \begin{scope}
                \clip (2,3) .. (0,2) .. (-0.4,1)
                            .. (0,0) .. (0.4,-1)
                            .. (0,-2) .. (-2,-3)
                            .. (-4.8,0) .. (-2,3)
                            .. (0,2) .. (0.4,1) -- (2,3);
                \clip (2,-3) .. (0,-2) .. (-0.4,-1)
                            .. (0,0) .. (0.4,1)
                            .. (0,2) .. (-2,3)
                            .. (-4.8,0) .. (-2,-3)
                            .. (0,-2) .. (0.4,-1) -- (2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}    
            \draw (-2.2,0) node[scale=3] {$+$};
            \draw (2.2,0) node[scale=3] {$-$};
            \draw (5,2.5) node[scale=3] {$M$};
        \end{pgfonlayer}
        \begin{knot}[
                        consider self intersections,
                        clip width=10,
                        clip radius=0.5cm,
                        ignore endpoint intersections=false,
                        flip crossing/.list={6,14}
                    ]
            \strand[very thick,black,closed] 
                    (0.4,1) .. (0,2) .. (-2,3) 
                            .. (-4.8,0) .. (-2,-3) 
                            .. (0,-2) .. (0.4,-1) 
                            .. (0,0) .. (-0.4,1) 
                            .. (0,2) .. (2,3) 
                            .. (4.8,0) .. (2,-3) 
                            .. (0,-2) .. (-0.4,-1) .. (0,0);
        \end{knot}
    \end{tikzpicture}}
\end{document}

在此处输入图片描述 你可以清楚地看到,尽管它们是由相同的TikZ代码,结果不一样。

有人能解释一下发生了什么吗如何修复这个问题? 提前致谢。

答案1

经过进一步测试,这不是 或 中的错误hobbyknots而是它们交互的“特性”,可能应该在某处记录下来。

要创建封闭的爱好曲线,可以输入:

\draw[closed] (0,0) .. (1,1) .. (0,2) .. (-1,1) .. (0,0);

并且closed表示该曲线应被视为封闭曲线,而不是端点恰好位于同一点的曲线(hobby有关差异的更多信息,请参阅文档)。

closed现在命令上的选项\draw有点不合适,因为 TikZ 不知道此时爱好曲线正在路上。所以我们必须存储该命令并有效地说“将其应用于您遇到的下一个爱好曲线”。毕竟,在创建爱好曲线之前,路径上可能有一些非爱好的东西。

这通常都很好,并且是指定兴趣曲线应该闭合的最简单方法。但有时我们需要更精细的控制,例如如果在同一路径中使用两个兴趣构造。在这种情况下,我们可以将选项添加到closed兴趣路径中的坐标。因此,以下任何一种都会绘制与上述相同的路径:

\draw ([closed]0,0) .. (1,1) .. (0,2) .. (-1,1) .. (0,0);
\draw (0,0) .. ([closed]1,1) .. (0,2) .. (-1,1) .. (0,0);
\draw (0,0) .. (1,1) .. (0,2) .. (-1,1) .. ([closed]0,0);

在第二和第三个步骤中,TikZ 知道它正在构建一个爱好路径,因此可以说“好的,我们将关闭路径。”。在第一个中,爱好构造尚未被触发(发生在第一个..),因此 closed仍然是“将其应用到下一个爱好路径中”。但这只是一个小技术细节。

一旦将选项应用到爱好路径,无论是从早期的风格设置还是从坐标收集的选项,它们已清除(以及全球)。如果清算出现问题,那么有一种风格clear next Hobby path options可以用来力量清除选项。

如此hobby行为才算应然。

让我们转到knots。当您定义一个链时(通过\strand),则路径会被使用很多。如果没有其他事情,它会被用来绘制线束本身,然后从它经过的线束中剪掉一些部分(因此对于每个交叉点,线束都会重新绘制)。当consider self intersections使用密钥时,情况会变得更糟,因为这需要将线束拆分成组件并分别考虑每个组件。因此线束被绘制很多时间

每次绘制时,都需要对其进行样式设置。因此,\strand存储其样式选项,并在每次绘制线时重新调用它们。通常,这正是需要的。但影响建造这里不需要路径,因为路径已经构建好了。通常,这无关紧要 - 此类选项通常会被丢弃。

但不是closed爱好路径的选项。因为这是一个延迟操作命令,所以它会挂起等待下一个爱好路径。它是爱好路径意味着for 已经构造并处理完毕。因此,它会非常耐心地等待下一个。由于下一个不会关闭,因此您会感到惊讶。

好的,那么来谈谈解决方案。最简单也是我认为最好的解决方案是将closed从移至\strand坐标之一。

        \strand[very thick,black] 
                ([closed]0.4,1) .. (0,2) .. (-2,3) 
                        .. (-4.8,0) .. (-2,-3) 
                        .. (0,-2) .. (0.4,-1) 
                        .. (0,0) .. (-0.4,1) 
                        .. (0,2) .. (2,3) 
                        .. (4.8,0) .. (2,-3) 
                        .. (0,-2) .. (-0.4,-1) .. (0,0);

另一种方法是将密钥放在clear next Hobby path options混乱的路径之前。我觉得这个方法不太优雅,但我还是提到了这种方法。

有人可能会说,当一条路径开始时,它应该清除选项,以便每条路径都从一张白纸开始。然而,要使它变得健壮,就需要比我迄今为止对 hobby 包所做的更深层次地连接到 TikZ 作用域机制。所以如果我发现我需要出于其他原因进行连接,我会记住这一点,但现在我会把它放在一边。

综上所述,我推荐以下解决方案:

\documentclass{standalone}
% \url{https://tex.stackexchange.com/q/505080/86}
\usepackage{tikz}
\usetikzlibrary{knots}
\usetikzlibrary{hobby}
\begin{document}
    \definecolor{skyblue}{RGB}{60,120,234}
    \scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
        \pgfdeclarelayer{foreground}
        \pgfsetlayers{main,foreground}
        \begin{pgfonlayer}{foreground}
            \begin{scope}
                \clip (-2,3) .. (0,2) .. (0.4,1)
                            .. (0,0) .. (-0.4,-1)
                            .. (0,-2) .. (2,-3)
                            .. (4.8,0) .. (2,3)
                            .. (0,2) .. (-0.4,1) -- (-2,3);
                \clip (-2,-3) .. (0,-2) .. (0.4,-1)
                            .. (0,0) .. (-0.4,1)
                            .. (0,2) .. (2,3)
                            .. (4.8,0) .. (2,-3)
                            .. (0,-2) .. (-0.4,-1) -- (-2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}
            \begin{scope}
                \clip (2,3) .. (0,2) .. (-0.4,1)
                            .. (0,0) .. (0.4,-1)
                            .. (0,-2) .. (-2,-3)
                            .. (-4.8,0) .. (-2,3)
                            .. (0,2) .. (0.4,1) -- (2,3);
                \clip (2,-3) .. (0,-2) .. (-0.4,-1)
                            .. (0,0) .. (0.4,1)
                            .. (0,2) .. (-2,3)
                            .. (-4.8,0) .. (-2,-3)
                            .. (0,-2) .. (0.4,-1) -- (2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}    
            \draw (-2.2,0) node[scale=3] {$+$};
            \draw (2.2,0) node[scale=3] {$-$};
            \draw (5,2.5) node[scale=3] {$M$};
        \end{pgfonlayer}
        \begin{knot}[
                        consider self intersections,
                        clip width=10,
                        clip radius=0.5cm,
                        ignore endpoint intersections=false,
                        flip crossing/.list={6,14}
                    ]
            \strand[very thick,black]
                    ([closed]0.4,1) .. (0,2) .. (-2,3) 
                            .. (-4.8,0) .. (-2,-3) 
                            .. (0,-2) .. (0.4,-1) 
                            .. (0,0) .. (-0.4,1) 
                            .. (0,2) .. (2,3) 
                            .. (4.8,0) .. (2,-3) 
                            .. (0,-2) .. (-0.4,-1) .. (0,0);
        \end{knot}
    \end{tikzpicture}}

    \scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
        \pgfdeclarelayer{foreground}
        \pgfsetlayers{main,foreground}
        \begin{pgfonlayer}{foreground}
            \begin{scope}
                \clip (-2,3) .. (0,2) .. (0.4,1)
                            .. (0,0) .. (-0.4,-1)
                            .. (0,-2) .. (2,-3)
                            .. (4.8,0) .. (2,3)
                            .. (0,2) .. (-0.4,1) -- (-2,3);
                \clip (-2,-3) .. (0,-2) .. (0.4,-1)
                            .. (0,0) .. (-0.4,1)
                            .. (0,2) .. (2,3)
                            .. (4.8,0) .. (2,-3)
                            .. (0,-2) .. (-0.4,-1) -- (-2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}
            \begin{scope}
                \clip (2,3) .. (0,2) .. (-0.4,1)
                            .. (0,0) .. (0.4,-1)
                            .. (0,-2) .. (-2,-3)
                            .. (-4.8,0) .. (-2,3)
                            .. (0,2) .. (0.4,1) -- (2,3);
                \clip (2,-3) .. (0,-2) .. (-0.4,-1)
                            .. (0,0) .. (0.4,1)
                            .. (0,2) .. (-2,3)
                            .. (-4.8,0) .. (-2,-3)
                            .. (0,-2) .. (0.4,-1) -- (2,-3);  
                \fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
            \end{scope}    
            \draw (-2.2,0) node[scale=3] {$+$};
            \draw (2.2,0) node[scale=3] {$-$};
            \draw (5,2.5) node[scale=3] {$M$};
        \end{pgfonlayer}
        \begin{knot}[
                        consider self intersections,
                        clip width=10,
                        clip radius=0.5cm,
                        ignore endpoint intersections=false,
                        flip crossing/.list={6,14}
                    ]
            \strand[very thick,black] 
                    ([closed]0.4,1) .. (0,2) .. (-2,3) 
                            .. (-4.8,0) .. (-2,-3) 
                            .. (0,-2) .. (0.4,-1) 
                            .. (0,0) .. (-0.4,1) 
                            .. (0,2) .. (2,3) 
                            .. (4.8,0) .. (2,-3) 
                            .. (0,-2) .. (-0.4,-1) .. (0,0);
        \end{knot}
    \end{tikzpicture}}
\end{document}

正确使用爱好和结

相关内容