两行各有 5 个子图,分别为 3 和 2,且第二行不居中

两行各有 5 个子图,分别为 3 和 2,且第二行不居中

\begin{figure}[h] % figure will span both columns in article
\centering
    \setkeys{Gin}{width=\linewidth} % common settings of images widths
\begin{subfigure}{0.3\linewidth}
    \includegraphics{./Figures/a.png}
    \caption{cap1}
    \label{1}
\end{subfigure}%
    \hfill
    \begin{subfigure}{0.3\linewidth}
    \includegraphics{./Figures/s.png}
    \caption{cap2}
    \label{2}
\end{subfigure}%
    \hfill
    \begin{subfigure}{0.3\linewidth}
    \includegraphics{./Figures/d.png}
    \caption{cap3}
    \label{3}
\end{subfigure}%
\hfill
\centering
    \begin{subfigure}{0.3\linewidth}
    \includegraphics{./Figures/f.png}
    \caption{cap4}
    \label{4}
\end{subfigure}%
    \hfill
    \begin{subfigure}{0.3\linewidth}
    \includegraphics{./Figures/g.png}
    \caption{cap5}
    \label{5}
\end{subfigure}%
    \caption{figure}
    \label{total}
\end{figure}

我想要每个子图的标题和标签,此代码的唯一缺陷是第二行中的两个子图没有居中,它们占据了很大的空间并填满了整行,谢谢!

答案1

一些建议:

  • 消除两个都 \centering说明。第一个没有做任何有用的事情,第二个与你在帖子标题中所说的想要实现的目标背道而驰,,“第二行未居中”。

  • 更换最后的 \hfill指令带有\hspace{0.05\textwidth}%%符号很重要。为什么0.05\textwidth?在第 1 行中,三个图占据了0.9\textwidth;这0.1\textwidth为两个图间空白留下了间隙。一半0.1\textwidth0.05\textwidth

  • \hfill用空白行和指令替换倒数第二条指令\bigskip

在此处输入图片描述

\documentclass[demo]{article} % remove 'demo' option in final document
\usepackage{graphicx,subcaption}
\begin{document}

\begin{figure}[h] % figure will span both columns in article % really?

%%\centering % <-- no need to use \centering
\setkeys{Gin}{width=\linewidth} % common settings of images widths

\begin{subfigure}{0.3\linewidth}
    \includegraphics{./Figures/a.png}
    \caption{cap1}
    \label{1}
\end{subfigure}%
    \hfill
    \begin{subfigure}{0.3\linewidth}
    \includegraphics{./Figures/s.png}
    \caption{cap2}
    \label{2}
\end{subfigure}%
    \hfill
    \begin{subfigure}{0.3\linewidth}
    \includegraphics{./Figures/d.png}
    \caption{cap3}
    \label{3}
\end{subfigure}

\bigskip % <-- leave a blank line (to cause a line break) and '\bigskip'
%% \centering  % <-- this instruction is counterproductive
\begin{subfigure}{0.3\linewidth}% <-- the "%" symbol is needed
    \includegraphics{./Figures/f.png}
    \caption{cap4}
    \label{4}
\end{subfigure}%
\hspace{0.05\textwidth}% <-- instead of '\hfill'
\begin{subfigure}{0.3\linewidth}
    \includegraphics{./Figures/g.png}
    \caption{cap5}
    \label{5}
\end{subfigure}

\caption{A figure with five subfigures}
\label{fig:total}

\end{figure}

\end{document}

相关内容