使用 TikZ 配置节点位置和宽度、重叠节点

使用 TikZ 配置节点位置和宽度、重叠节点

我创建了一个简单的反馈结构,但无法使最右边的块不与圆圈重叠。我尝试使用和来改变这个块的宽度minimum widthtext width但没有效果。我需要改变什么才能将块移到右边?

\begin{tikzpicture}[auto, node distance=2cm,>=latex']
    \tikzstyle{block} = [draw, rectangle, minimum height=2em, minimum width=3em, text centered, text depth=0pt]
    \tikzstyle{sum} = [draw, circle, node distance=1.5cm]

    \node (input) {};
    \node [above, above of=input] (gyro_input) {};
    \node [block, right of=input] (error) { $\hat{R}^T \bar{R}$ };
    \node [block, right of=error] (normalize) {$P_a(\tilde{R})$};
    \node [block, right of=normalize] (gain) {$C(s)$};
    \node [sum, right of=gain] (sum2) {};
    \node [block, right of=sum2] (correction) {$\dot{\hat{R}} = \hat{R} ( \bar{\Omega}_\times + C(s) P_a (\tilde{R}))$};
    \node [right of=correction] (output) {};
    \node [block, above of=sum2] (R_dynamics) {$\bar{\Omega}_\times$};
    \node [block, below of=error] (transpose) {$\hat{R}^T$};

    \draw [->] (input) node [above] {$\bar{R}$} -- (error);
    \draw [->] (error) -- (normalize) node [midway, above] {$\tilde{R}$};
    \draw [->] (normalize) -- (gain);
    \draw [->] (gain) -- (sum2);
    \draw [->] (sum2) -- (correction);
    \draw [->] (correction) -- node (fork) {} (output) node [above] {$\hat{R}$};
    \draw [->] (gyro_input) node [above] {$\bar{\Omega}$} -- (R_dynamics);
    \draw [->] (R_dynamics) -- (sum2);
    \draw [->] (fork) |- (transpose);
    \draw [->] (transpose) -- (error);
\end{tikzpicture}

上述结果是: 在此处输入图片描述

答案1

出现问题的原因是node distance设置为1.5cm。这个距离指的是节点中心之间的距离,不是节点边之间的距离。在这种情况下,重叠节点的宽度远大于此值,因此它将重叠。

您可以通过本地覆盖来规避它node distance

\node [block, right of=sum2,node distance=3.5cm]

这对你有好处。

另一个好主意是明确使用节点锚点来定位它们。这将产生一致的距离之间节点的一侧。我不记得这是否以某种方式实现,但可以像这样手动完成:

\tikzstyle{my below of} = [below=of #1.south]
\tikzstyle{my right of} = [right=of #1.east]
\tikzstyle{my left of} = [left=of #1.west]
\tikzstyle{my above of} = [above=of #1.north]
\begin{tikzpicture}[auto, node distance=.75cm,>=latex']
    \tikzstyle{block} = [draw, rectangle, minimum height=2em, minimum width=3em, text centered, text depth=0pt]
    \tikzstyle{sum} = [draw, circle]

    \node (input) {};
    \node [above, my above of=input] (gyro_input) {};
    \node [block, my right of=input] (error) { $\hat{R}^T \bar{R}$ };
    \node [block, my right of=error] (normalize) {$P_a(\tilde{R})$};
    \node [block, my right of=normalize] (gain) {$C(s)$};
    \node [sum, my right of=gain] (sum2) {};
    \node [block, my right of=sum2] (correction) {$\dot{\hat{R}} = \hat{R} ( \bar{\Omega}_\times + C(s) P_a (\tilde{R}))$};
    \node [my right of=correction] (output) {};
    \node [block,my above of=sum2] (R_dynamics) {$\bar{\Omega}_\times$};
    \node [block,my below of=error] (transpose) {$\hat{R}^T$};

    \draw [->] (input) node [above] {$\bar{R}$} -- (error);
    \draw [->] (error) -- (normalize) node [midway, above] {$\tilde{R}$};
    \draw [->] (normalize) -- (gain);
    \draw [->] (gain) -- (sum2);
    \draw [->] (sum2) -- (correction);
    \draw [->] (correction) -- node (fork) {} (output) node [above] {$\hat{R}$};
    \draw [->] (gyro_input) node [above] {$\bar{\Omega}$} -- (R_dynamics);
    \draw [->] (R_dynamics) -- (sum2);
    \draw [->] (fork) |- (transpose);
    \draw [->] (transpose) -- (error);
\end{tikzpicture}               

请注意,node distance现在告诉您节点边之间的距离,而不是节点中心之间的距离。您确实需要确保gyro_input节点定位的间距正确。但是,这很容易做到。

另外,考虑使用\coordinate没有文本的节点。这将表现得更符合预期,因为您的坐标input实际上将具有有限的大小。因此锚点north/south/east/west将不是一个点。尝试插入:

\draw[blue] (gyro_input.north) -- (gyro_input.south);

并查看该节点的大小。这是由于inner sepouter sep

答案2

对于这种规则的结构,我更喜欢使用matrix来放置节点。值column seprow sep表示节点边界之间的距离,但您可以为每列和每行固定不同的值。在下面的示例中,&[-8mm]将第一列和第二列中所有节点之间的默认列间距减去 8 毫米。

关于您的代码的第二条评论,您可以使用coordinate节点而不是空节点。

\documentclass[border=3mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix,arrows}

\begin{document}

\begin{tikzpicture}[auto, node distance=2cm,>=latex']
    \tikzstyle{block} = [draw, rectangle, minimum height=2em, minimum width=3em, text centered, text depth=0pt]
    \tikzstyle{sum} = [draw, circle, node distance=1.5cm]

\matrix[column sep=1.2cm, row sep=8mm]{
    \coordinate[label=above:$\bar{\Omega}$] (gyro input); &[-8mm] & & &[-7mm] \node [block] (R_dynamics) {$\bar{\Omega}_\times$}; &[-7mm] \\
\coordinate[label=above:$\bar{R}$] (input);
&
\node [block] (error) { $\hat{R}^T \bar{R}$ };
&    
\node [block] (normalize) {$P_a(\tilde{R})$};
&    
\node [block] (gain) {$C(s)$};
&
\node [sum] (sum2) {};
&
\node [block] (correction) {$\dot{\hat{R}} = \hat{R} ( \bar{\Omega}_\times + C(s) P_a (\tilde{R}))$};
&
\coordinate (output) {}; \\
& 
\node [block] (transpose) {$\hat{R}^T$};\\
};

\draw [->] (gyro input) -- (R_dynamics);
\draw [->] (R_dynamics) -- (sum2);

\draw [->] (input)--(error);
\draw[->] (error) -- (normalize) node [midway, above] {$\tilde{R}$};
    \draw [->] (normalize) -- (gain);
    \draw [->] (gain) -- (sum2);
    \draw [->] (sum2) -- (correction);
    \draw [->] (transpose) -- (error);
    \draw [->] (transpose) -| (output)--(correction) node [above,midway] {$\hat{R}$};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

解决方案schemabloc包(另请参阅使用 LaTeX 中的 PGF/TIKZ 构建图表块[法语])。

\documentclass{article}
\usepackage{tikz}

\usepackage{schemabloc}

\begin{document}

\begin{tikzpicture}
\sbEntree{input}
\sbBloc[4]{error}{$\hat{R}^T \bar{R}$ }{input} \sbRelier[$\bar{R}$]{input}{error}
\sbBloc[3]{normalize}{$P_a(\tilde{R})$ }{error} \sbRelier[$\tilde{R}$]{error}{normalize}
\sbBlocL{gain}{$C(s)$}{normalize}
\sbCompSum*[5]{sum2}{gain}{ }{ }{ }{ } \sbRelier{gain}{sum2}
\sbBlocL{correction}{$\dot{\hat{R}} = \hat{R} ( \bar{\Omega}_\times + C(s) P_a (\tilde{R}))$}{sum2}
\sbSortie[3]{output}{correction}                
\sbRelier[$\hat{R}$]{correction}{output}
\sbDecaleNoeudy[-6]{input}{gyro input}
\node[above of=gyro input]{$\bar{\Omega}$};
\sbBlocL[20.5]{R_dynamics}{$\bar{\Omega}_\times$}{gyro input} 
\sbDecaleNoeudy[6]{input}{transpose}
\sbBloc[4]{transpose}{$\hat{R}^T$}{transpose}
\sbRelieryx{correction-output}{transpose}
\sbRelier{transpose}{error}
\sbRelier{R_dynamics}{sum2}
\end{tikzpicture}

\end{document}

整体结构图

相关内容