如何在 TikZ 矩阵单元格中换行?

如何在 TikZ 矩阵单元格中换行?

我可以使用 TikZ 自己的工具在矩阵单元格中进行换行吗?或者我必须使用 -environmenttabular\parbox

MWE(不起作用,我包含了错误的代码)

\documentclass[
11pt
]{scrartcl}
\usepackage{
tikz,
relsize,
tgheros
}

\usetikzlibrary{
    calc,trees,shadows,positioning,arrows,chains,
    decorations.pathreplacing,
    decorations.pathmorphing,
    decorations.shapes,
    decorations.text,
    shapes,
    shapes.geometric,
    shapes.symbols,
    matrix,
    patterns,
    intersections,
    fit
}

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
    >=latex,
    line/.style={draw, thick, ->},
    phase/.style={
        text height=1.5ex,
        text depth=0ex,
        align=left,
        anchor=center,
        %   text width=6cm,
        font=\sffamily\bfseries\small,
        rectangle,
        minimum height=1.0cm,
        draw=black,
        very thick,
        fill=black!40
    }
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small,node distance=0.5cm]
\matrix (m) [
matrix of nodes,
nodes in empty cells,
row sep=6mm,
column sep=3mm,
inner sep=7pt
] {
    %
    |[phase]| This should include\\a line break\\
    |[phase], align=center| Here should be a line\\break as well.
};
\end{tikzpicture}
\end{center}
\end{document}

答案1

在节点内换行的常用方法tikz也可以在这里应用。您可以使用text width=<dimesion>和 如果您想在某个指定点换行,请在\\那里放置 。但要小心使用括号将其隐藏,否则它会被误认为是行的末尾。

\documentclass[
11pt
]{scrartcl}
\usepackage{
tikz,
relsize,
tgheros
}

\usetikzlibrary{
    calc,trees,shadows,positioning,arrows,chains,
    decorations.pathreplacing,
    decorations.pathmorphing,
    decorations.shapes,
    decorations.text,
    shapes,
    shapes.geometric,
    shapes.symbols,
    matrix,
    patterns,
    intersections,
    fit
}

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
    >=latex,
    line/.style={draw, thick, ->},
    phase/.style={
        %text height=1.5ex,
        %text depth=1.5ex,
        align=left,
        anchor=center,
           text width=6cm,
        font=\sffamily\bfseries\small,
        rectangle,
        minimum height=1.5cm,
        draw=black,
        very thick,
        fill=black!40
    }
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small,node distance=0.5cm]
\matrix (m) [
matrix of nodes,
nodes in empty cells,
row sep=6mm,
column sep=3mm,
inner sep=7pt
] {
    %
    |[phase]| {This should include\\a line break}\\
    |[phase, align=center]| {Here should be a line\\break as well}\\
};
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

相关内容