TikZ 自动机周围的空白

TikZ 自动机周围的空白

我有以下 TikZ 图片:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}

\begin{document}
\begin{tikzpicture}[initial text={}, auto, >=stealth', node distance=0.75cm]
  \tikzstyle{every edge}=[->, draw]
  \node[state, initial] (i) {$q_0$};
  \node[state, right=of i] (0) {$0$};
  \node[state, right=of 0] (134) {$1, 3, 4$};
  \node[state, right=of 134] (25) {$2, 5$};
  \node[state, right=of 25] (13) {$1, 3$};
  \node[state, below=of 13] (2) {$2$};
  \node[state, below=of 25] (6) {$6$};
  \node[state, below=of 0] (es) {$\emptyset$};
  \node[state, accepting, right=of 13] (f) {$q_1$};

  \path (i) edge node {$1$} (0);
  \path (i) edge[bend left=60] node[above, pos=0.2] {$1$} (f);
  \path (0) edge node {$a$} (134);
  \path (0) edge node {$b$} (es);
  \path (0) edge[bend left=45] node {$1$} (f);
  \path (134) edge[loop below] node[above] {$a$} (134);
  \path (134) edge node {$b$} (25);
  \path (25) edge node {$a$} (13);
  \path (25) edge node {$b$} (6);
  \path (25) edge[bend left] node {$1$} (f);
  \path (13) edge[bend right=45] node {$a$} (134);
  \path (13) edge node {$b$} (2);
  \path (2) edge[bend left] node {$a$} (13);
  \path (2) edge[bend left] node[above] {$b$} (es);
  \path (2) edge node[pos=0.75] {$1$} (f);
  \path (6) edge node {$a$} (134);
  \path (6) edge node {$b$} (es);
  \path (es) edge[loop left] node {$a, b$} (es);
  \path (es) edge[bend right=70] node[pos=0.1, below] {$1$} (f);
\end{tikzpicture}
\end{document}

不幸的是,TikZ 在图片上方和下方生成了大量空白,我想摆脱它们。我发现它与边缘有关\path (i) edge[bend left=60] node[above, pos=0.2] {$1$} (f);,并且\path (es) edge[bend right=70] node[pos=0.1, below] {$1$} (f);当值bend ...=##改变时空间也会改变。TikZ 似乎为标签节点保留了空间,但这并不是必需的,因为节点不在其默认位置。

现在我的问题是:是什么导致了这种行为以及我该如何摆脱它?

答案1

TikZ 3.1.5 版引入了一个bbox库,可让您为边界框激活更好的计算。由于许可问题,该库已从pgf/TikZ 3.1.6 版中删除,请参阅https://github.com/pgf-tikz/pgf/releases/tag/3.1.6,但后来它被放在一个单独的包中,名为tikz-bbox

因此,如果您拥有 TikZ 版本 3.1.5 或该tikz-bbox软件包,我认为下面给出的答案应该有效。


使用\usetikzlibrary{bbox},原则上你只需要添加bezier bounding box=true到 的选项中tikzpicture。不过对于你的情况,它对两条路径都失败了,所以我把它们放在环境中scope,并在这里停用了边界框计算。请注意,编译确实需要更长的时间。

左侧的一点空白来自节点initial,屏幕截图中的灰色部分是 PDF 查看器的背景。

在此处输入图片描述

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning,bbox} % added bbox

\begin{document}
\begin{tikzpicture}[
   initial text={},
   auto,
   >=stealth',
   node distance=0.75cm,
   bezier bounding box=true,  % <-- added
   every edge/.style={->, draw}
   ]

  \node[state, initial] (i) {$q_0$};
  \node[state, right=of i] (0) {$0$};
  \node[state, right=of 0] (134) {$1, 3, 4$};
  \node[state, right=of 134] (25) {$2, 5$};
  \node[state, right=of 25] (13) {$1, 3$};
  \node[state, below=of 13] (2) {$2$};
  \node[state, below=of 25] (6) {$6$};
  \node[state, below=of 0] (es) {$\emptyset$};
  \node[state, accepting, right=of 13] (f) {$q_1$};

  \path (i) edge node {$1$} (0);
  \path (i) edge[bend left=60] node[above, pos=0.2] {$1$} (f);
  \path (0) edge node {$a$} (134);
  \path (0) edge node {$b$} (es);
  \path (0) edge[bend left=45] node {$1$} (f);
  \path (134) edge node {$b$} (25);
  \path (25) edge node {$a$} (13);
  \path (25) edge node {$b$} (6);
  \path (13) edge[bend right=45] node {$a$} (134);
  \path (13) edge node {$b$} (2);
  \path (2) edge[bend left] node {$a$} (13);
  \path (2) edge[bend left] node[above] {$b$} (es);
  \path (2) edge node[pos=0.75] {$1$} (f);
  \path (6) edge node {$a$} (134);
  \path (6) edge node {$b$} (es);
  \path (es) edge[loop left] node {$a, b$} (es);
  \path (es) edge[bend right=70] node[pos=0.1, below] {$1$} (f);
  
  % but deactivate for this bit
  \begin{scope}[bezier bounding box=false] 
    \path (134) edge[loop below] node[above] {$a$} (134);
    \path (25) edge[bend left] node {$1$} (f);
  \end{scope}

\end{tikzpicture}
\end{document}

答案2

事实证明,之所以添加空白,是因为 TikZ 包含了图片中边缘的控制点(参见当我“弯曲”边缘时,Tikz 使用哪种类型的曲线?)。

如果能够绘制控制点,这一点就会变得明显......

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}

\usetikzlibrary{decorations.pathreplacing,shapes.misc}
\tikzset{
  show control points/.style={
    decoration={show path construction, curveto code={
        \draw [blue, dashed]
        (\tikzinputsegmentfirst) -- (\tikzinputsegmentsupporta)
        node [at end, anchor=center, cross out, draw, solid, red, inner sep=2pt]{};
        \draw [blue, dashed]
        (\tikzinputsegmentsupportb) -- (\tikzinputsegmentlast)
        node [at start, anchor=center, cross out, draw, solid, red, inner sep=2pt]{};
      }
    },
    postaction=decorate
  },
}

\begin{document}
  \begin{tikzpicture}[initial text={}, auto, >=stealth', node distance=0.75cm]
  \tikzstyle{every edge}=[->, draw]
  \node[state, initial] (i) {$q_0$};
  \node[state, right=of i] (0) {$0$};
  \node[state, right=of 0] (134) {$1, 3, 4$};
  \node[state, right=of 134] (25) {$2, 5$};
  \node[state, right=of 25] (13) {$1, 3$};
  \node[state, below=of 13] (2) {$2$};
  \node[state, below=of 25] (6) {$6$};
  \node[state, below=of 0] (es) {$\emptyset$};
  \node[state, accepting, right=of 13] (f) {$q_1$};

  \path (i) edge node {$1$} (0);
  \path (i) edge[bend left=60, show control points] node[above, pos=0.2] {$1$} (f);
  \path (0) edge node {$a$} (134);
  \path (0) edge node {$b$} (es);
  \path (0) edge[bend left=45, show control points] node {$1$} (f);
  \path (134) edge[loop below] node[above] {$a$} (134);
  \path (134) edge node {$b$} (25);
  \path (25) edge node {$a$} (13);
  \path (25) edge node {$b$} (6);
  \path (25) edge[bend left, show control points] node {$1$} (f);
  \path (13) edge[bend right=45, show control points] node {$a$} (134);
  \path (13) edge node {$b$} (2);
  \path (2) edge[bend left, show control points] node {$a$} (13);
  \path (2) edge[bend left, show control points] node[above] {$b$} (es);
  \path (2) edge node[pos=0.75] {$1$} (f);
  \path (6) edge node {$a$} (134);
  \path (6) edge node {$b$} (es);
  \path (es) edge[loop left, show control points] node {$a, b$} (es);
  \path (es) edge[bend right=70, show control points] node[pos=0.1, below] {$1$} (f);
  \end{tikzpicture}
\end{document}

...并将输出与问题中提到的空格进行比较: 控制点正好位于图片的边缘

我仍然没有找到解决这个问题的方法,除非按照 Ignasi 的建议手动操作(参见有没有办法控制 TikZ 图片周围的空白?)。

相关内容