我正在尝试使用 TikZ 并排绘制两个图形(实际上是三个,但两个也不行)。我使用 \newcommand 在 matrix.tex 中添加了两个 TikZ 图形,并尝试将它们添加到主文件中。但它不起作用。(我的想法来自将 TikZ 绘图并排放置在小页面中)
\documentclass[twoside,11pt]{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\input{matrix.tex}
\begin{figure}[t]
\captionsetup[subfigure]{font=footnotesize}
\centering
\subcaptionbox{Confution Matrix}[.4\textwidth]{
\begin{tikzpicture}[box/.style={draw,rectangle,text width=1.5cm,align=left}]
\confmatrix
\end{tikzpicture} }
\subcaptionbox{Cost Matrix}[.3\textwidth] {
\begin{tikzpicture}[box/.style={draw,rectangle,minimum size =1cm,text width=1.5cm,align=left}]
\costmatrix
\end{tikzpicture} }
\end{figure}
\end{document}
矩阵.tex
\newcommand{\confmatrix}{
\matrix (conmat) [row sep=.01cm,column sep=.01cm, ampersand replacement = \&] {
\node (tpos) [box, align = center,
label=left:\( \mathbf{P'} \),
label=above:\( \mathbf{P} \)
] {True \\ Positive \\ (tp)};
\&
\node (fneg) [box, align = center,
label=above:\textbf{N},
label=above right:\textbf{total},
label=right:\( \mathrm{P}' \)] {False \\ Negative \\ (fn)};
\\
\node (fpos) [box, align = center,
label=left:\( \mathbf{N'} \),
label=below left:\textbf{total},
label=below:P] {False \\ Positive \\ (fp)};
\&
\node (tneg) [box, align = center,
label=right:\( \mathrm{N}' \),
label=below:N] {True \\ Negative \\ (tn)};
\\
};
\node [rotate=90,left=.01cm of conmat, text width=2.5cm,align=center,anchor=center] {\textbf{Actual Label}};
\node [above=.05cm of conmat] {\textbf{Predicted Label}};
}
\newcommand{\costmatrix}{
\matrix (costmat) [row sep=.01cm,column sep=.01cm, ampersand replacement = \&] {
\node (tpcos) [box, align = center] {$0$};
\&
\node (fncos) [box, align = center] {$1+\beta^2-t$};
\\
\node (fpcos) [box, align = center] {$t$};
\&
\node (tncos) [box, align = center] {$0$};
\\
};
}
答案1
我不会猜测要留出多少空间,而是会计算图形的宽度。我在下面的代码中使用 来执行此操作\settowidth...
。
正如 Pier Paolo 所提到的,您需要删除子图之间的空白行,否则它将像往常一样被解释为段落分隔符。按照文档,我还习惯于%
避免您可能不想要的虚假空格。
我不太清楚为什么你要将图片内容的代码与环境分开,但如果保持原样,你可以尝试这样的方法:
\documentclass[twoside,11pt]{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{caption}
\usepackage{subcaption}
\captionsetup[subfigure]{font=footnotesize}% this should be consistent throughout your document
\newcommand{\confmatrix}{%
\matrix (conmat) [row sep=.01cm,column sep=.01cm, ampersand replacement = \&] {
\node (tpos) [box, align = center,
label=left:\( \mathbf{P'} \),
label=above:\( \mathbf{P} \)
] {True \\ Positive \\ (tp)};
\&
\node (fneg) [box, align = center,
label=above:\textbf{N},
label=above right:\textbf{total},
label=right:\( \mathrm{P}' \)] {False \\ Negative \\ (fn)};
\\
\node (fpos) [box, align = center,
label=left:\( \mathbf{N'} \),
label=below left:\textbf{total},
label=below:P] {False \\ Positive \\ (fp)};
\&
\node (tneg) [box, align = center,
label=right:\( \mathrm{N}' \),
label=below:N] {True \\ Negative \\ (tn)};
\\
};
\node [rotate=90,left=.01cm of conmat, text width=2.5cm,align=center,anchor=center] {\textbf{Actual Label}};
\node [above=.05cm of conmat] {\textbf{Predicted Label}};
}
\newcommand{\costmatrix}{%
\matrix (costmat) [row sep=.01cm,column sep=.01cm, ampersand replacement = \&] {
\node (tpcos) [box, align = center] {$0$};
\&
\node (fncos) [box, align = center] {$1+\beta^2-t$};
\\
\node (fpcos) [box, align = center] {$t$};
\&
\node (tncos) [box, align = center] {$0$};
\\
};
}
\newlength\confmatrixwidth
\newlength\costmatrixwidth
\settowidth{\confmatrixwidth}{%
\begin{tikzpicture}[box/.style={draw,rectangle,text width=1.5cm,align=left}]
\confmatrix
\end{tikzpicture}}
\settowidth{\costmatrixwidth}{%
\begin{tikzpicture}[box/.style={draw,rectangle,minimum size =1cm,text width=1.5cm,align=left}]
\costmatrix
\end{tikzpicture}}
\begin{document}
\begin{figure}[t]
\centering
\subcaptionbox{Confution Matrix}[\confmatrixwidth]{%
\begin{tikzpicture}[box/.style={draw,rectangle,text width=1.5cm,align=left}]
\confmatrix
\end{tikzpicture}}%
\subcaptionbox{Cost Matrix}[\costmatrixwidth]{%
\begin{tikzpicture}[box/.style={draw,rectangle,minimum size =1cm,text width=1.5cm,align=left}]
\costmatrix
\end{tikzpicture}}%
\end{figure}
\end{document}
在这种情况下,子图在基线上对齐,其标题也对齐。我不清楚这是否是您想要做的,但这对于评论来说似乎太长了。