如何制作包含 4 个矩阵子图的 1 个图形?

如何制作包含 4 个矩阵子图的 1 个图形?

像标题一样,但下面是我想在 LaTeX 文档中恢复的图像。 在此处输入图片描述

这是我尝试过的:

\begin{figure}[h!]
\begin{subfigure}[b]{0.2\textwidth}
    \begin{tabular}{|c|}
        \hline
        3 \\
        \hline
        1  \\
        \hline
        4 \\
        \hline
    \end{tabular}
    \newline
    \vspace*{0.5cm}
    \newline
    \begin{tabular}{|c|}
        \hline
        2 \\
        \hline
        7  \\
        \hline
        2 \\
        \hline
    \end{tabular}
    \caption{Input}
 \end{subfigure}
 \hfill
 \begin{subfigure}[b]{0.2\textwidth}
    \centering
    \begin{tabular}{|c|}
        \hline
        5 \\
        \hline
        8  \\
        \hline
        6 \\
        \hline
    \end{tabular}
    \caption{Addition}
 \end{subfigure}
 \hfill
 \begin{subfigure}[b]{0.2\textwidth}
    \centering
    \begin{tabular}{|c|}
        \hline
        6 \\
        \hline
        7  \\
        \hline
        8 \\
        \hline
    \end{tabular}
    \caption{Multiplication}
 \end{subfigure}
 \hfill
 \begin{subfigure}[b]{0.2\textwidth}
    \centering
    \begin{tabular}{|c|}
        \hline
        3 \\
        \hline
        1  \\
        \hline
        4 \\
        \hline
        2 \\
        \hline
        7  \\
        \hline
        2 \\
        \hline
    \end{tabular}
    \caption{Concatenation}
\end{subfigure}
\caption{xyz}
\label{fig:x}
\end{figure}

但这不是我想要的。我在居中第一个子图时也遇到了问题。

如能得到任何帮助我将非常感激。

答案1

您可以测量最高子图的高度和子标题的宽度。 的语法subcaption与 的语法相同minipage,因此您可以指定高度。

\documentclass{article}
\usepackage{subcaption}

\newlength{\subfigureht}
\newlength{\subfigurewd}

\begin{document}

\begin{figure}[!htp]

\settoheight{\subfigureht}{%
  \begin{minipage}[b]{\textwidth}
    \begin{tabular}{|c|}
        \hline
        3 \\
        \hline
        1  \\
        \hline
        4 \\
        \hline
    \end{tabular}
    \endgraf\medskip
    \begin{tabular}{|c|}
        \hline
        2 \\
        \hline
        7  \\
        \hline
        2 \\
        \hline
    \end{tabular}
    \endgraf\vspace{0pt}
  \end{minipage}%
}
\addtolength{\subfigureht}{2\baselineskip}

\settowidth{\subfigurewd}{(m) Input}%
\begin{subfigure}[b][\subfigureht][s]{\subfigurewd}
    \centering
    \vfill
    \begin{tabular}{|c|}
        \hline
        3 \\
        \hline
        1  \\
        \hline
        4 \\
        \hline
    \end{tabular}

    \medskip

    \begin{tabular}{|c|}
        \hline
        2 \\
        \hline
        7  \\
        \hline
        2 \\
        \hline
    \end{tabular}
    \vfill
    \caption{Input}
 \end{subfigure}\hfill
 \settowidth{\subfigurewd}{(m) Addition}%
 \begin{subfigure}[b][\subfigureht][s]{\subfigurewd}
    \centering
    \vfill
    \begin{tabular}{|c|}
        \hline
        5 \\
        \hline
        8  \\
        \hline
        6 \\
        \hline
    \end{tabular}
    \vfill
    \caption{Addition}
 \end{subfigure}\hfill
 \settowidth{\subfigurewd}{(m) Multiplication}%
 \begin{subfigure}[b][\subfigureht][s]{\subfigurewd}
    \centering
    \vfill
    \begin{tabular}{|c|}
        \hline
        6 \\
        \hline
        7  \\
        \hline
        8 \\
        \hline
    \end{tabular}
    \vfill
    \caption{Multiplication}
 \end{subfigure}\hfill
 \settowidth{\subfigurewd}{(m) Concatenation}%
 \begin{subfigure}[b][\subfigureht][s]{\subfigurewd}
    \centering
    \vfill
    \begin{tabular}{|c|}
        \hline
        3 \\
        \hline
        1  \\
        \hline
        4 \\
        \hline
        2 \\
        \hline
        7  \\
        \hline
        2 \\
        \hline
    \end{tabular}
    \vfill
    \caption{Concatenation}
\end{subfigure}
\caption{xyz}
\label{fig:x}
\end{figure}

\end{document}

在此处输入图片描述

答案2

试试这个代码。子图的解释在主图的标题中完成。(并替换了 s \newline

埃

\documentclass[12pt,a4paper]{article}

\usepackage{subcaption}
\usepackage{graphicx}

 \captionsetup{labelformat= empty, textfont=small} 
 \captionsetup[sub]{textfont=small} 

\usepackage{showframe}% only to show margins

\begin{document}

\noindent\begin{figure}[h!] 
    \begin{subfigure}[c]{0.2\textwidth}
            \centering  \bfseries
        \begin{tabular}{|c|}
            \hline
            3 \\
            \hline
            1  \\
            \hline
            4 \\
            \hline
        \end{tabular}\\
        \vspace*{0.5cm}
        \begin{tabular}{|c|}
            \hline
            2 \\
            \hline
            7  \\
            \hline
            2 \\
            \hline
        \end{tabular}
    \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[c]{0.2\textwidth}
        \centering  \bfseries
        \begin{tabular}{|c|}
            \hline
            5 \\
            \hline
            8  \\
            \hline
            6 \\
            \hline
        \end{tabular}
    \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[c]{0.2\textwidth}
        \centering  \bfseries
        \begin{tabular}{|c|}
            \hline
            6 \\
            \hline
            7  \\
            \hline
            8 \\
            \hline
        \end{tabular}
        \caption{}
    \end{subfigure} 
    \hfill
    \begin{subfigure}[c]{0.2\textwidth}
        \centering  \bfseries
        \begin{tabular}{|c|}
            \hline
            3 \\
            \hline
            1  \\
            \hline
            4 \\
            \hline
            2 \\
            \hline
            7  \\
            \hline
            2 \\
            \hline
        \end{tabular}
        \caption{}  
    \end{subfigure}
    \caption{(a) input \, (b) `+' addition \,  (c) `\times' multiplication \, (d) `\oplus' concatenation}
    \label{fig:x}
\end{figure}
\end{document}

如果您想将符号添加到标题中,例如 \caption{\times}在子图中。

F

答案3

这使用 tabularx 分别对齐表格和子标题。标题以 开始和结束\par,因此您需要某种段落列类型,这也会均匀分布它们(而不是基于标题宽度)。

仅第一个表格需求放入保存箱中。此外,varwidth 不能很好地与 配合使用\newline

\documentclass{article}
\usepackage{subcaption}
\usepackage{tabularx}
\usepackage{varwidth}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{figure}[ht]
\sbox0{\begin{varwidth}[c]{\linewidth}% special case
    \begin{tabular}{|c|}
        \hline
        3 \\
        \hline
        1  \\
        \hline
        4 \\
        \hline
    \end{tabular}
    \vskip 0.5cm
    \begin{tabular}{|c|}
        \hline
        2 \\
        \hline
        7  \\
        \hline
        2 \\
        \hline
    \end{tabular}
\end{varwidth}}%
%
\sbox1{\begin{tabular}{|c|}
        \hline
        5 \\
        \hline
        8  \\
        \hline
        6 \\
        \hline
    \end{tabular}}%
%
\sbox2{\begin{tabular}{|c|}
        \hline
        6 \\
        \hline
        7  \\
        \hline
        8 \\
        \hline
    \end{tabular}}%
%
\sbox3{\begin{tabular}{|c|}
        \hline
        3 \\
        \hline
        1  \\
        \hline
        4 \\
        \hline
        2 \\
        \hline
        7  \\
        \hline
        2 \\
        \hline
    \end{tabular}}%
% now format the whole thing
\begin{tabularx}{\linewidth}{CCCC}
    \usebox0 &
    \usebox1 &
    \usebox2 &
    \usebox3 \\
    \subcaption{Input} & \subcaption{Addition} & \subcaption{Multiplication} & \subcaption{Concatenation}
\end{tabularx}
\caption{xyz}
\label{fig:x}
\end{figure}

\end{document}

演示


此版本用于\raisebox使 LaTeX 认为所有图像都是相同的大小(\ht0\dp0)。

\documentclass{article}
\usepackage{subcaption}
\usepackage{varwidth}

\begin{document}

\begin{figure}[ht]
\sbox0{\begin{varwidth}[c]{\linewidth}% special case
    \begin{tabular}{|c|}
        \hline
        3 \\
        \hline
        1  \\
        \hline
        4 \\
        \hline
    \end{tabular}
    \vskip 0.5cm
    \begin{tabular}{|c|}
        \hline
        2 \\
        \hline
        7  \\
        \hline
        2 \\
        \hline
    \end{tabular}
\end{varwidth}}%
%
\sbox1{\begin{tabular}{|c|}
        \hline
        5 \\
        \hline
        8  \\
        \hline
        6 \\
        \hline
    \end{tabular}}%
%
\sbox2{\begin{tabular}{|c|}
        \hline
        6 \\
        \hline
        7  \\
        \hline
        8 \\
        \hline
    \end{tabular}}%
%
\sbox3{\begin{tabular}{|c|}
        \hline
        3 \\
        \hline
        1  \\
        \hline
        4 \\
        \hline
        2 \\
        \hline
        7  \\
        \hline
        2 \\
        \hline
    \end{tabular}}%
% now format the whole thing
\begin{subfigure}{0.25\linewidth}
    \centering
    \usebox0
    \caption{Input}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
    \centering
    \raisebox{0pt}[\ht0][\dp0]{\usebox1}
    \caption{Addition}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
    \centering
    \raisebox{0pt}[\ht0][\dp0]{\usebox2}
    \caption{Multiplication}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
    \centering
    \raisebox{0pt}[\ht0][\dp0]{\usebox3}
    \caption{Concatenation}
\end{subfigure}
\caption{xyz}
\label{fig:x}
\end{figure}

\end{document}

相关内容