LaTeX中subfigure子图与标题无法居中对齐如何修复
LaTeX子图与对应标题居中错位修复方案
问题描述
使用LaTeX的subfigure环境排版子图时,出现子图图片和子标题无法居中对齐的错位问题,首次尝试网上的修复方案后问题仍未解决。
问题对应的原始代码如下:
\begin{figure}[H] \centering \begin{subfigure}{0.7\textwidth} \includegraphics[width=4cm,height=6cm]{outwithsel.PNG} \caption{Outliner} \label{fig:h1} \end{subfigure} \begin{subfigure}{\textwidth} \includegraphics[width=0.8\linewidth]{moveBtns.PNG} \caption{Buttons that move selected object in 4 different directions} \label{fig:h2} \end{subfigure} \caption{Kitchen is selected and can be moved to where desired} \end{figure}
问题根因
错位是两个设置疏漏共同导致的:
- 外层
figure环境中写的\centering仅能控制所有子图块整体在页面水平居中,不会渗透到subfigure环境内部作用于图片 - 代码里设置的
subfigure容器宽度大于内部图片的实际排版宽度,没有额外居中指令时,图片默认在subfigure区域左对齐,而子图标题是按照subfigure容器的全宽居中排版,二者基准位置不一致就会出现错位。
修复方法
在每个subfigure环境内部、\includegraphics语句前单独添加\centering指令,让子图内部的图片和标题都以subfigure容器宽度为基准居中即可。
修复后的完整可运行代码如下(导言区记得提前加载依赖宏包,标题内容已翻译为中文):
% 导言区加载必要宏包,不要和旧版subfigure宏包混用 \usepackage{graphicx} \usepackage{subcaption} \usepackage{float} \begin{figure}[H] \centering % 第一个子图 \begin{subfigure}{0.7\textwidth} \centering % 子图内部单独设置居中 \includegraphics[width=4cm,height=6cm]{outwithsel.PNG} \caption{大纲视图} \label{fig:h1} \end{subfigure} % 第二个子图 \begin{subfigure}{\textwidth} \centering % 子图内部单独设置居中 \includegraphics[width=0.8\linewidth]{moveBtns.PNG} \caption{控制选中对象向4个方向移动的按钮} \label{fig:h2} \end{subfigure} \caption{选中厨房模块后可将其移动到目标位置} \end{figure}
补充说明
- 目前排版子图推荐使用
subcaption宏包提供的subfigure环境,不要和已经废弃的旧版subfigure宏包同时加载,避免出现命令冲突 - 如果设置的subfigure容器宽度和内部图片宽度完全相等,不加内部
\centering也不会出现错位,但只要容器宽度大于图片宽度,就必须添加内部居中指令保证对齐 - 如果需要让多个子图并排而非纵向排列,调整每个subfigure的宽度到总和小于
\textwidth即可,内部的\centering指令同样生效
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

