TikZ-适合多个矩阵单元的节点不想垂直对齐

TikZ-适合多个矩阵单元的节点不想垂直对齐

我想将名为 的节点的顶线ImportantStuff与 的顶线 对齐AlsoImportantStuff。我该怎么做?

图片

在此处输入图片描述

MWE(新)

\documentclass[
11pt,
a4paper
]
{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{
    tikz,
}

\usetikzlibrary{
    external,
    plotmarks,
    calc,
    matrix,
    intersections,
    fit
}

\tikzset{
hauptgruppe/.style={
    rectangle,
    minimum width=1cm,
    minimum height=1cm,
    font=\sffamily\bfseries,
    draw=black,
    very thick,
    fill=white,
},
infotext/.style={
    draw=black,
    rectangle,
    font=\sffamily\footnotesize,
    align=left,
},
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\matrix [
matrix of nodes,
nodes in empty cells,
row sep=5pt,
column sep=5pt,
inner sep=5pt,
row 1/.style={nodes={hauptgruppe}},
%row 2/.style={nodes={infotext, text width=0.25\linewidth, align=center}},
name=m,
]
{
    | (asdf)| {\parbox[t]{1.8cm}{AAAA}\hfill\parbox[t]{1.8cm}{BBBB}} &
    | (dfgdsfgadgf)| {BBBB} &
    | (regf3refg)| {BBBB} \\
    | [infotext, text width=0.25\linewidth, font=\sffamily\footnotesize, anchor=north] (ImportantStuff) | {%
        \begin{itemize}
        \item{AAA}
        \item{BBB}
        \item{CCC}
        \item{Arbitrary words. Who knows how much text exactly\ldots}
        \item{\ldots gets puts here\ldots}
%       \item{See?}
        \end{itemize}} & |[minimum width=0.25\linewidth, minimum height=2cm, anchor=north]| ~ & |[minimum width=0.25\linewidth, minimum height=1cm, anchor=north]| ~ \\
};
\node[infotext, font=\sffamily\footnotesize, text width=0.5\linewidth, fit=(m-2-2) (m-2-3), anchor=north] (AlsoImportantStuff){%
    \begin{itemize}
    \item{Longer text to make it worth it.}
    \item{And some words}
    \item{Even more}
    \end{itemize}};
\end{tikzpicture}
\end{center}
\end{document}

MWE(旧版),如果你想了解解决方案,这是必需的

\documentclass[
11pt,
a4paper
]
{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{
    tikz,
}

\usetikzlibrary{
    external,
    plotmarks,
    calc,
    matrix,
    intersections,
    fit
}

\tikzset{
hauptgruppe/.style={
    rectangle,
    minimum width=1cm,
    minimum height=1cm,
    font=\sffamily\bfseries,
    draw=black,
    very thick,
    fill=white,
},
gruppe/.style={
    rectangle,
    minimum width=2cm,
    minimum height=0.8cm,
    draw=gray,
    very thick,
    font=\sffamily\bfseries,
},
untergruppe/.style={
    rectangle,
    minimum width=2cm,
    minimum height=1cm, 
    text width=3.5cm,
    draw=gray,
    very thick,
    font=\sffamily,
},
infotext/.style={
    draw=black,
    rectangle,
    font=\sffamily\footnotesize,
    align=left,
},
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\matrix [
matrix of nodes,
nodes in empty cells,
row sep=5pt,
column sep=5pt,
inner sep=5pt,
row 1/.style={nodes={hauptgruppe}},
row 2/.style={nodes={infotext, text width=0.25\linewidth, align=center}},
%row 3/.style={nodes={infotext, text width=0.25\linewidth, font=\sffamily\footnotesize}},
row 4/.style={nodes={infotext, text width=0.25\linewidth, font=\sffamily\footnotesize}},
name=m,
%below=2cm of 
]
{
    | (ergergt3gdfg) | {Word} &
    | (efgsdfhdahf) | {Yo} &
    | (3fefgdsf) | {Lo} \\
    | (erg4zdhfsz)| {\parbox[t]{1.8cm}{AAAA}\hfill\parbox[t]{1.8cm}{BBBB}} &
    | (ElastomereBedhfwjeoifjwgispiel)| {BBBB} &
    | (woiefs9djfo)| {BBBB} \\
    | [infotext, text width=0.25\linewidth, font=\sffamily\footnotesize, anchor=north] (ImportantStuff) | {%
        \begin{itemize}
        \item{AAA}
        \item{BBB}
        \item{CCC}
        \item{Arbitrary words. Who knows how much text exactly\ldots}
        \item{\ldots gets puts here\ldots}
%       \item{See?}
        \end{itemize}} & |[minimum width=0.25\linewidth, minimum height=2cm, anchor=north]| ~ & |[minimum width=0.25\linewidth, minimum height=1cm, anchor=north]| ~ \\
    | (XXXXenschaften) | {%
        \begin{itemize}
        \item{A}
        \item{B}
        \item{C}
        \end{itemize}} &
    | (YYYYYY) | {%
        \begin{itemize}
        \item{A}
        \item{B}
        \item{C}
        \end{itemize}} &
    | (ZZZZZ) | {%
        \begin{itemize}
        \item{A}
        \item{B}
        \item{C}
        \end{itemize}}\\
};
\node[infotext, font=\sffamily\footnotesize, text width=0.5\linewidth, fit=(m-3-2) (m-3-3), anchor=north] (AlsoImportantStuff){%
    \begin{itemize}
    \item{Longer text to make it worth it.}
    \item{And some words}
    \item{Even more}
    \end{itemize}};
\end{tikzpicture}
\end{center}
\end{document}

答案1

north west您希望将框的左上角(即:它的锚点)放置在通过的水平线和通过的垂直线AlsoImportantStuff的交点处,因此您可以使用以下命令将这些规范转换为 tikz 语言:ImportantStuff.northElastomereBedhfwjeoifjwgispiel.west

\node[infotext, font=\sffamily\footnotesize, text width=0.5\linewidth, fit=(m-3-2) (m-3-3), 
    anchor=north west]                                             % <-------
    at (ImportantStuff.north-|ElastomereBedhfwjeoifjwgispiel.west) % <-------
    (AlsoImportantStuff){%                                                                     
    \begin{itemize}                                                                           
    \item{Longer text to make it worth it.}                                                   
    \item{And some words}                                                                     
    \item{Even more}                                                                          
    \end{itemize}}; 

其结果为:

结果

是的,我注意到盒子的宽度比它应该的稍微短一些,但这与您定义的 tikz 样式有关,而与所问的问题无关:-)

答案2

另一种方法是分别调整inner/outer sep。当前位置是默认值 0pt。此解决方案显示添加 inner sep=4.5pt,outer sep=-28pt 将对齐顶部,而 outer sep=30pt 将对齐底部

在此处输入图片描述

代码

\documentclass[
11pt,
a4paper
]
{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{
    tikz,
}

\usetikzlibrary{
    external,
    plotmarks,
    calc,
    matrix,
    intersections,
    fit
}

\tikzset{
hauptgruppe/.style={
    rectangle,
    minimum width=1cm,
    minimum height=1cm,
    font=\sffamily\bfseries,
    draw=black,
    very thick,
    fill=white,
},
gruppe/.style={
    rectangle,
    minimum width=2cm,
    minimum height=0.8cm,
    draw=gray,
    very thick,
    font=\sffamily\bfseries,
},
untergruppe/.style={
    rectangle,
    minimum width=2cm,
    minimum height=1cm, 
    text width=3.5cm,
    draw=gray,
    very thick,
    font=\sffamily,
},
infotext/.style={
    draw=black,
    rectangle,
    font=\sffamily\footnotesize,
    align=left,
},
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\matrix [
matrix of nodes,
nodes in empty cells,
row sep=5pt,
column sep=5pt,
inner sep=5pt,
row 1/.style={nodes={hauptgruppe}},
row 2/.style={nodes={infotext, text width=0.25\linewidth, align=center}},
%row 3/.style={nodes={infotext, text width=0.25\linewidth, font=\sffamily\footnotesize}},
row 4/.style={nodes={infotext, text width=0.25\linewidth, font=\sffamily\footnotesize}},
name=m,
%below=2cm of 
]
{
    | (ergergt3gdfg) | {Word} &
    | (efgsdfhdahf) | {Yo} &
    | (3fefgdsf) | {Lo} \\
    | (erg4zdhfsz)| {\parbox[t]{1.8cm}{AAAA}\hfill\parbox[t]{1.8cm}{BBBB}} &
    | (ElastomereBedhfwjeoifjwgispiel)| {BBBB} &
    | (woiefs9djfo)| {BBBB} \\
    | [infotext, text width=0.25\linewidth, font=\sffamily\footnotesize, anchor=north] (ImportantStuff) | {%
        \begin{itemize}
        \item{AAA}
        \item{BBB}
        \item{CCC}
        \item{Arbitrary words. Who knows how much text exactly\ldots}
        \item{\ldots gets puts here\ldots}
%       \item{See?}
        \end{itemize}} & |[minimum width=0.25\linewidth, minimum height=2cm, anchor=north]| ~ & |[minimum width=0.25\linewidth, minimum height=1cm, anchor=north]| ~ \\
    | (XXXXenschaften) | {%
        \begin{itemize}
        \item{A}
        \item{B}
        \item{C}
        \end{itemize}} &
    | (YYYYYY) | {%
        \begin{itemize}
        \item{A}
        \item{B}
        \item{C}
        \end{itemize}} &
    | (ZZZZZ) | {%
        \begin{itemize}
        \item{A}
        \item{B}
        \item{C}
        \end{itemize}}\\
};
\node[infotext, font=\sffamily\footnotesize, text width=0.5\linewidth, fit=(m-3-2) (m-3-3), anchor=north, inner sep=4.5pt,outer sep=-28pt] (AlsoImportantStuff){%
    \begin{itemize}
    \item{Longer text to make it worth it.}
    \item{And some words}
    \item{Even more}
    \end{itemize}};
\end{tikzpicture}
\end{center}
\end{document}

答案3

另一个解决方案,在我看来是最直接的。它基于@JLDiaz的答案,这个答案一开始就有效,但我继续尝试。

图片

在此处输入图片描述

代码

\documentclass[
11pt,
a4paper
]
{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{
    tikz,
}

\usetikzlibrary{
    external,
    plotmarks,
    calc,
    matrix,
    intersections,
    fit
}

\tikzset{
hauptgruppe/.style={
    rectangle,
    minimum width=1cm,
    minimum height=1cm,
    font=\sffamily\bfseries,
    draw=black,
    very thick,
    fill=white,
},
infotext/.style={
    draw=black,
    rectangle,
    font=\sffamily\footnotesize,
    align=left,
},
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\matrix [
matrix of nodes,
nodes in empty cells,
row sep=5pt,
column sep=5pt,
inner sep=5pt,
row 1/.style={nodes={hauptgruppe}},
%row 2/.style={nodes={infotext, text width=0.25\linewidth, align=center}},
name=m,
]
{
    | (asdf)| {\parbox[t]{1.8cm}{AAAA}\hfill\parbox[t]{1.8cm}{BBBB}} &
    | (dfgdsfgadgf)| {BBBB} &
    | (regf3refg)| {BBBB} \\
    | [infotext, text width=0.25\linewidth, font=\sffamily\footnotesize, anchor=north] (ImportantStuff) | {%
        \begin{itemize}
        \item{AAA}
        \item{BBB}
        \item{CCC}
        \item{Arbitrary words. Who knows how much text exactly\ldots}
        \item{\ldots gets puts here\ldots}
%       \item{See?}
        \end{itemize}} & |[minimum width=0.25\linewidth, minimum height=2cm, anchor=north]| ~ & |[minimum width=0.25\linewidth, minimum height=1cm, anchor=north]| ~ \\
};
%old
%\node[infotext, font=\sffamily\footnotesize, text width=0.5\linewidth, fit=(m-2-2) (m-2-3), anchor=north] (AlsoImportantStuff){%
%the solution
\node[infotext, font=\sffamily\footnotesize, text width=0.5\linewidth, fit=(m-2-2) (m-2-3), anchor=north west, minimum height=1.5cm, xshift=5pt] at (ImportantStuff.north east|-m-2-2.north west) {%
    \begin{itemize}
    \item{Longer text to make it worth it.}
    \item{And some words}
    \item{Even more}
    \end{itemize}};
\end{tikzpicture}
\end{center}
\end{document}

相关内容