确保连续 tikz 图片之间的垂直间距

确保连续 tikz 图片之间的垂直间距

我需要生成一个由 3 个连续的 tikz 图片组成的图形,中间有垂直间距。我尝试了以下代码:

\begin{figure}[h]
\caption{Cross-Validation with Three-Segmented Training Dataset}
\vspace{0.5 cm}
\begin{tikzpicture}
\node[text width=3cm] (Iteration 1)
    {Iteration 1};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm,
    fill = green!30!, right = 0.5cm of Iteration 1, rectangle] (Segment 1) {Training};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = green!30!, rectangle, right = 0.1cm of Segment 1] (Segment 2) {Training};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = red!30!, rectangle, right = 0.1cm of Segment 2] (Segment 3) {Testing};
\end{tikzpicture}
\vspace{0.25 cm}
\begin{tikzpicture}
\node[text width=3cm] (Iteration 2)
    {Iteration 2};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm,
    fill = red!30!, right = 0.5cm of Iteration 2, rectangle] (Segment 1) {Testing};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = green!30!, rectangle, right = 0.1cm of Segment 1] (Segment 2) {Training};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = green!30!, rectangle, right = 0.1cm of Segment 2] (Segment 3) {Training};
\end{tikzpicture}
\vspace{0.25 cm}
\begin{tikzpicture}
\node[text width=3cm] (Iteration 3)
    {Iteration 3};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm,
    fill = green!30!, right = 0.5cm of Iteration 3, rectangle] (Segment 1) {Training};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = red!30!, rectangle, right = 0.1cm of Segment 1] (Segment 2) {Testing};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = green!30!, rectangle, right = 0.1cm of Segment 2] (Segment 3) {Training};
\end{tikzpicture}
\centering
\end{figure}

附加的输出不正确。为什么 \vspace{0.25cm} 命令在第一和第二张 tikz 图片(分别是迭代 1 和迭代 2)之间无法正常工作?请提供最简单/最易行的解决方案。提前谢谢您。

答案1

tikzpicture 的位置像字母一样,你有

a \vspace{0.25cm} b\vspace{0.25cm} c\vspace{0.25cm} d\vspace{0.25cm} e

在这样的行中,a b c d e内容会像平常一样排版,并且可能会被分成多行,然后在分行之后,它们\vspace会被添加到它们“自然”落下的行之后。

这就是为什么你所有的 vspace 都在同一位置。你想在每一对之后有一个段落分隔符

a b

\vspace{0.25cm}

c d

\vspace{0.25cm}

e f

在垂直模式下使用几乎总是最好的\vspace,所以在空白行之后。在水平模式下使用时的行为是可以预测的,但通常不是理想的。

答案2

请提供最简单的解决方案。

我发现以下两个解决方案比您尝试做的更简单。

在这两种情况下,整个图表实际上只是一张 TikZ 图片,因此我们可以使用 TikZ 来控制所有物体之间的距离。

第一个使用matrix of nodes来自matrix库的,第二个使用chains库的。

我还使用该caption包来指定浮点数的标题和内容之间的距离以及它的位置(以便将距离插入到正确的位置)。

根据您的文档类别,可能存在更好的解决方案,因为它们对浮动和标题管理有自己的控制。

代码

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains, matrix}

\usepackage[skip=.25cm, figureposition=above]{caption}
\tikzset{every matrix/.append style={% no padding around matrices
  every outer matrix/.append style={inner sep=+0pt, outer sep=auto}}}
\begin{document}
\begin{figure}
\caption{Cross-Validation with Three-Segmented Training Dataset}
\centering
\tikz
  \matrix[
    matrix of nodes,
    row    sep = .25cm,
    column sep = .50cm,
    nodes={
      draw,
      minimum width  = 2cm,
      minimum height = 1cm,
    },
    column 1/.style={
      nodes          = path only, % disables any drawing, filling etc
      text width     = 3cm,
    },
    tr/.style={fill=green!30},
    te/.style={fill=red!30},
  ]{
    Iteration 1 & |[tr]| Training & |[tr]| Training & |[te]|  Testing \\
    Iteration 2 & |[te]|  Testing & |[tr]| Training & |[tr]| Training \\
    Iteration 3 & |[tr]| Training & |[te]|  Testing & |[tr]| Training \\
  };
\end{figure}

\begin{figure}
\caption{Cross-Validation with Three-Segmented Training Dataset}
\centering
\tikz[
  node distance  = .25cm and .50cm,
  start chain    = rows going below,
  minimum height = 1cm, % valid for all nodes
  iter node/.style={
    path only,
    text width = 3cm,
  },
  ing  node/.style={
    draw,
    minimum width = 2cm
  },
  tr/.style={ing node, fill=green!30, node contents=Training},
  te/.style={ing node, fill=  red!30, node contents= Testing},
] \foreach[count=\i] \List in {{tr, tr, te},
                               {te, tr, tr},
                               {tr, te, tr}}
    \node[on chain=rows, iter node] {Iteration \i}
      [start branch=\i- going base right]
      node foreach \st in \List [on chain=rows/\i-, \st]{};
\end{figure}
\end{document}

答案3

这用于\lineskip添加 tikzpictures 之间的间距。

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h]
\setlength{\lineskip}{0.25cm}%
\caption{Cross-Validation with Three-Segmented Training Dataset}

\begin{tikzpicture}
\node[text width=3cm] (Iteration 1)
    {Iteration 1};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm,
    fill = green!30!, right = 0.5cm, rectangle] (Segment 1) at(Iteration 1.east) {Training};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = green!30!, rectangle, right = 0.1cm] (Segment 2) at (Segment 1.east) {Training};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = red!30!, rectangle, right = 0.1cm] (Segment 3) at (Segment 2.east) {Testing};
\end{tikzpicture}
\begin{tikzpicture}
\node[text width=3cm] (Iteration 2)
    {Iteration 2};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm,
    fill = red!30!, right = 0.5cm, rectangle] (Segment 1) at(Iteration 2.east) {Testing};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = green!30!, rectangle, right = 0.1cm] (Segment 2) at(Segment 1.east){Training};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = green!30!, rectangle, right = 0.1cm] (Segment 3) at(Segment 2.east) {Training};
\end{tikzpicture}
\begin{tikzpicture}
\node[text width=3cm] (Iteration 3)
    {Iteration 3};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm,
    fill = green!30!, right = 0.5cm, rectangle] (Segment 1) at(Iteration 3.east) {Training};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = red!30!, rectangle, right = 0.1cm] (Segment 2) at(Segment 1.east) {Testing};
  \node [draw, 
    minimum width = 2cm, 
    minimum height = 1cm, fill = green!30!, rectangle, right = 0.1cm] (Segment 3) at(Segment 2.east){Training};
\end{tikzpicture}
\centering
\end{figure}

\end{document}

答案4

\centering最后使用肯定不是最好的主意。

您需要三个段落,并将行间距设置为等于矩形之间的距离,即 0.1 厘米。

您还希望避免重复任务并定义样式。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{caption}

\captionsetup[figure]{position=top}

\begin{document}

\begin{figure}[htp]
\centering

\caption{Cross-Validation with Three-Segmented Training Dataset}

\setlength{\lineskip}{0.1cm}
\tikzset{
  segmented/.style={draw,minimum width=2cm,minimum height=1cm,rectangle},
  G/.style={fill=green!30},
  R/.style={fill=red!30},
}

\begin{tikzpicture}
\node[text width=3cm] (Iteration 1) {Iteration 1};
\node[segmented,G,right = 0.5cm of Iteration 1] (Segment 1) {Training};
\node[segmented,G,right = 0.1cm of Segment 1] (Segment 2) {Training};
\node[segmented,R,right = 0.1cm of Segment 2] (Segment 3) {Testing};
\end{tikzpicture}

\begin{tikzpicture}
\node[text width=3cm] (Iteration 2) {Iteration 2};
\node[segmented,R,right = 0.5cm of Iteration 2] (Segment 1) {Testing};
\node[segmented,G,right = 0.1cm of Segment 1] (Segment 2) {Training};
\node[segmented,G,right = 0.1cm of Segment 2] (Segment 3) {Training};
\end{tikzpicture}

\begin{tikzpicture}
\node[text width=3cm] (Iteration 3) {Iteration 3};
\node[segmented,G,right = 0.5cm of Iteration 3] (Segment 1) {Training};
\node[segmented,R,right = 0.1cm of Segment 1] (Segment 2) {Testing};
\node[segmented,G,right = 0.1cm of Segment 2] (Segment 3) {Training};
\end{tikzpicture}

\end{figure}

\end{document}

在此处输入图片描述

我没有设置左侧部分的宽度,而是设置了inner sep=0pt

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{caption}

\captionsetup[figure]{position=top}

\begin{document}

\begin{figure}[htp]
\centering

\caption{Cross-Validation with Three-Segmented Training Dataset}

\setlength{\lineskip}{0.1cm}
\tikzset{
  segmented/.style={draw,minimum width=2cm,minimum height=1cm,rectangle},
  G/.style={fill=green!30},
  R/.style={fill=red!30},
}

\begin{tikzpicture}
\node[inner sep=0pt] (Iteration 1) {Iteration 1};
\node[segmented,G,right = 0.5cm of Iteration 1] (Segment 1) {Training};
\node[segmented,G,right = 0.1cm of Segment 1] (Segment 2) {Training};
\node[segmented,R,right = 0.1cm of Segment 2] (Segment 3) {Testing};
\end{tikzpicture}

\begin{tikzpicture}
\node[inner sep=0pt] (Iteration 2) {Iteration 2};
\node[segmented,R,right = 0.5cm of Iteration 2] (Segment 1) {Testing};
\node[segmented,G,right = 0.1cm of Segment 1] (Segment 2) {Training};
\node[segmented,G,right = 0.1cm of Segment 2] (Segment 3) {Training};
\end{tikzpicture}

\begin{tikzpicture}
\node[inner sep=0pt] (Iteration 3) {Iteration 3};
\node[segmented,G,right = 0.5cm of Iteration 3] (Segment 1) {Training};
\node[segmented,R,right = 0.1cm of Segment 1] (Segment 2) {Testing};
\node[segmented,G,right = 0.1cm of Segment 2] (Segment 3) {Training};
\end{tikzpicture}

\end{figure}

\end{document}

在此处输入图片描述

相关内容