Tikz 的垂直文本位置错误

Tikz 的垂直文本位置错误

这让我很抓狂。“Job5”和“Job6”文本在垂直方向上比其他文本略高。

我怀疑“内部分离”,但我需要它来适应。

我该如何重新调整文本?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}[auto,
    singlejob/.style = {rectangle, minimum height = 1cm, minimum width = 2cm, fill=blue!20, thick, outer sep=0pt, inner sep=0pt, align=center, anchor=mid, text centered},
    doublejob/.style = {rectangle, minimum height = 1cm, minimum width = 4.1cm, fill=blue!20, thick, inner sep = 0pt, align=center, anchor=mid, text centered}
]
    \matrix[column sep=0.1cm, row sep=0.1cm, ampersand replacement=\& ] {
        \node[rectangle, minimum height = 1cm, minimum width = 1cm ] { $Resc_1$ };\&
        \node[draw, singlejob] {$Job_1$};\& \node[draw, singlejob] {$Job_2$};\&
        \node[draw, singlejob] {$Job_3$};\& \node[draw, singlejob] {$Job_4$};\\

        \node[rectangle, minimum height = 1cm, minimum width = 1cm ] { $Resc_2$ };\&
        \node[singlejob] (j51) {}; \& \node[singlejob] (j52) {}; \&
        \node[singlejob] (j61) {}; \& \node[singlejob] (j62) {}; \\
    };
    \node[draw,doublejob] (outer) [fit=(j51) (j52)] {$Job_5$};
    \node[draw,doublejob] (outer) [fit=(j61) (j62)] {$Job_6$};
\end{tikzpicture}
\end{document}

编译上述代码的结果


较小的示例(由 Andrew Stacey 添加)。问题似乎与fit库如何扩展其节点有关:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\begin{tikzpicture}
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {a};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b)
at (1.5,0) {a};
\draw[ultra thick,red] (a.base) -- (b.base);
\end{tikzpicture}

\end{document}

适合节点中的文本定位

答案1

我不知道这是否是漏洞特征fit节点的工作原理是确定节点应位于的中心以包含给定的所有点,然后将其文本框的大小设置为正确的大小,以便生成的节点将包含所有给定的点。问题在于设置文本框的高度和深度。这些必须加起来达到正确的大小,但可以相对彼此进行调整。一旦设置了高度,那么显然深度就是所需的总高度减去设置的高度,因此关键的一行是fit库代码中的以下内容:

  /tikz/text height/.expanded=\the\[email protected]\dp\pgfnodeparttextbox,

这表示将框的高度设置为所需高度的一半(\the\pgf@y)减去框深度的一半实际的框(-.5\dp\pgfnodeparttextbox)。测试了几个值后,我发现如果使用以下值,可以得到更好的对齐效果:

  /tikz/text height/.expanded=\the\pgf@y+.5\ht\pgfnodeparttextbox

-.5\dp\pgfnodepart文本框,

(这不是一个完全随机的选择,它是通过观察形状如何rectangle计算其锚点而提出的。)

幸运的是,紧接着这些语句,TeX 就会计算键every fit。所以我们可以在这里插入并替换公式。

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{fit}

\makeatletter
\tikzset{
  adjust fit placement/.style={
    every fit/.style={
      text height/.expanded=\the\pgf@y+.5\ht\pgfnodeparttextbox -.5\dp\pgfnodeparttextbox,
    }
  }
}
\makeatother

\begin{document}

\begin{tikzpicture}[every node/.style={outer sep=0pt}]
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {a};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b) at (1.5,0) {a};
\node[draw] (c) at (3,-1) {a};
\draw[ultra thick,green] (a.center) -- (b.center) -- (c.center);
\draw[ultra thick,red] (a.base) -- (b.base) -- (c.base);
\end{tikzpicture}

\begin{tikzpicture}[every node/.style={outer sep=0pt}]
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {bq};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b) at (1.5,0) {bq};
\node[draw] (c) at (3,-1) {bq};
\draw[ultra thick,green] (a.center) -- (b.center) -- (c.center);
\draw[ultra thick,red] (a.base) -- (b.base) -- (c.base);
\end{tikzpicture}

\tikzset{adjust fit placement}

\begin{tikzpicture}[every node/.style={outer sep=0pt}]
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {a};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b) at (1.5,0) {a};
\node[draw] (c) at (3,-1) {a};
\draw[ultra thick,green] (a.center) -- (b.center) -- (c.center);
\draw[ultra thick,red] (a.base) -- (b.base) -- (c.base);
\end{tikzpicture}

\begin{tikzpicture}[every node/.style={outer sep=0pt}]
\node[draw,inner sep=0pt,fit={(0,0) (1,-2)}] (a) {bq};
\node[draw,minimum width=1cm,minimum height=2cm,anchor=north west] (b) at (1.5,0) {bq};
\node[draw] (c) at (3,-1) {bq};
\draw[ultra thick,green] (a.center) -- (b.center) -- (c.center);
\draw[ultra thick,red] (a.base) -- (b.base) -- (c.base);
\end{tikzpicture}

\end{document}

附有前后图片:

贴合度调整前后

相关内容