关于 latex-figure-table-layout
为 LaTeX 文档作者提供图表位置与布局指导,涵盖浮动体安排和并排排版;不处理尺寸或缩放。
主要功能
- 01浮动体位置选项
- 02子图与子表布局
- 03图表并排布局
- 04跨栏浮动体
- 05排查浮动体位置问题
适用场景
- 01避免图像偏离正文引用处
- 02将图像排成 2×2 网格
- 03将图像与表格并排放置
- 04让表格横跨两栏
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dw-dengwei/ skills --skill latex-figure-table-layout安装后在 Claude Code 中输入/latex-figure-table-layout确认已加载。
附带文件references/float-positioning.mdreferences/subfigure-layout.md
详细介绍
LaTeX 图表布局
关于在 LaTeX 文档中定位图片和表格的专家指南。本技能可处理浮动体位置、子图布局、并排布局以及跨栏定位。
何时使用本技能
适用于以下情况:
- 调整图片/表格的位置参数(
[h]、[t]、[b]、[p]) - 将多个图片/表格排成子图
- 创建图片并排布局,或图片与表格并排的布局
- 修复浮动体定位问题(位置漂移、单独占页)
- 处理跨栏图片(
figure*、table*)
注意:本技能不会修改大小、栏宽或缩放比例,只处理定位。
浮动体位置参数
位置说明符
| 参数 | 含义 | 优先级 |
|---|---|---|
[h] | 此处(精确位置) | 最高 |
[t] | 页面顶部 | 中 |
[b] | 页面底部 | 中 |
[p] | 独立浮动页 | 最低 |
[!] | 覆盖 LaTeX 默认设置 | - |
推荐顺序
\begin{figure}[htbp] % Recommended: try here, then top, bottom, then float page
\end{figure}
强烈建议
\begin{figure}[!htbp] % Force LaTeX to respect your order
\end{figure}
最佳实践:大多数情况下使用 [htbp] 或 [!htbp]。避免单独使用 [h],这可能导致浮动体漂移。
使用 subcaption 的子图布局
基本子图(并排 2 张)
\usepackage{subcaption}
\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{0.45\textwidth}
\includegraphics[width=\textwidth]{figure1.pdf}
\caption{First figure}
\label{fig:first}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.45\textwidth}
\includegraphics[width=\filename]{figure2.pdf}
\caption{Second figure}
\label{fig:second}
\end{subfigure}
\caption{Main caption for both figures}
\label{fig:both}
\end{figure}
网格子图(2x2)
\begin{figure}[htbp]
\centering
\begin{subfigure}[t]{0.45\textwidth}
\includegraphics[width=\filename]{fig1.pdf}
\caption{Caption A}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.45\textwidth}
\includegraphics[width=\filename]{fig2.pdf}
\caption{Caption B}
\end{bmatrix}
\\
\begin{subfigure}[t]{0.45\textwidth}
\includegraphics[width=\filename]{fig3.pdf}
\caption{Caption C}
\end{subfigure}
\hfill
\begin{subfigure}[t]{0.45\textwidth}
\includegraphics[width=\filename]{fig4.pdf}
\caption{Caption D}
\end{subfigure}
\caption{Main caption for all four}
\label{fig:grid}
\end{figure}
子表格
\begin{table}[htbp]
\centering
\begin{subtable}[t]{0.45\textwidth}
\centering
\begin{tabular}{cc}
\toprule
A & B \\
\midrule
1 & 2 \\
\bottomrule
\end{tabular}
\caption{First table}
\label{tab:first}
\end{subtable}
\hfill
\begin{subtable}[t]{0.45\textwidth}
\centering
\begin{tabular}{cc}
\toprule
C & D \\
\midrule
3 & 4 \\
\bottomrule
\end{tabular}
\caption{Second table}
\label{tab:second}
\end{subtable}
\caption{Main caption}
\label{tab:both}
\end{table}
图片与表格并排布局
使用 minipage
\begin{figure}[htbp]
\centering
\begin{minipage}[c]{0.45\textwidth}
\centering
\includegraphics[width=\filename]{figure.pdf}
\caption{My figure}
\label{fig:minipage}
\end{minipage}
\hfill
\begin{minipage}[c]{0.45\textwidth}
\centering
\begin{tabular}{lc}
\toprule
Method & Acc \\
\midrule
A & 85.2 \\
B & 92.1 \\
\bottomrule
\end{tabular}
\caption{My table}
\label{tab:minipage}
\end{minipage}
\caption{Combined figure and table}
\label{fig+tab}
\end{figure}
使用 subcaptionbox
\begin{figure}[htbp]
\centering
\subcaptionbox{Figure label\label{fig:subcap}}
{\includegraphics[width=0.45\textwidth]{figure.pdf}}
\qquad
\subcaptionbox{Table label\label{tab:subcap}}
{\begin{tabular}{lc}
\toprule
A & B \\
\midrule
1 & 2 \\
\bottomrule
\end{tabular}}
\caption{Combined caption}
\label{fig:subcap:combined}
\end{figure}
跨栏图片(twocolumn)
跨栏图片
\begin{figure*}[htbp]
\centering
\includegraphics[width=0.8\textwidth]{wide-figure.pdf}
\caption{Wide figure spanning both columns}
\label{fig:wide}
\end{figure*}
跨栏表格
\begin{table*}[htbp]
\centering
\begin{tabular}{lcccc}
\toprule
Method & A & B & C & D \\
\midrule
Baseline & 85.2 & 86.1 & 87.3 & 88.0 \\
Ours & \textbf{92.1} & \textbf{91.5} & \textbf{93.2} & \textbf{94.1} \\
\bottomrule
\end{tabular}
\caption{Wide table spanning both columns}
\label{tab:wide}
\end{table*}
常见浮动体问题及解决方案
问题:图片漂移到文档末尾
原因:单独使用 [h],或图片过大
解决方案:
% Before (problematic)
\begin{figure}[h]
% After (recommended)
\begin{figure}[htbp]
% Or force placement
\begin{figure}[!htbp]
问题:图片单独占据整页
原因:[p] 触发浮动页,或图片过大
解决方案:
% Add stretch to push other content
\begin{figure}[htbp]
\centering
\includegraphics[width=0.7\textwidth]{figure.pdf}
\caption{Caption}
% Add space to encourage text to flow around
\end{figure}
问题:图片与正文中的引用分离
原因:浮动体定位规则
解决方案:
- 使用
[!htbp]让图片更早放置 - 在源文件中将图片放在更靠近其引用的位置
- 使用
placeins宏包中的\FloatBarrier:
\usepackage{placeins}
\section{My Section}
Text reference to \figurename~\ref{fig:myfig}.
\begin{figure}[htbp]
\centering
\includegraphics[width=0.8\textwidth]{figure.pdf}
\caption{My figure}
\label{fig:myfig}
\end{figure}
\FloatBarrier % Prevents floats from crossing this barrier
问题:子图未对齐
原因:高度不同,缺少对齐选项
解决方案:
% Use [t], [b], or [c] for vertical alignment
\begin{subfigure}[t]{0.45\textwidth} % [t] = top alignment
\begin{subfigure}[b]{0.45\textwidth} % [b] = bottom alignment
问题:子图下方的标题未居中
原因:缺少宽度设定
解决方案:
\begin{subfigure}[t]{0.45\textwidth}
\centering
\includegraphics[width=\filename]{fig.pdf}
\caption{Caption text}
\end{subfigure}
最佳实践
- 默认使用
[htbp]- 让 LaTeX 保持灵活性,同时遵循顺序 - 重要图片使用
[!htbp]- 覆盖部分浮动体限制 - 将浮动体放在引用附近 - 按源文件顺序排列,不必与最终位置一致
- 表格使用
booktabs- 呈现专业效果,但本技能不处理格式 - 使用
subcaption宏包 - 作为subfig的现代替代方案 - 考虑使用
\FloatBarrier- 在需要严格控制浮动体的位置使用
所需宏包
以上示例中引用了以下宏包:
\usepackage{subcaption} % For subfigure/subtable
\usepackage{booktabs} % For professional tables (not positioning)
\usepackage{placeins} % For \FloatBarrier
示例任务
| 请求 | 处理方法 |
|---|---|
| “Figure 1 总是移到下一页” | 将 [h] 改为 [htbp] 或 [!htbp] |
| “将 4 张图片排成 2x2 网格” | 使用子图,并用换行符 \\ 换行 |
| “将图片和表格并排放置” | 使用 minipage 或 subcaptionbox |
| “Figure 2 离 Section 2.2 很远” | 使用 [!htbp] 或添加 \FloatBarrier |
| “宽表格跨越两栏” | 使用 table* 替代 table |
评论
支持 Markdown