我尝试了混淆矩阵的 TikZ 示例,我想设置换行符,但它不起作用。如何在第二和第三个节点中设置换行符?当我尝试使用“\”时,我收到一些错误。
你能帮我吗?
非常感谢!
\begin{tikzpicture}[
box/.style={draw,rectangle,minimum size=3cm,text width=1.5cm,align=left}]
\matrix (conmat) [row sep=.1cm,column sep=.1cm] {
\node (tpos) [box,
label=left:\textbf{G},
label=above:\textbf{G},
] {True \\ positive};
&
\node (fneg) [box,
label=above:\textbf{Sentence with Line break!?},
label=above right:\textbf{Summe},
label=right:\( \mathrm{P}' \)] {False \\ negative};
\\
\node (fpos) [box,
label=left:\textbf{Sentence with Line break!?},
label=below left:\textbf{Summe},
label=below:P] {False \\ positive};
&
\node (tneg) [box,
label=right:\( \mathrm{N}' \),
label=below:N] {True \\ negative};
\\
};
\node [rotate=90,left=.05cm of conmat,anchor=center,text width=1.5cm,align=center] {\textbf{Ergebnis}};
\node [above=.05cm of conmat] {\textbf{Erwartungswert}};
\end{tikzpicture}
答案1
要将文本分成两部分,换行符必须\\
不包含在 \textbf{...} 组中。这里将句子分成两组就足够了。
label=above:\textbf{Sentence with}\\ \textbf{Line break!?},
我把第二段文字里的粗体字去掉了,就没问题了。
label={left:Sentence with\\ Line break!?},
我已将其设置align=center
为全局选项。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[align=center,
box/.style={draw,rectangle,minimum size=3cm,text width=1.5cm,align=left}]
\matrix (conmat) [row sep=.1cm,column sep=.1cm] {
\node (tpos) [box,
label=left:\textbf{G},
label=above:\textbf{G},
] {True \\ positive};
&
\node (fneg) [box,
label=above:\textbf{Sentence with}\\ \textbf{Line break!?},
label=above right:\textbf{Summe},
label=right:\( \mathrm{P}' \)
] {False \\ negative};
\\
\node (fpos) [box,
label={left:Sentence with\\ Line break!?},
label=below left:\textbf{Summe},
label=below:P
] {False \\ positive};
&
\node (tneg) [box,
label=right:\( \mathrm{N}' \),
label=below:N
] {True \\ negative};
\\
};
\node [rotate=90,left=.05cm of conmat,anchor=center,text width=1.5cm] {\textbf{Ergebnis}};
\node [above=.05cm of conmat] {\textbf{Erwartungswert}};
\end{tikzpicture}
\end{document}
答案2
我意识到 cis 在我之前就提出了这一点,所以完全归功于他们。字体键font
和就是为此而设计的。你应该使用node font
而不是将文本换行,这样就没问题了。\textbf
font=\bfseries
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
box/.style={draw,rectangle,minimum size=3cm,text width=1.5cm,align=left}]
\matrix (conmat) [row sep=.1cm,column sep=.1cm] {
\node (tpos) [box,
label=left:\textbf{G},
label=above:\textbf{G},
] {True \\ positive};
&
\node (fneg) [box,
label={[font=\bfseries,align=center]above:Sentence with\\ Line break!?},
label=above right:\textbf{Summe},
label=right:\( \mathrm{P}' \)] {False \\ negative};
\\
\node (fpos) [box,
label={[font=\bfseries,align=center]left:Sentence with\\ Line break!?},
label=below left:\textbf{Summe},
label=below:P] {False \\ positive};
&
\node (tneg) [box,
label=right:\( \mathrm{N}' \),
label=below:N] {True \\ negative};
\\
};
\node [rotate=90,left=.05cm of conmat,anchor=center,text width=1.5cm,align=center] {\textbf{Ergebnis}};
\node [above=.05cm of conmat] {\textbf{Erwartungswert}};
\end{tikzpicture}
\end{document}