在tikz中设置具有不同分割的矩形的相同高度

在tikz中设置具有不同分割的矩形的相同高度

我发布了同样的问题所以。当两个节点是分成不同数量部分的矩形时,我无法得到具有相同高度(10 厘米)的图形。当对象分成更多部分时,总高度似乎会增加。如何解决这个问题?

可重现的示例和实际输出位于关联

\documentclass[a4paper]{scrbook}
\usepackage[hmargin=2.5cm,vmargin=2.5cm]{geometry}
\usepackage[utf8x]{inputenc}

\usepackage{caption}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{subcaption} 

\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows,positioning,shapes,calc,arrows}

\newcommand\Textbox[2]{%
    \parbox[c][#1][c]{2.3cm}{\centering#2}}

\begin{document}

\begin{figure}
    \begin{subfigure}[b]{0.32\textwidth}
        \centering
        \resizebox{\linewidth}{!}{

        \begin{tikzpicture}
        \node (h1) {\textbf{Text I}};
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=3, below of= h1,yshift=.5cm,
        rectangle split part fill={gray!30, blue!20, green!15},
        anchor=north east]{\textbf{Tit A}
            \nodepart{two}\Textbox{6.7cm}{Cir 1\\67\% } 
            \nodepart{three}\Textbox{3.3cm}{Cir 2\\33\%} };
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=4, below of=h1,yshift=0.5cm,
        rectangle split part fill={gray!30, blue!15, green!15,green!30},
        anchor=north west]{\textbf{Tit B}
            \nodepart{two}\Textbox{4.6cm}{Cir 1\\ 46\%}  
            \nodepart{three}\Textbox{3cm}{Met \\30\%}  
            \nodepart{four}\Textbox{2.4cm}{Let\\24\%} };
        \end{tikzpicture}
        }
    \end{subfigure}
    \begin{subfigure}[b]{0.32\textwidth}
    \centering
    \resizebox{\linewidth}{!}{
        \begin{tikzpicture}
        \node (h1) {\textbf{Text II}};
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=3, below of= h1,yshift=0.5cm,
        rectangle split part fill={gray!30, blue!20, green!15},
        anchor=north east]{\textbf{Tit A}
            \nodepart{two}\Textbox{3.3cm}{Part 1\\33\% } 
            \nodepart{three}\Textbox{6.7cm}{Part 2\\67\%} };
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=5, below of=h1,yshift=0.5cm,
        rectangle split part fill={gray!30, blue!15, green!15,green!30,yellow!15},
        anchor=north west]{\textbf{Tit B}
            \nodepart{two}\Textbox{3.3cm}{Cr1 \\ 33\%}  
            \nodepart{three}\Textbox{2.5cm}{Cr2\\25\%}  
            \nodepart{four}\Textbox{2.2cm}{Cr3\\22\%}
            \nodepart{five}\Textbox{2.0cm}{Cr4\\20\%} };
        \end{tikzpicture}
    }
    \end{subfigure}

\end{figure}

\end{document}

在此处输入图片描述

答案1

正如我在对 OP 的评论中指出的那样,似乎存在某种\fboxsep我不知道如何修复的效果。但是,我可以通过重新定义为从每个构造的垂直高度中减去(或 1.62ex) 来tikz克服它。\Textbox7.7pt

\newcommand\Textbox[2]{%
    \parbox[c][\dimexpr#1-7.7pt][c]{2.3cm}{\centering#2}}

这是 MWE

\documentclass[a4paper]{scrbook}
\usepackage[hmargin=2.5cm,vmargin=2.5cm]{geometry}
\usepackage[utf8x]{inputenc}

\usepackage{caption}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{subcaption} 

\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows,positioning,shapes,calc,arrows}

\newcommand\Textbox[2]{%
    \parbox[c][\dimexpr#1-7.7pt][c]{2.3cm}{\centering#2}}

\begin{document}

\begin{figure}
    \begin{subfigure}[b]{0.32\textwidth}
        \centering
        \resizebox{\linewidth}{!}{

        \begin{tikzpicture}
        \node (h1) {\textbf{Text I}};
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=3, below of= h1,yshift=.5cm,
        rectangle split part fill={gray!30, blue!20, green!15},
        anchor=north east]{\textbf{Tit A}
            \nodepart{two}\Textbox{6.7cm}{Cir 1\\67\% } 
            \nodepart{three}\Textbox{3.3cm}{Cir 2\\33\%} };
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=4, below of=h1,yshift=0.5cm,
        rectangle split part fill={gray!30, blue!15, green!15,green!30},
        anchor=north west]{\textbf{Tit B}
            \nodepart{two}\Textbox{4.6cm}{Cir 1\\ 46\%}  
            \nodepart{three}\Textbox{3cm}{Met \\30\%}  
            \nodepart{four}\Textbox{2.4cm}{Let\\24\%} };
        \end{tikzpicture}
        }
    \end{subfigure}
    \begin{subfigure}[b]{0.32\textwidth}
    \centering
    \resizebox{\linewidth}{!}{
        \begin{tikzpicture}
        \node (h1) {\textbf{Text II}};
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=3, below of= h1,yshift=0.5cm,
        rectangle split part fill={gray!30, blue!20, green!15},
        anchor=north east]{\textbf{Tit A}
            \nodepart{two}\Textbox{3.3cm}{Part 1\\33\% } 
            \nodepart{three}\Textbox{6.7cm}{Part 2\\67\%} };
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=6, below of=h1,yshift=0.5cm,
        rectangle split part fill={gray!30, blue!15, green!15,green!30,yellow!15,
                                   red!15},
        anchor=north west]{\textbf{Tit B}
            \nodepart{two}\Textbox{3.3cm}{Cr1 \\ 33\%}  
            \nodepart{three}\Textbox{1.5cm}{Cr2\\15\%}  
            \nodepart{four}\Textbox{2.2cm}{Cr3\\22\%}
            \nodepart{five}\Textbox{1.2cm}{Cr4\\12\%}
            \nodepart{six}\Textbox{1.8cm}{Cr4\\18\%} };
        \end{tikzpicture}
    }
    \end{subfigure}

\end{figure}
\end{document}

在此处输入图片描述

我上面提到的所谓的\fboxsep效果(在定义的图像中添加有限大小的边框)可以通过重新定义来看到

\renewcommand\Textbox[2]{%
  \parbox[c]{2.3cm}{\centering#2}%
  \rule[-.5\dimexpr#1]{2pt}{#1}%
}

完成后,结果如下所示,清楚地显示了框远远超出了规则的垂直范围:

在此处输入图片描述

答案2

正如手册所述(第 716-171 页)

垂直分割时,矩形分割将满足所有minimum width要求,但任何要求 minimum height都会被忽略。相反,水平分割时,minimum height要求将得到满足,但任何要求minimum width都会被忽略。此外,inner sep适用于所使用的每个部件,因此无法单独为特定部件指定。

您正尝试强制将 添加minimum height到整个多部分中,并使用\Textbox命令固定每个部分的高度。这很完美,但您忘记了在绘制边框之前inner sep会将 添加到每个\Textbox尺寸中。如果两个多部分节点都有相等的部分,这不是问题,但只要一个有树而其他四个有树,就会出现高度差异。

最好的解决方案可能是强制inner sep=0ptinner ysep=0pt。但这还不够,因为您还需要考虑有多少水平边框并补偿平行多部分节点之间的差异。在左侧示例中,Tit A节点有三个组件而Tit B有四个,而在右侧示例中,分区为三个而五个。这意味着右侧框将比左侧框\pgflinewidth2\pgflinewidth

您必须决定要在哪里调整这些差异。在下面的代码中,我缩短了右下部分。我也在inner sep=0pt所有节点中使用了它,\Textbox也将其用于标题。

\documentclass[a4paper]{scrbook}
\usepackage[hmargin=2.5cm,vmargin=2.5cm]{geometry}
\usepackage[utf8x]{inputenc}

\usepackage{caption}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{subcaption} 

\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows,positioning,shapes,calc,arrows}

\newcommand\Textbox[2]{%
    \parbox[c][#1][c]{2.3cm}{\centering#2}}

\begin{document}

\begin{figure}
    \begin{subfigure}[b]{0.32\textwidth}
        \centering
        \resizebox{\linewidth}{!}{

        \begin{tikzpicture}
        \node (h1) {\textbf{Text I}};
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=3, below of= h1,yshift=.5cm,
        rectangle split part fill={gray!30, blue!20, green!15},
        anchor=north east,
        inner sep=0pt %<--------------------------
        ]{\Textbox{1cm}{Tit A}
            \nodepart{two}\Textbox{6.7cm}{Cir 1\\67\% } 
            \nodepart{three}\Textbox{3.3cm}{Cir 2\\33\%} };
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=4, below of=h1,yshift=0.5cm,
        rectangle split part fill={gray!30, blue!15, green!15,green!30},
        anchor=north west,
        inner sep=0pt %<--------------------------
        ]{\Textbox{1cm}{Tit B}
            \nodepart{two}\Textbox{4.6cm}{Cir 1\\ 46\%}  
            \nodepart{three}\Textbox{3cm}{Met \\30\%}  
            \nodepart{four}\Textbox{2.4cm-\pgflinewidth} %<-----
                 {Let\\24\%} };
        \end{tikzpicture}
        }
    \end{subfigure}
    \begin{subfigure}[b]{0.32\textwidth}
    \centering
    \resizebox{\linewidth}{!}{
        \begin{tikzpicture}
        \node (h1) {\textbf{Text II}};
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=3, below of= h1,yshift=0.5cm,
        rectangle split part fill={gray!30, blue!20, green!15},
        anchor=north east,
        inner sep=0pt %<--------------------------
        ]{\Textbox{1cm}{Tit A}
            \nodepart{two}\Textbox{3.3cm}{Part 1\\33\% } 
            \nodepart{three}\Textbox{6.7cm}{Part 2\\67\%} };
        \node[draw,
        align=center,
        text width=2.5cm,
        %minimum height=7cm,
        draw=red!50,
        rectangle split, 
        rectangle split parts=5, below of=h1,yshift=0.5cm,
        rectangle split part fill={gray!30, blue!15, green!15,green!30,yellow!15},
        anchor=north west,
        inner sep=0pt %<--------------------------
        ]{\Textbox{1cm}{Tit B}
            \nodepart{two}\Textbox{3.3cm}{Cr1 \\ 33\%}  
            \nodepart{three}\Textbox{2.5cm}{Cr2\\25\%}  
            \nodepart{four}\Textbox{2.2cm}{Cr3\\22\%}
            \nodepart{five}\Textbox{2.0cm-2\pgflinewidth}%<-----
            {Cr4\\20\%} };
        \end{tikzpicture}
    }
    \end{subfigure}

\end{figure}
\end{document}

在此处输入图片描述

相关内容