如何使用 tikz 对齐/调整带有表格/列表的彩色框架?

如何使用 tikz 对齐/调整带有表格/列表的彩色框架?

我想生成彩色框架并对齐它们。但我不知道如何实现这种对齐。如何摆脱多余的垂直和水平空间。红色、绿色和黄色列之间不应有间隙和重叠。我尝试了 tcolorboxes 和 mdframes。但我想使用纯 tikz。并且第一行和第二行的长度应该相等。并且括号应该垂直对齐。并且第二个表中的颜色应该延伸到最后一行文本的下方一点,并且三列的高度相等。在此处输入图片描述

\documentclass[12pt,parskip=full]{scrartcl}
\usepackage[a4paper,margin=1in,landscape]{geometry}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage[neverdecrease]{paralist}
\setdefaultleftmargin{\parindent}{}{}{}{}{}
\usepackage[super]{nth}

\begin{document}
%
% First Tabular / Picture / Blue
\begin{tikzpicture}[remember picture,overlay]
%
% Blue node (sem1): 3 Columns : 7cm | 8 cm | 6 cm
\node [%
rectangle,
shade, 
top    color = blue!1, 
bottom color = blue!10,
inner sep = 0pt,
anchor = north west] (sem1) {%
\begin{minipage}{\textwidth}
\vspace{-3mm} % <-- Get rid of this Line?
\begin{tabular}{p{7cm}p{8cm}p{6cm}}
\begin{compactitem}
\item 1.1
\item 1.2
\end{compactitem} &
\begin{compactitem}
\item 1.3
\item 1.4
\end{compactitem} &
\begin{compactitem}
\item 1.5
\item 1.6
\end{compactitem}
\end{tabular}
\vspace{-4mm} % <-- Get rid of this Line?
\end{minipage}
};

% Bracket with (1st)
\coordinate (A) at (sem1.north west);
\coordinate (D) at (sem1.south west);
\coordinate (B) at ($(A) - (0.2,0)$);
\coordinate (C) at ($(D) - (0.2,0)$);
\draw [ultra thick] 
    (A) -- (B) 
    (B) -- node[midway, left] {\nth{1}} (C) 
    (C) -- (D);
\end{tikzpicture}

\vspace{4mm}
% Second Tabular: Red : Green : Yellow
\begin{tabular}{p{7cm}p{8cm}p{6cm}}
%
% Red Node
\begin{tikzpicture}[remember picture,overlay]
\node[%
rectangle,
shade, 
top    color = red!12, 
bottom color = red!24,
inner sep = 0pt,
anchor = north west] (sem2red) {%
\parbox{7cm}{%
\begin{center}
\textbf{Red}
\end{center}
\begin{compactitem}
\item R.1
\item R.2
\item R.3
\end{compactitem}}};
%
% Bracket with (2nd)
\coordinate (A) at (sem2red.north west);
\coordinate (D) at (sem2red.south west);
\coordinate (B) at ($(A) - (0.2,0)$);
\coordinate (C) at ($(D) - (0.2,0)$);
\draw [ultra thick] 
    (A) -- (B) 
    (B) -- node[midway, left] {\nth{2}} (C) 
    (C) -- (D);
\end{tikzpicture} &
%
% Green Node
\begin{tikzpicture}[remember picture,overlay]
\node[%
rectangle,
shade, 
top    color = green!12, 
bottom color = green!24,
inner sep = 0pt,
anchor = north west] {%
\parbox{8cm}{%
\begin{center}
\textbf{Green}
\end{center}
\begin{compactitem}
\item G.1
\item G.2
\item G.3
\end{compactitem}}}; 
\end{tikzpicture} &
%
% Yellow Node
\begin{tikzpicture}[remember picture,overlay]
\node[%
rectangle,
shade, 
top    color = yellow!12, 
bottom color = yellow!24,
inner sep = 0pt,
anchor = north west]{%
\parbox{6cm}{%
\begin{center}
\textbf{Yellow}
\end{center}
\begin{compactitem}
\item Y.1
\item Y.2
\end{compactitem}}};
\end{tikzpicture}
\end{tabular}

\end{document}

答案1

这是一个可能的解决方案,也许不是您想要的。我\hspace在命令中添加了一些选项\parbox

\documentclass[12pt,parskip=full]{scrartcl}
\usepackage[a4paper,margin=1in,landscape]{geometry}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage[neverdecrease]{paralist}
\setdefaultleftmargin{\parindent}{}{}{}{}{}
\usepackage[super]{nth}

\begin{document}
    %
    % First Tabular / Picture / Blue
    \begin{tikzpicture}[remember picture,overlay]
    %
    % Blue node (sem1): 3 Columns : 7cm | 8 cm | 6 cm
    \node [%
    rectangle,
    shade, 
    top    color = blue!1, 
    bottom color = blue!10,
    inner sep = 0pt,
    anchor = north west] (sem1) {%
        \begin{minipage}{\textwidth}
        \vspace{-3mm} % <-- Get rid of this Line?
        \hspace{-3.5mm} % <-- Added horizontal space
        \begin{tabular}{p{7cm}p{8cm}p{6cm}}
        \begin{compactitem}
        \item 1.1
        \item 1.2
        \end{compactitem} &
        \begin{compactitem}
        \item 1.3
        \item 1.4
        \end{compactitem} &
        \begin{compactitem}
        \item 1.5
        \item 1.6
        \end{compactitem}
        \end{tabular}
        \vspace{-4mm} % <-- Get rid of this Line?
        \end{minipage}
    };

    % Bracket with (1st)
    \coordinate (A) at (sem1.north west);
    \coordinate (D) at (sem1.south west);
    \coordinate (B) at ($(A) - (0.2,0)$);
    \coordinate (C) at ($(D) - (0.2,0)$);
    \draw [ultra thick] 
    (A) -- (B) 
    (B) -- node[midway, left] {\nth{1}} (C) 
    (C) -- (D);
    \end{tikzpicture}

    \vspace{4mm}
    \hspace{-3.5mm} % <-- Added horizontal space
    % Second Tabular: Red : Green : Yellow
    \begin{tabular}{p{7cm}p{8cm}p{6cm}}
        %
        % Red Node
        \begin{tikzpicture}[remember picture,overlay]
        \node[%
        rectangle,
        shade, 
        top    color = red!12, 
        bottom color = red!24,
        inner sep = 0pt,
        anchor = north west] (sem2red) {%
            \parbox[b][7.5em][t]{7cm}{% <-- added [pos][height][contentpos] options in parbox
                \begin{center}
                \textbf{Red}
                \end{center}
                \begin{compactitem}
                \item R.1
                \item R.2
                \item R.3
                \end{compactitem}}};
        %
        % Bracket with (2nd)
        \coordinate (A) at (sem2red.north west);
        \coordinate (D) at (sem2red.south west);
        \coordinate (B) at ($(A) - (0.2,0)$);
        \coordinate (C) at ($(D) - (0.2,0)$);
        \draw [ultra thick] 
        (A) -- (B) 
        (B) -- node[midway, left] {\nth{2}} (C) 
        (C) -- (D);

        \end{tikzpicture} &
        %
        % Green Node
        \begin{tikzpicture}[remember picture,overlay]
        \node[%
        rectangle,
        shade, 
        top    color = green!12, 
        bottom color = green!24,
        inner sep = 0pt,
        anchor = north west] {%
            \parbox[b][7.5em][t]{8cm}{% <-- added [pos][height][contentpos] options in parbox
                \begin{center}
                \textbf{Green}
                \end{center}
                \begin{compactitem}
                \item G.1
                \item G.2
                \item G.3
                \end{compactitem}}}; 
        \end{tikzpicture} &
        %
        % Yellow Node
        \begin{tikzpicture}[remember picture,overlay]
        \node[%
        rectangle,
        shade, 
        top    color = yellow!12, 
        bottom color = yellow!24,
        inner sep = 0pt,
        anchor = north west]{%
            \parbox[b][7.5em][t]{8.8cm}{% <-- added [pos][height][contentpos] options in parbox
                \begin{center}
                \textbf{Yellow}
                \end{center}
                \begin{compactitem}
                \item Y.1
                \item Y.2
                \end{compactitem}}};
        \end{tikzpicture}
    \end{tabular}

\end{document}

在此处输入图片描述

这是另一种可能的解决方案,使用 tikz 节点\tikzstyle{}

\documentclass[12pt,parskip=full]{scrartcl}
\usepackage[a4paper,margin=1in,landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{array}

\usepackage[neverdecrease]{paralist}
\setdefaultleftmargin{\parindent}{}{}{}{}{}
\usepackage[super]{nth}
\begin{document}
    \tikzstyle{bluebox} = [
    shade, 
    top    color = blue!1, 
    bottom color = blue!10,
    rectangle,
    minimum width=.33\textwidth,
    minimum height=.1\textheight,
    outer xsep=2mm,
    inner sep=1mm,anchor = north west]

    \tikzstyle{redbox} = [
    shade, 
    top    color = red!12, 
    bottom color = red!24,
    rectangle,
    minimum width=7cm,
    minimum height=.1\textheight,
    outer xsep=2mm,
    inner xsep=3mm,anchor = north west]

    \tikzstyle{greenbox} = [
    shade, 
    top    color = green!12, 
    bottom color = green!24,
    rectangle,
    minimum width=7cm,
    minimum height=.1\textheight,
    outer xsep=2mm,
    inner xsep=3mm,anchor = north west]

    \tikzstyle{yellowbox} = [
    shade, 
    top    color = yellow!12, 
    bottom color = yellow!24,
    rectangle,
    minimum width=7cm,
    minimum height=.1\textheight,
    outer xsep=2mm,
    inner xsep=3mm,anchor = north west]

    \begin{tikzpicture}[remember picture,overlay]
        \node [bluebox] (sem1){
            \begin{tabular}{p{7.6cm}p{7.6cm}p{6.9cm}}
            \begin{compactitem}
            \item 1.1
            \item 1.2
            \end{compactitem} &
            \begin{compactitem}
            \item 1.3
            \item 1.4
            \end{compactitem} &
            \begin{compactitem}
            \item 1.5
            \item 1.6
            \end{compactitem}
            \end{tabular}
        };
        % Bracket with (1st)
        \coordinate (A) at (sem1.north west);
        \coordinate (D) at (sem1.south west);
        \coordinate (B) at ($(A) - (0.2,0)$);
        \coordinate (C) at ($(D) - (0.2,0)$);
        \draw [ultra thick] 
        (A) -- (B) 
        (B) -- node[midway, left] {\nth{1}} (C) 
        (C) -- (D);
    \end{tikzpicture}

    \begin{tikzpicture}[remember picture,overlay]
        \node [redbox] at (0,-1.5) (sem2red){
            \parbox[b][7.5em][t]{7cm}{% <-- added [pos][height][contentpos] options in parbox
                \begin{center}
                \textbf{Red}
                \end{center}
                \begin{compactitem}
                \item R.1
                \item R.2
                \item R.3
                \end{compactitem}}
        };
        \node [greenbox] at (sem2red.north east) (sem3green){
            \parbox[b][7.5em][t]{7cm}{% <-- added [pos][height][contentpos] options in parbox
                \begin{center}
                \textbf{Green}
                \end{center}
                \begin{compactitem}
                \item G.1
                \item G.2
                \item G.3
                \end{compactitem}}
        };
        \node [yellowbox] at (sem3green.north east) (){
            \parbox[b][7.5em][t]{7cm}{% <-- added [pos][height][contentpos] options in parbox
                \begin{center}
                \textbf{Yellow}
                \end{center}
                \begin{compactitem}
                \item Y.1
                \item Y.2
                \end{compactitem}}
        };  

        \coordinate (A) at (sem2red.north west);
        \coordinate (D) at (sem2red.south west);
        \coordinate (B) at ($(A) - (0.2,0)$);
        \coordinate (C) at ($(D) - (0.2,0)$);
        \draw [ultra thick] 
        (A) -- (B) 
        (B) -- node[midway, left] {\nth{2}} (C) 
        (C) -- (D);
    \end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

编辑 (简化语法)

一个纯粹的tikz选择是把所有东西都放进去,matrix of nodes得到:

在此处输入图片描述

负责matrix of nodes条目的对齐,因此您不需要将tabular环境等嵌入到节点中,这使得代码相对紧凑(模数左括号所需的代码):

\documentclass[12pt,parskip=full]{scrartcl}
\usepackage[a4paper,margin=1in,landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,backgrounds}
\newdimen\topy
\newdimen\boty
\usepackage[super]{nth}

\begin{document}

\newcommand\MyBullet{$\bullet$\space}
\tikzset{%
    % a pic to add a labelled [ to certain rows
    pics/leftbracket/.style args={#1, #2, #3}{
       code = {
         \pgfextracty\topy{\pgfpointanchor{M-#1-1}{north west}}% y-coords of rows
         \pgfextracty\boty{\pgfpointanchor{M-#2-1}{south west}}
         \topy=\dimexpr\topy-\boty+1pt% height of rows
         % draw the delimiter with the correct height
         \node[rectangle, left delimiter={[}, minimum height=\topy]
                at ($ (0,0.5pt)+(M-#1-1.north west)!0.5!(M-#2-1.south west) $){};
         % the label
         \node at ($ (M-#1-1.north west)!0.5!(M-#2-1.south west) $)[left=3mm]{#3};
      }
   },
}

\begin{tikzpicture}[nobullet/.code={\let\MyBullet\relax},
                    centered/.style={align=center, nobullet},
  ]
  \matrix (M)[matrix of nodes,row sep=0mm,column sep=1mm,
              outer sep=0mm,
              nodes={align=left, text width=0.3\textwidth,
                     execute at begin node=\MyBullet},
    ]{
    1.1 & 1.3  & 1.3\\
    1.2 & 1.4  & 1.6\\[3mm]% extra space between rows
    |[centered]|Red & |[centered]|Green & |[centered]|Yellow\\
    R.1  & G.1 & Y.1\\
    R.2  & G.2 & Y.2\\
    R.3  & G.3 & |[nobullet]|\\
   };
   \pic{leftbracket={1, 2, \nth{1}}};
   \pic{leftbracket={3, 6, \nth{2}}};
   % now draw the background colours using layers
   \begin{scope}[on background layer, rounded corners]
      \fill[top color=blue!0, bottom color=blue!10]
             (M-1-1.north west) rectangle (M-2-3.south east);
      \fill[red!10]    (M-3-1.north west) rectangle (M-6-1.south east);
      \fill[green!20]  (M-3-2.north west) rectangle (M-6-2.south east);
      \fill[yellow!30] (M-3-3.north west) rectangle (M-6-3.south east);
   \end{scope}
\end{tikzpicture}

\end{document}

最棘手的部分是在左侧绘制括号,这是使用pic带有三个参数的函数完成的:

\pic{leftbracket={index of first row, index of last row, label}

[这将在矩阵的first和行之间绘制一个左括号,其中指定了。此代码基本是通用的,因此应该很容易适应其他分隔符,这些分隔符可以放置在矩阵的其他侧。 lastlabel

使用库绘制矩阵后,行会手动着色backgrounds。对于行条目,我已使用\MyBulletexecute at begin node自动添加$\bullet$\space到每个节点。项目符号被样式“关闭” ,该样式用于黄色部分的最后一行。如果您希望“项目”自动编号,您可以在使用nobullet中添加一个计数器,但由于您正在使用,我怀疑您不想要这个。\MyBullet\refstepcountercompactitem

默认情况下,中的条目是左对齐的。我已通过使用应用在环境开始时定义的样式来matrix of nodes覆盖第二行中的列标题的此设置。|[centered]|centeredtikzpicture

相关内容