Tikz でテキストの間に線を挿入する方法

Tikz でテキストの間に線を挿入する方法

私はTikz初心者で、今日から使い始めたばかりですが、解決方法が見つからない問題に遭遇しました。私は、以下の例に基づいてブロック図を作成しています。例: 制御システムの原理。必要に応じて を変更して、たとえばブロックの上にテキストを追加しようとしました\node [above of=controller, node distance=1.2cm] (MP:1) {$MP_1$};。これが正しい方法かどうかはわかりません。理想的なシナリオは、このテキストを左右の矢印でピンスタイルと に接続することですtin。およびに名前を付けることができないtout/tinため、コマンドを使用できず、機能しません。 を挿入して試しましたが、出力は再び正しくありません。Tikz のマニュアルを見ていて、 などの座標を使用することで、何とか問題の解決策を見つけました。結果は再び正しくありません。tintout/tin\draw [draw,->] (tin) -- node [name=line] {} (MP:1);\draw [->, right of=MP:1, node distance=1.5cm] {};\draw [<-] (20mm,10mm) -- (0mm,10mm);

最初の質問は、挿入しているテキストに参照用の名前を付ける方法は正しいか、それとももっと良い方法があるかということです。2 番目の質問は、テキストを正しく挿入している場合、ピンスタイルの参照名を作成できるかどうか、そうでない場合は線をどのように描画できるかということです。テスト目的で、以下にコードのサンプルと出力の画像を示します。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\tikzstyle{block} = [draw, rectangle, minimum height=2em, minimum width=5em] %fill=blue!20
\tikzstyle{sum} = [draw, circle, node distance=2.0cm, minimum size=6mm]
\tikzstyle{input} = [coordinate] 
\tikzstyle{output} = [coordinate]
\tikzstyle{pinstyle} = [pin edge={to-,thin,black}]

\begin{figure}[!ht]
\begin{center}
\makebox[\textwidth][c]{
\begin{tikzpicture}[auto, node distance=1.5cm,>=latex']
\node [input, name=input] {};
\node [sum, right of=input, pin={[pinstyle]above:$t_{in}$}, node distance=1.5cm] (sum) {MP};
\draw [draw,->] (input) -- node [name=begging] {UE} (sum);
\node [block, right of=sum, node distance=2.0cm] (controller) {Node B};
% Text above of GGSN (MP1)
\node [above of=controller, node distance=1.2cm] (MP:1) {$MP_1$};
\draw [<-] (20mm,10mm) -- (0mm,10mm);
%\draw [->, right of=MP:1, node distance=1.5cm] {};
\draw [->] (sum) -- node {} (controller);
\node [sum, right of=controller, pin={[pinstyle]above:$t_{out}/t_{in}$}, node distance=2.0cm] (sum_2) {MP};
\draw [->] (controller) -- node [name=u] {} (sum_2);
\node [block, right of=sum_2, node distance=2.0cm] (controller_2) {RNC};
% Text above of GGSN (MP2)
\node [above of=controller_2, node distance=1.2cm] (MP:2) {$MP_2$};
\draw [->] (sum_2) -- node [name=u2] {} (controller_2);
\node [sum, right of=controller_2, pin={[pinstyle]above:$t_{out}/t_{in}$}, node distance=2.0cm] (sum_3) {MP};
\draw [->] (controller_2) -- node [name=u3] {} (sum_3);
\node [block, right of=sum_3, node distance=2.0cm] (controller_3) {SGSN};
% Text above of GGSN (MP3)
\node [above of=controller_3, node distance=1.2cm] (MP:3) {$MP_3$};
\draw [->] (sum_3) -- node [name=u4] {} (controller_3);
\node [sum, right of=controller_3, pin={[pinstyle]above:$t_{out}/t_{in}$}, node distance=2.0cm] (sum_4) {MP};
\draw [->] (controller_3) -- node [name=u5] {} (sum_4);
\node [block, right of=sum_4, node distance=2.0cm] (controller_4) {GGSN};
% Text above of GGSN (MP4)
\node [above of=controller_4, node distance=1.2cm] (MP:4) {$MP_4$};
\draw [->] (sum_4) -- node [name=u6] {} (controller_4);
\node [sum, right of=controller_4, pin={[pinstyle]above:$t_{out}$}, node distance=2.0cm] (sum_5) {MP};
\draw [->] (controller_4) -- node [name=u8] {} (sum_5);
\node [output, right of=sum_5] (output) {};
\draw [->] (sum_5) -- node [name=end] {PDN}(output);
\node [block, above of=sum_3, node distance=2.5cm] (pipeline) {Pipeline};
\draw [->] (begging) |- (pipeline);
\draw [->] (pipeline) -| (end);
\end{tikzpicture} } % End of makebox
\caption{Test}
\label{fig:blockdiagram}
\end{center}
\end{figure}

\end{document}

ブロックダイアグラム出力

答え1

改良版:

  1. 古い構文\tikzsetstyleをより適切な\tikzset構文に変更しました。

  2. 演算子はノード名に適用されると特別な意味を持つ:ため、ノード名には使用しないことをお勧めします。:

  3. ライブラリをロードしpositioning、非推奨のof=構文を=of構文に変更します (結果のコードの経済性に注目してください)。

  4. ピンの代わりにmytextノードにスタイルを使用しました。これにより、より良い結果が得られます。

  5. 改善の余地: ここでチェーンを使用すると、コードが簡素化される可能性があります。これは演習として残しておきます。

コード:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\begin{document}

\tikzset{
block/.style={
  draw, 
  rectangle, 
  minimum height=2em, 
  minimum width=5em
  },
sum/.style={
  draw, 
  circle, 
  minimum size=6mm
  },
input/.style={coordinate}, 
output/.style={coordinate},
mytext/.style={
  draw,
  text depth=4pt,
  text height=10pt
  }
}
\begin{figure}[!ht]
\begin{center}
\makebox[\textwidth][c]{%
\begin{tikzpicture}[auto,node distance=1cm and 0.8cm,>=latex']
\node[
  input,
  name=input] {};
\node[
  sum, 
  right = of input
  ] 
  (sum) {MP};
\node[
  mytext,
  above=of sum.center,
  name=tin1
  ] 
  {$t_{in}$};  
\draw[->] 
  (input) -- node [name=begging] {UE} (sum);
\node[
  block, 
  right = of sum
  ] 
  (controller) {Node B};
% Text above of GGSN (MP1)
\node[
  mytext,
  above = of controller.center
  ] 
  (MP1) {$MP_1$};
%\draw [->, right = of MP:1, node distance=1.5cm] {};
\draw[->] 
  (sum) -- node {} (controller);
\node[
  sum,
  right = of controller
  ] 
  (sum_2) {MP};
\node[
  mytext,
  above=of sum_2.center,
  name=toti1
  ] 
  {$t_{out}/t_{in}$};  
\draw[->] 
  (controller) -- node [name=u] {} (sum_2);
\node[
  block, 
  right = of sum_2
  ] 
  (controller_2) {RNC};
% Text above of GGSN (MP2)
\node[
  mytext,
  above = of controller_2.center
  ] 
  (MP2) {$MP_2$};
\draw[->] 
  (sum_2) -- node [name=u2] {} (controller_2);
\node[
  sum, 
  right = of controller_2, 
  ] 
  (sum_3) {MP};
\node[
  mytext,
  above=of sum_3.center,
  name=toti2
  ] 
  {$t_{out}/t_{in}$};
\draw[->] 
  (controller_2) -- node [name=u3] {} (sum_3);
\node[
  block,
  right = of sum_3
  ] 
  (controller_3) {SGSN};
% Text above of GGSN (MP3)
\node[
  mytext,
  above = of controller_3.center
  ] 
  (MP3) {$MP_3$};
\draw[->] 
  (sum_3) -- node [name=u4] {} (controller_3);
\node[
  sum, 
  right = of controller_3, 
  ] 
  (sum_4) {MP};
\node[
  mytext,
  above=of sum_4.center,
  name=  toti3
  ] {$t_{out}/t_{in}$};
\draw[->] 
  (controller_3) -- node [name=u5] {} (sum_4);
\node[
  block, 
  right = of sum_4
  ] 
  (controller_4) {GGSN};
% Text above of GGSN (MP4)
\node[
  mytext,
  above = of controller_4.center
  ] 
  (MP4) {$MP_4$};
\draw[->] 
  (sum_4) -- node [name=u6] {} (controller_4);
\node[
  sum, 
  right = of controller_4
  ] 
  (sum_5) {MP};
\node[
  mytext,
  above=of sum_5.center,
  name=tout1
  ]
  {$t_{out}$}; 
\draw[->] 
  (controller_4) -- node [name=u8] {} (sum_5);
\node[
  output, 
  right = of sum_5
  ] 
  (output) {};
\draw[->] 
  (sum_5) -- node [name=end] {PDN}(output);
\node[
  block,
  above = 2cm of sum_3
  ] 
  (pipeline) {Pipeline};
\draw[->] 
  (begging) |- (pipeline);
\draw[->] 
  (pipeline) -| (end);
  
\draw[->]
  (tin1) -- (sum);
\draw[->]
  (toti1) -- (sum_2);
\draw[->]
  (toti2) -- (sum_3);
\draw[->]
  (toti3) -- (sum_4);
\draw[->]
  (tout1) -- (sum_5);

\draw[<->] 
  (tin1) -- (MP1);
\draw[<->] 
  (MP1.east) -- (toti1.west|-MP1.east);
\draw[<->] 
  (toti1.east) -- (MP2.west|-toti1.east);
\draw[<->] 
  (MP2.east) -- (toti2.west|-MP2.east);
\draw[<->] 
  (toti2.east) -- (MP3.west|-toti2.east);
\draw[<->] 
  (MP3.east) -- (toti3.west|-MP3.east);
\draw[<->] 
  (toti3.east) -- (MP4.west|-toti3.east);
\draw[<->] 
  (MP4.east) -- (tout1.west|-MP4.east);
\end{tikzpicture}} % End of makebox
\caption{Test}
\label{fig:blockdiagram}
\end{center}
\end{figure}

\end{document}

ここに画像の説明を入力してください

drawスタイルからオプションを削除しますmytext

初期バージョン:

A はpin単なる特別なノードなので、 を使用してname=<string>接続に使用する名前を付けることができます。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\begin{document}

\tikzset{
block/.style={
  draw, 
  rectangle, 
  minimum height=2em, 
  minimum width=5em
  },
sum/.style={
  draw, 
  circle, 
  node distance=2.0cm, 
  minimum size=6mm
  },
input/.style={coordinate}, 
output/.style={coordinate},
pinstyle/.style={
  pin edge={to-,thin,black}
  }
}
\begin{figure}[!ht]
\begin{center}
\makebox[\textwidth][c]{
\begin{tikzpicture}[auto, node distance=1.5cm,>=latex']
\node [input, name=input] {};
\node [sum, right of=input, pin={[pinstyle,name={tin1}]above:$t_{in}$}, node distance=1.5cm] (sum) {MP};
\draw [draw,->] (input) -- node [name=begging] {UE} (sum);
\node [block, right of=sum, node distance=2.0cm] (controller) {Node B};
% Text above of GGSN (MP1)
\node [above of=controller, node distance=1.2cm] (MP1) {$MP_1$};
\draw [<-] (20mm,10mm) -- (0mm,10mm);
%\draw [->, right of=MP:1, node distance=1.5cm] {};
\draw [->] (sum) -- node {} (controller);
\node [sum, right of=controller, pin={[pinstyle,name=toti1]above:$t_{out}/t_{in}$}, node distance=2.0cm] (sum_2) {MP};
\draw [->] (controller) -- node [name=u] {} (sum_2);
\node [block, right of=sum_2, node distance=2.0cm] (controller_2) {RNC};
% Text above of GGSN (MP2)
\node [above of=controller_2, node distance=1.2cm] (MP2) {$MP_2$};
\draw [->] (sum_2) -- node [name=u2] {} (controller_2);
\node [sum, right of=controller_2, pin={[pinstyle,name=toti2]above:$t_{out}/t_{in}$}, node distance=2.0cm] (sum_3) {MP};
\draw [->] (controller_2) -- node [name=u3] {} (sum_3);
\node [block, right of=sum_3, node distance=2.0cm] (controller_3) {SGSN};
% Text above of GGSN (MP3)
\node [above of=controller_3, node distance=1.2cm] (MP3) {$MP_3$};
\draw [->] (sum_3) -- node [name=u4] {} (controller_3);
\node [sum, right of=controller_3, pin={[pinstyle,name=toti3]above:$t_{out}/t_{in}$}, node distance=2.0cm] (sum_4) {MP};
\draw [->] (controller_3) -- node [name=u5] {} (sum_4);
\node [block, right of=sum_4, node distance=2.0cm] (controller_4) {GGSN};
% Text above of GGSN (MP4)
\node [above of=controller_4, node distance=1.2cm] (MP4) {$MP_4$};
\draw [->] (sum_4) -- node [name=u6] {} (controller_4);
\node [sum, right of=controller_4, pin={[pinstyle,name=tout1]above:$t_{out}$}, node distance=2.0cm] (sum_5) {MP};
\draw [->] (controller_4) -- node [name=u8] {} (sum_5);
\node [output, right of=sum_5] (output) {};
\draw [->] (sum_5) -- node [name=end] {PDN}(output);
\node [block, above of=sum_3, node distance=2.5cm] (pipeline) {Pipeline};
\draw [->] (begging) |- (pipeline);
\draw [->] (pipeline) -| (end);

\draw[<->] (tin1.east) -- (MP1.west|-tin1.east);
\draw[<->] (MP1.east) -- (toti1.west|-MP1.east);
\draw[<->] (toti1.east) -- (MP2.west|-toti1.east);
\draw[<->] (MP2.east) -- (toti2.west|-MP2.east);
\draw[<->] (toti2.east) -- (MP3.west|-toti2.east);
\draw[<->] (MP3.east) -- (toti3.west|-MP3.east);
\draw[<->] (toti3.east) -- (MP4.west|-toti3.east);
\draw[<->] (MP4.east) -- (tout1.west|-MP4.east);
\end{tikzpicture} } % End of makebox
\caption{Test}
\label{fig:blockdiagram}
\end{center}
\end{figure}

\end{document}

ここに画像の説明を入力してください

古い構文\tikzsetstyleをより適切な構文に変更しました。また、演算子はノード名に適用されると特別な意味を持つため、ノード名では\tikzset使用しないことをお勧めします。::

of=また、ポジショニング ライブラリをロードし、非推奨の構文を構文に変更することをお勧めします=of

答え2

ゴンサロはすでに非常に良い答えを出していますが、通常スキームmatrixノードは良いオプションです。すべてのノードを手動で配置する必要はなく、matrix of nodesオプション (matrixライブラリをロードする必要があります) を使用すると、入力作業を大幅に節約できます。

\documentclass{article}
\usepackage{tikz}
\usepackage{adjustbox}
\usetikzlibrary{shapes,matrix,arrows}

\begin{document}

\tikzset{
block/.style={
  draw, 
  rectangle, 
  minimum height=2em, 
  minimum width=5em
  },
sum/.style={
  draw, 
  circle, 
  minimum size=6mm
  },
input/.style={coordinate}, 
output/.style={coordinate},
mytext/.style={
  text depth=4pt,
  text height=10pt
  }
}

\begin{figure}[!ht]
\centering
\adjustbox{width=\linewidth}{
\begin{tikzpicture}[>=latex']

\matrix (D) [matrix of nodes, 
                column sep=1cm, 
                row sep=0.8cm,
                row 1/.style={nodes={block,anchor=center}},
                row 3/.style={nodes={block,anchor=center}},
                row 2/.append style={nodes={mytext,anchor=center}},
                ]
{
& & & & & Pipeline \\
& $t_in$ & $MP_1$ & $t_{out}/t_{in}$ & $MP_2$ & $t_{out}/t_{in}$ & $MP_3$ & $t_{out}/t_{in}$ & $MP_4$ & $t_{out}$ & \\
|[input]| &|[sum]| MP & Node B &|[sum]| MP & RNC &|[sum]| MP & SGSN &|[sum]| MP & GGSN &|[sum]| MP & |[input]|\\
};

\foreach \i [remember=\i as \lasti (initially 1)] in {2,...,11}
\draw[->] (D-3-\lasti)--(D-3-\i);

\foreach \i [remember=\i as \lasti (initially 2)] in {3,...,10}
\draw[<->] (D-2-\lasti)--(D-2-\i);

\foreach \i in {2,4,6,8,10}
\draw[->] (D-2-\i)--(D-3-\i);

\path (D-3-1)--(D-3-2) node [above,midway] (UE) {UE};
\path (D-3-10)--(D-3-11) node [above,midway] (PDN) {PDN};
\draw[->] (UE) |- (D-1-6);
\draw[<-] (PDN) |- (D-1-6);

\end{tikzpicture}}
\caption{Test}
\label{fig:blockdiagram}
\end{figure}

\end{document}

ここに画像の説明を入力してください

関連情報