避免線相交

避免線相交

我想做這樣的事情:類別圖上的不相交線

接著就,隨即: http://www.texample.net/tikz/examples/氧化和還原/

它會是這樣的:

https://i.stack.imgur.com/4fLoS.png

答案1

intersections這是使用該庫的一種可能性TikZ:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node[inner xsep=0pt,inner ysep=10pt,outer sep=0pt] (#1) {};}

\begin{document}

\[
\tikzmark{c}2:\tikzmark{a}3 + 5:\tikzmark{d}3+\tikzmark{b}2:3+5:3
\]

\begin{tikzpicture}[remember picture,overlay]
\draw[name path=line1,red,->] ([xshift=2pt]a.north east) -- +(0pt,10pt) -| ([xshift=2pt]b.north east);
\path[name path=line2] ([xshift=2pt]c.north east) -- +(0pt,23pt) -| ([xshift=2pt]d.north east);
\draw[name intersections={of=line1 and line2,by={int}},draw,->,red] 
  ([xshift=2pt]c.north east) -- +(0pt,23pt) -| ([yshift=3pt]int) arc(90:-90:2.5pt) -- ([xshift=2pt]d.north east);
\end{tikzpicture}

\end{document}

在此輸入影像描述

這個想法是利用無所不在的方法\tikzmark為路徑放置標記,然後找到兩條路徑的交點,然後在交點處畫一個圓弧。

第一次運行上述程式碼將導致錯誤:

! Package pgf Error: No shape named intersection-1 is known.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.19 ...{of=line1 and line2,by={int}},draw,->,red]

由於標記的座標尚不清楚,因此路徑交集的計算失敗。退出編譯並重新編譯,應該會得到想要的結果。

在聊天中,大衛卡萊爾提出如下修改:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node[inner xsep=0pt,inner ysep=10pt,outer sep=0pt] (#1) {};%
}

\begin{document}

\[
\tikzmark{c}2:\tikzmark{a}3 + 5:\tikzmark{d}3+\tikzmark{b}2:3+5:3
\]

\expandafter \ifx\csname pgf@sys@pdf@mark@pos@pgfid\the \csname
pgf@picture@serial@count\endcsname\endcsname\relax
\typeout{no}%
\else
\typeout{yes}%
\begin{tikzpicture}[remember picture,overlay]
\draw[name path=line1,red,->] ([xshift=2pt]a.north east) -- +(0pt,10pt) -| ([xshift=2pt]b.north east);
\path[name path=line2] ([xshift=2pt]c.north east) -- +(0pt,23pt) -| ([xshift=2pt]d.north east);
\draw[name intersections={of=line1 and line2,by={int}},draw,->,red] 
  ([xshift=2pt]c.north east) -- +(0pt,23pt) -| ([yshift=3pt]int) arc(90:-90:2.5pt) -- ([xshift=2pt]d.north east);
\end{tikzpicture}
\fi

\end{document}

這不會產生錯誤。該文件必須執行 3 次。

現在打擊樂提供了這種替代方案:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node[inner xsep=0pt,inner ysep=10pt,outer sep=0pt] (#1) {};}

\begin{document}

\[
\tikzmark{c}2:\tikzmark{a}3 + 5:\tikzmark{d}3+\tikzmark{b}2:3+5:3
\]

\begin{tikzpicture}[remember picture,overlay]
\draw[name path=line1,red,->] ([xshift=2pt]a.north east) -- +(0pt,10pt) -| ([xshift=2pt]b.north east);
\path[name path=line2] ([xshift=2pt]c.north east) -- +(0pt,23pt) -| ([xshift=2pt]d.north east);
\path[name intersections={of=line1 and line2,total=\totinter}] \pgfextra{\xdef\totinter{\totinter}};
\ifnum\totinter=0\relax%
\else
\path[name intersections={of=line1 and line2,by={int}}];
  \draw[,draw,->,red] ([xshift=2pt]c.north east) -- +(0pt,23pt) -| ([yshift=3pt]int) arc(90:-90:2.5pt) -- ([xshift=2pt]d.north east);
\fi
\end{tikzpicture}


\end{document}

相關內容