Tikz 和节点间距太大

Tikz 和节点间距太大

我遇到了麻烦,因为第一行与图表的其余部分在水平和垂直方向上都不能很好地对齐。

在此处输入图片描述

我尝试过左右移动,但似乎没什么帮助。如果我只添加箭头 ( \node (b) [right=0.1pt of a] {};),行高似乎没问题:

在此处输入图片描述

但当我添加标签(\node (b) [right=0.1pt of a,label=right:Request] {};)时,一切都变得一团糟。

活动视频:

\documentclass[class=minimal,border=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows,fit}

\begin{document}
\begin{tikzpicture}[
  process_style/.append style={
    rectangle,
    fill=blue!15,
    draw=black,
    thick
  },
  host_style/.append style={
    rectangle,
    draw=black,
    dashed
  }
]

% legend
\matrix [draw,below left] at (20.35, -1.5) {
  \node (a) {};
  \node (c) [left=0.1pt of a] {};
  \node (b) [right=0.1pt of a,label=right:Request] {};
  \draw[stealth-, thick] (b) -- (c);
  \\
  \node [process_style, label=right:Process] {}; \\
  \node [host_style, label=right:Machine] {}; \\
};

\end{tikzpicture}
\end{document}

答案1

一个可能的解决方案是围绕label符号进行游戏。

\documentclass[class=minimal,border=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows,fit}

\begin{document}
\begin{tikzpicture}[
  process_style/.append style={
    rectangle,
    fill=blue!15,
    draw=black,
    thick
  },
  host_style/.append style={
    rectangle,
    draw=black,
    dashed
  }
]

% legend
\matrix [draw,below left] at (20.35, -1.5) {
  \node (a) {};
  \node (c) [left=0.1pt of a] {};
  \node (b) [right=0.1pt of a] {Request};
  \draw[stealth-, thick] (b) -- (c);
  \\[-2.5pt]
  \node [process_style, label=right:Process] {}; \\
  \node [host_style, label=right:Machine] {}; \\
};

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我对这部分代码的写法有所不同:

  \node (a) {};
  \node (c)[left=0.1pt of a] {};
  \node (b)[right=0.1pt of a,label=right:Request] {};
  \draw[stealth-, thick] (b) -- (c);

事实上,一个不包含任何内容的节点有一个最小宽度作为0.3333em您的节点样式 (process_style 和 host_style)如果不指定宽度,其宽度默认为0.3333em。箭头的起始点不是从节点 (a),而是从 开始,到 结束0.1 pt更加容易。(a.west)(a.east)

这就是给出的结果:

  \node[] (a) {};
  \draw[-stealth, thick] (a.west) -- (a.east) node[right=0.3333em of a,inner sep=0pt]{Request};

使用 www.DeepL.com/Translator 翻译

截屏

\documentclass[class=minimal,border=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows,fit}

\begin{document}
\begin{tikzpicture}[
  process_style/.append style={
    rectangle,
    fill=blue!15,
    draw=black,
    thick
  },
  host_style/.append style={
    rectangle,
    draw=black,
    dashed
  }
]

% legend
\matrix [draw,below left] at (20.35, -1.5) {
  \node[] (a) {};
  \draw[-stealth, thick] (a.west) -- (a.east) node[right=0.3333em of a,inner sep=0pt]{Request};
  \\
  \node [process_style, label=right:Process] {};\\
  \node [host_style, label=right:Machine] {};\\
};

\end{tikzpicture}
\end{document}

相关内容