> For the complete documentation index, see [llms.txt](https://ayakaleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayakaleaf-pro.ayaka.space/latex/zh-cn/tu-xing-he-biao-ge/02-positioning-images-and-tables.md).

# 图片和表格的位置设置

LaTeX 是一种编辑工具，负责处理格式，因此你只需关心文档内容；不过，有时仍然需要更好地控制浮动元素。本文将解释如何在 LaTeX 文档中放置图片和表格。

## 引言

图片和表格的默认对齐方式设置为 *左侧*

```latex
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra
sollicitudin.

\includegraphics[width=0.5\textwidth]{overleaf-logo}

Praesent imperdiet mi nec
ante. Donec ullamcorper, felis non sodales commodo, lectus velit
ultrices augue, a dignissim nibh lectus placerat pede.
 Vivamus nunc nunc, molestie ut, ultricies
vel, semper in, velit. Ut porttitor.
```

![PositioningTnIEX1OverleafV2.png](/files/68891205587d141b9ad50afc20381bfec0d7cc02)

这是一个简单示例，如需了解在 LaTeX 文件中插入图片的此方法及其他方法，请参见文章 [插入图片](/latex/zh-cn/geng-duo-zhu-ti/27-inserting-images.md).

&#x20;[在 Overleaf 中打开示例](https://www.overleaf.com/project/new/template/19663?id=66518064\&templateName=Example+of+positioning+tables+and+figures\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## 图片定位

### 基本定位

要将图片的默认对齐方式从 *左侧* 或 *右侧*更改为

```latex
\usepackage[export]{adjustbox}
```

添加到文件导言区，然后在导入图片的语句中使用一个附加选项

```latex
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisis sem. Nullam nec mi et neque
pharetra sollicitudin.

\includegraphics[width=0.5\textwidth, right]{overleaf-logo}

Praesent imperdiet mi necante. Donec ullamcorper, felis
non sodales commodo, lectus velit ultrices augue,
a dignissim nibh lectus placerat pede. Vivamus nunc nunc,
molestie ut, ultriciesvel, semper in, velit. Ut porttitor.
```

![PositioningTnIEx2OLV2.png](/files/32d0c2286ed90da3624f9a969e7f347be6db21f6)

该宏包 **adjustbox** 使 `\includegraphics` 命令中的一个附加选项可用，在示例中图片对齐到 `右侧`。可用的值有： *左侧*, *右侧*, *center*, *外侧* 和 *内侧*，后两个用于双面文档。

&#x20;[在 Overleaf 中打开示例](https://www.overleaf.com/project/new/template/19663?id=66518064\&templateName=Example+of+positioning+tables+and+figures\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

### figure 环境

figure 环境（见 [插入图片](/latex/zh-cn/geng-duo-zhu-ti/27-inserting-images.md)）旨在提供自动定位。

```latex
Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Duis fringilla tristique neque. Sed interdum libero ut metus.
Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet
ante lobortis sollicitudin.

\begin{figure}[h]
\includegraphics[width=0.5\textwidth, inner]{overleaf-logo}
\caption{Caption}
\label{fig:figure2}
\end{figure}
```

![PositioningTnIEx3OLV2.png](/files/7fe28dc45945dffee51017e155a0c765982c1db2)

该环境使用在方括号中传入的定位参数，它可以取以下值：

| 参数 | 位置                                                  |
| -- | --------------------------------------------------- |
| h  | 放置浮动体 *此处*，即， *大致* 在它出现在源文本中的同一位置（不过，不 *精确地* 在那个位置） |
| t  | 位置于 *顶部* 的页面。                                       |
| b  | 位置于 *底部* 的页面。                                       |
| p  | 放到一个专门的 *page* 仅供浮动体使用的页面上。                         |
| !  | 覆盖 LaTeX 用于确定“合适”浮动体位置的内部参数。                        |
| H  | 将浮动体精确放置在 LaTeX 代码中的该位置。需要 `float` 包。这在某种程度上等同于 h!。 |

你可以在参数中放入多个值，例如，如果你写 `[ht]` LaTeX 会尝试将图形放置在 **此处**，但如果不可能（空间可能不足），那么该图形将出现在 **顶部** 页面的。建议使用多个定位参数，以防出现意外结果。

&#x20;[在 Overleaf 中打开示例](https://www.overleaf.com/project/new/template/19663?id=66518064\&templateName=Example+of+positioning+tables+and+figures\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

### 一张图中放置多张图片

可以在一个图形中插入多张图片，每张都有自己的引用和标签

```latex
Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque...

\begin{figure}[h]

\begin{subfigure}{0.5\textwidth}
\includegraphics[width=0.9\linewidth, height=6cm]{overleaf-logo}
\caption{Caption1}
\label{fig:subim1}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=0.9\linewidth, height=6cm]{mesh}
\caption{Caption 2}
\label{fig:subim2}
\end{subfigure}

\caption{Caption for this figure with two images}
\label{fig:image2}
\end{figure}

Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.
```

![PositioningTnIEx4OLV2.png](/files/47f8dc9beeec3bf74e3782f8e4d234699fd911c0)

首先，你必须导入宏包 **subcaption** 通过在导言区添加

```latex
\usepackage{subcaption}
```

然后你可以使用环境 `\subfigure` 它接受一个参数，即图形的宽度。该环境必须在 `figure` 环境内部使用，且可以为每个 *subfigure*.

&#x20;[在 Overleaf 中打开示例](https://www.overleaf.com/project/new/template/19663?id=66518064\&templateName=Example+of+positioning+tables+and+figures\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

### 让文本环绕图形

该宏包 **wrapfig** 提供了一个有用的功能，文本可以环绕图片排版。

```latex
Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Duis fringilla tristique neque. Sed interdum
libero ut metus. Pellentesque placerat.

\begin{wrapfigure}{l}{0.25\textwidth}
\includegraphics[width=0.9\linewidth]{overleaf-logo}
\caption{Caption1}
\label{fig:wrapfig}
\end{wrapfigure}

Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Duis fringilla tristique neque. Sed interdum
```

![PositioningTnIEx5OLV2.png](/files/6d7f7e7e11bb4d3c764f760d319f9151754a1688)

首先导入宏包 *wrapfig* 通过添加

```latex
\usepackage{wrapfig}
```

到导言区。

之后你可以使用环境 `wrapfig`，它接受两个通过花括号传入的参数：对齐方式，可以是 *l*, *r*, *c*, *i* 或 *或*；这些字母分别表示左、右、居中、内侧和外侧（后两个用于双面文档）。第二个参数是图形容器的宽度，在示例中其值为文本宽度的 0.25。有关可用长度单位的列表，请参见 [参考指南](#reference-guide) 。

&#x20;[在 Overleaf 中打开示例](https://www.overleaf.com/project/new/template/19663?id=66518064\&templateName=Example+of+positioning+tables+and+figures\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## 表格定位

表格定位选项与图形可用的选项类似。

### 基本定位

tabular 环境的默认位置是 *居中*.

```latex
Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque.
Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet
ante lobortis sollicitudin.

\arrayrulecolor[HTML]{DB5800}
\begin{tabular}{ |s|p{2cm}|p{2cm}|  }
\hline
\rowcolor{lightgray} \multicolumn{3}{|c|}{Country List} \\
\hline
国家名称或地区名称& ISO ALPHA 2 Code &ISO ALPHA 3 \\
\hline
阿富汗 & AF &AFG \\
\rowcolor{gray}
Aland Islands & AX  & ALA \\
Albania    &AL & ALB \\
Algeria   &DZ & DZA \\
美属萨摩亚 & AS & ASM \\
Andorra & AD & \cellcolor[HTML]{AA0044} AND \\
安哥拉 & AO & AGO \\
\hline
\end{tabular}

Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Duis fringilla tristique neque. Sed interdum libero ut metus.
Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit
amet ante lobortis sollicitudin.
```

&#x20;[在 Overleaf 中打开这段代码片段](https://www.overleaf.com/docs?engine=\&snip_name=Default+position+of+tabular\&snip=%5Cdocumentclass%7Barticle%7D%0A%25Table-related+commands%0A%5Cusepackage%7Barray%7D%0A%5Cusepackage%5Btable%5D%7Bxcolor%7D%0A%5Csetlength%7B%5Carrayrulewidth%7D%7B1mm%7D%0A%5Csetlength%7B%5Ctabcolsep%7D%7B18pt%7D%0A%5Crenewcommand%7B%5Carraystretch%7D%7B1.5%7D%0A%5Cnewcolumntype%7Bs%7D%7B%3E%7B%5Ccolumncolor%5BHTML%5D%7BAAACED%7D%7D+p%7B3cm%7D%7D%0A%0A%5Cbegin%7Bdocument%7D%0APraesent+in+sapien.+Lorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+elit.+Duis+fringilla+tristique+neque.+%0ASed+interdum+libero+ut+metus.+Pellentesque+placerat.+Nam+rutrum+augue+a+leo.+Morbi+sed+elit+sit+amet+%0Aante+lobortis+sollicitudin.%0A%0A%5Carrayrulecolor%5BHTML%5D%7BDB5800%7D%0A%5Cbegin%7Btabular%7D%7B+%7Cs%7Cp%7B2cm%7D%7Cp%7B2cm%7D%7C++%7D%0A%5Chline%0A%5Crowcolor%7Blightgray%7D+%5Cmulticolumn%7B3%7D%7B%7Cc%7C%7D%7BCountry+List%7D+%5C%5C%0A%5Chline%0ACountry+Name+or+Area+Name%26+ISO+ALPHA+2+Code+%26ISO+ALPHA+3+%5C%5C%0A%5Chline%0AAfghanistan+%26+AF+%26AFG+%5C%5C%0A%5Crowcolor%7Bgray%7D%0AAland+Islands+%26+AX++%26+ALA+%5C%5C%0AAlbania++++%26AL+%26+ALB+%5C%5C%0AAlgeria+++%26DZ+%26+DZA+%5C%5C%0AAmerican+Samoa+%26+AS+%26+ASM+%5C%5C%0AAndorra+%26+AD+%26+%5Ccellcolor%5BHTML%5D%7BAA0044%7D+AND+%5C%5C%0AAngola+%26+AO+%26+AGO+%5C%5C%0A%5Chline%0A%5Cend%7Btabular%7D%0A%0APraesent+in+sapien.+Lorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+%0Aelit.+Duis+fringilla+tristique+neque.+Sed+interdum+libero+ut+metus.+%0APellentesque+placerat.+Nam+rutrum+augue+a+leo.+Morbi+sed+elit+sit+%0Aamet+ante+lobortis+sollicitudin.%0A%5Cend%7Bdocument%7D)

下图显示了上面代码片段的结果：

![PositioningTnIEx6.png](/files/e3a98baf059e9dbb8e84dc15c84fce299b315882)

你还可以  [在 Overleaf 中打开一个完整的项目示例。](https://www.overleaf.com/project/new/template/19663?id=66518064\&templateName=Example+of+positioning+tables+and+figures\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

要了解如何创建表格，请参见 [表格](/latex/zh-cn/tu-xing-he-biao-ge/01-tables.md) 文章。

### table 环境

table 环境旨在自动定位表格，使其在文档流中显示得更加自然。

```latex
Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Duis fringilla tristique neque. Sed interdum
libero ut metus. Pellentesque placerat. Nam rutrum augue a leo.
Morbi sed elit sit amet ante lobortis sollicitudin.

\begin{table}[ht]
\arrayrulecolor[HTML]{DB5800}
\centering
\begin{tabular}{ |s|p{2cm}|p{2cm}|  }
\hline
\rowcolor{lightgray} \multicolumn{3}{|c|}{Country List} \\
\hline
国家名称或地区名称& ISO ALPHA 2 Code &ISO ALPHA 3 \\
\hline
阿富汗 & AF &AFG \\
\rowcolor{gray}
Aland Islands & AX  & ALA \\
Albania    &AL & ALB \\
Algeria   &DZ & DZA \\
美属萨摩亚 & AS & ASM \\
Andorra & AD & \cellcolor[HTML]{AA0044} AND \\
安哥拉 & AO & AGO \\
\hline
\end{tabular}
\caption{Table inside a floating element}
\label{table:ta}
\end{table}

Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Duis fringilla tristique neque. Sed interdum
libero ut metus. Pellentesque placerat. Nam rutrum augue a leo.
Morbi sed elit sit amet ante lobortis sollicitudin.
```

&#x20;[在 Overleaf 中打开这段代码片段](https://www.overleaf.com/docs?engine=\&snip_name=Default+position+of+tabular\&snip=%5Cdocumentclass%7Barticle%7D%0A%25Table-related+commands%0A%5Cusepackage%7Barray%7D%0A%5Cusepackage%5Btable%5D%7Bxcolor%7D%0A%5Csetlength%7B%5Carrayrulewidth%7D%7B1mm%7D%0A%5Csetlength%7B%5Ctabcolsep%7D%7B18pt%7D%0A%5Crenewcommand%7B%5Carraystretch%7D%7B1.5%7D%0A%5Cnewcolumntype%7Bs%7D%7B%3E%7B%5Ccolumncolor%5BHTML%5D%7BAAACED%7D%7D+p%7B3cm%7D%7D%0A%0A%5Cbegin%7Bdocument%7D%0APraesent+in+sapien.+Lorem+ipsum+dolor+sit+amet%2C+consectetuer+%0Aadipiscing+elit.+Duis+fringilla+tristique+neque.+Sed+interdum+%0Alibero+ut+metus.+Pellentesque+placerat.+Nam+rutrum+augue+a+leo.%0AMorbi+sed+elit+sit+amet+ante+lobortis+sollicitudin.%0A%0A%5Cbegin%7Btable%7D%5Bht%5D%0A%5Carrayrulecolor%5BHTML%5D%7BDB5800%7D%0A%5Ccentering%0A%5Cbegin%7Btabular%7D%7B+%7Cs%7Cp%7B2cm%7D%7Cp%7B2cm%7D%7C++%7D%0A%5Chline%0A%5Crowcolor%7Blightgray%7D+%5Cmulticolumn%7B3%7D%7B%7Cc%7C%7D%7BCountry+List%7D+%5C%5C%0A%5Chline%0ACountry+Name+or+Area+Name%26+ISO+ALPHA+2+Code+%26ISO+ALPHA+3+%5C%5C%0A%5Chline%0AAfghanistan+%26+AF+%26AFG+%5C%5C%0A%5Crowcolor%7Bgray%7D%0AAland+Islands+%26+AX++%26+ALA+%5C%5C%0AAlbania++++%26AL+%26+ALB+%5C%5C%0AAlgeria+++%26DZ+%26+DZA+%5C%5C%0AAmerican+Samoa+%26+AS+%26+ASM+%5C%5C%0AAndorra+%26+AD+%26+%5Ccellcolor%5BHTML%5D%7BAA0044%7D+AND+%5C%5C%0AAngola+%26+AO+%26+AGO+%5C%5C%0A%5Chline%0A%5Cend%7Btabular%7D%0A%5Ccaption%7BTable+inside+a+floating+element%7D%0A%5Clabel%7Btable%3Ata%7D%0A%5Cend%7Btable%7D%0A%0APraesent+in+sapien.+Lorem+ipsum+dolor+sit+amet%2C+consectetuer+%0Aadipiscing+elit.+Duis+fringilla+tristique+neque.+Sed+interdum+%0Alibero+ut+metus.+Pellentesque+placerat.+Nam+rutrum+augue+a+leo.+%0AMorbi+sed+elit+sit+amet+ante+lobortis+sollicitudin.%0A%5Cend%7Bdocument%7D)

下图显示了 Overleaf 链接生成的输出：

![PositioningTnIEx7.png](/files/0498d396b72a3ea970fe308a8461ac6d34b7ff4b)

可以在 **table** 环境中传入一个方括号里的位置参数。该参数可以取以下值：

| 参数 | 位置                                                  |
| -- | --------------------------------------------------- |
| h  | 放置浮动体 *此处*，即， *大致* 在它出现在源文本中的同一位置（不过，不 *精确地* 在那个位置） |
| t  | 位置于 *顶部* 的页面。                                       |
| b  | 位置于 *底部* 的页面。                                       |
| p  | 放到一个专门的 *page* 仅供浮动体使用的页面上。                         |
| !  | 覆盖 LaTeX 用于确定“合适”浮动体位置的内部参数。                        |
| H  | 将浮动体精确放置在 LaTeX 代码中的该位置。需要 `float` 包。这在某种程度上等同于 h!。 |

你可以在参数中设置多个值，例如，如果你写 `[ht]` LaTeX 会尝试将表格放置在 **此处**，但如果不可能（空间可能不足），那么表格将出现在 **顶部** 页面的。建议使用多个定位参数，以防出现意外结果。

还要注意命令 `\centering`。这会将表格在其容器中的对齐方式改为 *居中* 而不是默认的 *左侧*.

&#x20;[在 Overleaf 中打开示例](https://www.overleaf.com/project/new/template/19663?id=66518064\&templateName=Example+of+positioning+tables+and+figures\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

### 让文本环绕表格

如果你的表格没有占满所有可用空间，并且你想在它旁边或前面放置文本，可以使用宏包 `wrapfig`.

首先，导入宏包

```latex
\usepackage{wrapfig}
```

然后你可以使用环境 `wraptable` 它接受两个参数：第一个是对齐方式，可以是 `l`, `r`, `c`, `i` 或 `或` 分别表示左、右、居中、内侧和外侧。第二个是表格容器的宽度，请注意后一个参数必须与表格宽度相同，否则对齐可能不正确。

```latex
Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Duis fringilla tristique neque. Sed interdum
libero ut metus. Pellentesque placerat. Nam rutrum augue a leo.
Morbi sed elit sit amet ante lobortis sollicitudin.

\begin{wraptable}{r}{8cm}
\arrayrulecolor[HTML]{DB5800}
\centering
\begin{tabular}{ |s|p{2cm}|  }
\hline
\rowcolor{lightgray} \multicolumn{2}{|c|}{Country List} \\
\hline
Country Name or Area Name& ISO ALPHA 2 Code \\
\hline
Afghanistan & AF \\
\rowcolor{gray}
Aland Islands & AX \\
Albania    &AL  \\
Algeria   &DZ \\
American Samoa & AS \\
Andorra & \cellcolor[HTML]{AA0044} AD   \\
Angola & AO \\
\hline
\end{tabular}
\caption{Table inside a wraptable}
\label{table:ta2}
\end{wraptable}

Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Duis fringilla tristique neque. Sed interdum
libero ut metus. Pellentesque placerat. Nam rutrum augue a leo.
Morbi sed elit sit amet ante lobortis sollicitudin...
```

&#x20;打开此 `wrapfig` Overleaf 中的代码片段

下图显示了 Overleaf 链接生成的输出：

![PositioningTnIEx8OLV2.png](/files/ef03e229e2464b62665555cb4a86c0893250ec97)

## 参考指南

**LaTeX 的单位和长度**

| 缩写             | 定义                         |
| -------------- | -------------------------- |
| pt             | point 是默认长度单位。约为 0.3515 毫米 |
| mm             | 一毫米                        |
| cm             | 一厘米                        |
| .bib 文件中插入一个 % | 一英寸                        |
| ex             | 一个……的高度 **x** 在当前字体中       |
| em             | 一个……的宽度 **m** 在当前字体中       |
| \columnsep     | 列间距                        |
| \columnwidth   | 列宽                         |
| \linewidth     | 当前环境中行宽                    |
| \paperwidth    | 页面宽度                       |
| \paperheight   | 页面高度                       |
| \textwidth     | 文本宽度                       |
| \textheight    | 文本高度                       |
| \unitlength    | ……中的长度单位 *picture* 环境中。    |

## 进一步阅读

更多信息请参见：

* [表格](/latex/zh-cn/tu-xing-he-biao-ge/01-tables.md)
* [插入图片](/latex/zh-cn/geng-duo-zhu-ti/27-inserting-images.md)
* [表格和图形列表](/latex/zh-cn/tu-xing-he-biao-ge/03-lists-of-tables-and-figures.md)
* [LaTeX 中的长度](/latex/zh-cn/ge-shi-hua/01-lengths-in-latex.md)
* [**floatrow** 用于高级管理浮动元素（表格和图形）标题的宏包](http://tug.ctan.org/tex-archive/macros/latex/contrib/floatrow/floatrow.pdf)
* [**sidecap** 侧边图标题的宏包文档](ftp://ctan.tug.org/tex-archive/macros/latex/contrib/sidecap/sidecap.pdf)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ayakaleaf-pro.ayaka.space/latex/zh-cn/tu-xing-he-biao-ge/02-positioning-images-and-tables.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
