在 \draw[->] 上更好地使用 tikz 的圆角

在 \draw[->] 上更好地使用 tikz 的圆角

我有这个代码:

% !TeX encoding = UTF-8


\documentclass[utf8]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}

\newcommand{\multilinks}[3]{\foreach \noeud in {#1} {\draw[<-, rounded corners] (#2.west) -| ++(-#3em,0em) |- (\noeud.east);}}

\begin{document}
    \begin{figure}[htp]
        \centering
        \tikzset{
            basic/.style={draw, rounded corners=2pt, thick, text width=8em, align=flush center, node distance=2em},
        }
        \begin{tikzpicture}[]
            \matrix[row sep=2em, column sep=4em, every node/.style={basic}] {
                \node(n1){Text}; & \node(n3){another text}; \\
                \node(n2){one thing}; & \node(n4){again text}; \\
            };
            \multilinks{n1,n2}{n3}{3}
            \multilinks{n1}{n4}{1}
        \end{tikzpicture}
    \end{figure}

\end{document}

它给了我这个结果:

如您所见,曲线很奇怪。我该如何修复它?

这是我想要的结果:

答案1

第一个的定义\multilinks和列表略有变化:#1\multilinks

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,positioning}

\newcommand{\multilinks}[3]{
    \foreach \noeud in {#1} {
        \draw[<-, rounded corners] (#2.west) -- ++(-#3 em,0em) |- (\noeud.east);
    }
}

\begin{document}
\tikzset{
    basic/.style={
        draw, 
        rounded corners=2pt, 
        thick, 
        text width=8em, 
        align=flush center, 
        node distance=2em
    }
}
\begin{tikzpicture}[]
\fontsize{8}{9} \selectfont
\matrix[row sep=2em, column sep=4em, every node/.style={basic}] {
    \node(n1){Text}; & \node(n3){another text}; \\
    \node(n2){one thing}; & \node(n4){again text}; \\
};
\multilinks{n2}{n3}{3}
\multilinks{n1}{n4}{1}
\end{tikzpicture}

\end{document}

在此处输入图片描述


编辑

如果要保留原来的方式,则的定义\multilinks会稍微复杂一些:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,positioning}

\newcommand{\multilinks}[3]{
    \begin{scope}[x=1em,y=1em]
    \newdimen\xend
    \newdimen\yend  
    \path (#2.west);
    \pgfgetlastxy{\xend}{\yend}
    \foreach \i in {#1} {
        \newdimen\xstart
        \newdimen\ystart 
        \path (\i.east);
        \pgfgetlastxy{\xstart}{\ystart}
        \coordinate (1) at ({\xend-#3 em},\ystart);
        \coordinate (2) at ({\xend-#3 em},\yend);
        \ifdim\ystart=\yend
            \draw[->] (\i.east)--(#2.west);
        \else
            \draw[->,rounded corners] (\i.east)--(1)--(2)--(#2.west);
        \fi
    }
    \end{scope}
}

\begin{document}
\tikzset{
    basic/.style={
        draw, 
        rounded corners=2pt, 
        thick, 
        text width=8em, 
        align=flush center, 
        node distance=2em
    }
}
\begin{tikzpicture}[]
\fontsize{8}{9} \selectfont
\matrix[row sep=2em, column sep=4em, every node/.style={basic}] {
    \node(n1){Text}; & \node(n3){another text}; \\
    \node(n2){one thing}; & \node(n4){again text}; \\
};
\multilinks{n1,n2}{n3}{3}
\multilinks{n1}{n4}{1}
\end{tikzpicture}

\end{document}

在此处输入图片描述

我们必须添加一个\ifdim因为 Tirounded corners当 Z 被命令从点绘制一条路径时,它很困惑A指向A(即两个相同的点):

\documentclass[tikz,margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[y=0.2cm]
\draw[rounded corners] (0,0) --(1,0) --(1,0)--(2,0) ; % Some bugs?
\draw[rounded corners] (0,-1)--(1,-1)--       (2,-1);
\draw[rounded corners] (0,-2)--               (2,-2);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容