> For the complete documentation index, see [llms.txt](https://overleaf-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://overleaf-pro.ayaka.space/latex/zh-cn/ge-shi-hua/06-text-alignment.md).

# 文本对齐

## 引言

默认情况下，LaTeX 会将文本排版为两端对齐，但有时左对齐或“参差右对齐”（用于从右到左书写的语言）的文本可能更合适——例如窄栏中的文本。本文将介绍如何使用 LaTeX 内置功能和 ragged2e 宏包来更改文档部分或全部内容的文本对齐方式 `ragged2e`.

## 标准的 LaTeX 命令和环境

LaTeX 确实提供了用于更改文本排版对齐方式的内置命令：

* 参差右对齐（`\raggedright`)
* 参差左对齐（`\raggedleft`)
* 居中（`\centering`)

以及相应的环境：

* 参差右对齐（`flushleft` 环境）
* 参差左对齐（`flushright` 环境）
* 居中（`centering` 环境）

不过， [`ragged2e` 宏包](https://ctan.org/pkg/ragged2e?lang=en) 提供了一些改进，使其优于这些标准的 LaTeX 命令和环境。

## 使用 ragged2e 宏包

首先，下面是一个使用 `ragged2e` 宏包及其 `[document]` 选项来排版 *整个文档文本* 为参差右对齐（左对齐）。示例代码添加了以下行

```latex
\usepackage[document]{ragged2e}
```

到文档导言区：

```latex
\documentclass{article}
\usepackage[document]{ragged2e}

\begin{document}
\section{一级标题（section）}
你好，这里有一段没有意义的文字。这段文字应该显示打印文本在这里的样子。如果你读到这段文字，你不会获得任何信息。真的？这里没有任何信息吗？这段文字和一些胡言乱语之间有区别吗，完全没有！像这样的虚拟文本可以让你了解所选字体、字母的写法以及整体外观。
\end{document}
```

[在 Overleaf 中打开此 `ragged2e` Overleaf 中的示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+using+ragged2e+package\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Bdocument%5D%7Bragged2e%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BHeading+on+Level+1+%28section%29%7D%0AHello%2C+here+is+some+text+without+a+meaning.+This+text+should+show+what+a+printed+text+will+look+like+at+this+place.++If+you+read+this+text%2C+you+will+get+no+information.++Really%3F++Is+there+no+information%3F++Is+there+a+difference+between+this+text+and+some+nonsense+like+not+at+all%21++A+blind+text+like+this+gives+you+information+about+the+selected+font%2C+how+the+letters+are+written+and+an+impression+of+the+look.%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![Ragged2eOLV21.png](/files/8fba243d409b0f2021ac1c6479cad6b8ce9ec2c9)

## 比较 \raggedright（LaTeX）与 \RaggedRight（ragged2e）

LaTeX 命令 `\raggedright` 有时会产生看起来“过于参差不齐”的结果。 `ragged2e` 该宏包通过在某行过短时允许断字来解决这个问题，从而生成更均匀的参差文本边缘。下面的示例展示了通过 LaTeX 命令 `\raggedright` 生成的参差右对齐文本，以及作为比较，使用 `\RaggedRight` 命令排版的相同文本， `ragged2e`:

```latex
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}

\begin{document}
\setlength{\hsize}{0.9\hsize}% 强调效果

\subsection*{使用 \texttt{\string\raggedright}\\ 的左对齐示例（标准 \LaTeX{} 命令）}
\raggedright\blindtext[2]\par
\raggedright\blindtext[2]\par

\subsection*{使用 \texttt{\string\RaggedRight}\\ 的左对齐示例（\texttt{ragged2e} 命令）}
\RaggedRight\blindtext[2]\par
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Demonstrating+left-aligned+text\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Benglish%5D%7Bbabel%7D%0A%5Cusepackage%7Bragged2e%7D%0A%5Cusepackage%7Bblindtext%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Csetlength%7B%5Chsize%7D%7B0.9%5Chsize%7D%25+emphasize+effects%0A%0A%5Csubsection%2A%7BLeft-aligned+example+with+%0A%5Ctexttt%7B%5Cstring%5Craggedright%7D%5C%5C+%28standard+%5CLaTeX%7B%7D+command%29%7D%0A%5Craggedright%5Cblindtext%5B2%5D%5Cpar%0A%0A%5Csubsection%2A%7BLeft-aligned+example+with+%5Ctexttt%7B%5Cstring%5CRaggedRight%7D%5C%5C+%28%5Ctexttt%7Bragged2e%7D+command%29%7D%0A%5CRaggedRight%5Cblindtext%5B2%5D%5Cpar+%0A%5Cend%7Bdocument%7D)

![Leftaligncompare2.png](/files/2ed03e0a2c764bd011c103daea9dc13cf0cb95c6)

正如在 `ragged2e` [包文档](https://mirror.ox.ac.uk/sites/ctan.org/macros/latex/contrib/ragged2e/ragged2e.pdf), `ragged2e` 中所讨论的，提供了用于修改文本对齐方式的替代命令和环境，我们将看看其中的一些。

## 用于参差右对齐（左对齐）文本的环境

用于生成参差右对齐文本的默认 LaTeX 环境是 `flushleft`. 在 `ragged2e` 中的对应环境称为 `FlushLeft` （注意大小写）。下面的示例比较了这些环境的输出：

```latex
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% 强调效果

\subsection*{\texttt{flushleft} 环境：（标准 \LaTeX{}）}

\begin{flushleft}
\blindtext[1]
\end{flushleft}

\subsection*{\texttt{FlushLeft} 环境：（\texttt{ragged2e}）}

\begin{FlushLeft}
\blindtext[1]
\end{FlushLeft}

\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Demonstrating+left-aligned+text\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Benglish%5D%7Bbabel%7D%0A%5Cusepackage%7Bragged2e%7D%0A%5Cusepackage%7Bblindtext%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csetlength%7B%5Chsize%7D%7B0.9%5Chsize%7D%25+emphasize+effects%0A%0A%5Csubsection%2A%7B%5Ctexttt%7Bflushleft%7D+environment%3A+%28standard+%5CLaTeX%7B%7D%29%7D%0A%0A%5Cbegin%7Bflushleft%7D%0A%5Cblindtext%5B1%5D%0A%5Cend%7Bflushleft%7D%0A%0A%5Csubsection%2A%7B%5Ctexttt%7BFlushLeft%7D+environment%3A+%28%5Ctexttt%7Bragged2e%7D%29%7D%0A%0A%5Cbegin%7BFlushLeft%7D%0A%5Cblindtext%5B1%5D%0A%5Cend%7BFlushLeft%7D%0A%0A%5Cend%7Bdocument%7D)

![Flcompare.png](/files/3ff38a2251393105aab1a70c042db85c01ea9f98)

## 用于参差左对齐（右对齐）文本的环境

用于生成参差左对齐（右对齐）文本的默认 LaTeX 环境是 `flushright`. 在 `ragged2e` 中的对应环境称为 `FlushRight` （注意大小写）。下面的示例比较了这些环境的输出：

```latex
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% 强调效果

\subsection*{\texttt{flushright} 环境：（标准 \LaTeX{}）}

\begin{flushright}
\blindtext[1]
\end{flushright}

\subsection*{\texttt{FlushRight} 环境：（\texttt{ragged2e}）}

\begin{FlushRight}
\blindtext[1]
\end{FlushRight}

\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Demonstrating+right-aligned+text\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Benglish%5D%7Bbabel%7D%0A%5Cusepackage%7Bragged2e%7D%0A%5Cusepackage%7Bblindtext%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csetlength%7B%5Chsize%7D%7B0.9%5Chsize%7D%25+emphasize+effects%0A%0A%5Csubsection%2A%7B%5Ctexttt%7Bflushright%7D+environment%3A+%28standard+%5CLaTeX%7B%7D%29%7D%0A%0A%5Cbegin%7Bflushright%7D%0A%5Cblindtext%5B1%5D%0A%5Cend%7Bflushright%7D%0A%0A%5Csubsection%2A%7B%5Ctexttt%7BFlushRight%7D+environment%3A+%28%5Ctexttt%7Bragged2e%7D%29%7D%0A%0A%5Cbegin%7BFlushRight%7D%0A%5Cblindtext%5B1%5D%0A%5Cend%7BFlushRight%7D%0A%0A%5Cend%7Bdocument%7D)

![FRcompare.png](/files/f3ac7e5b2a548b355fd687a07adb78d5e7c68de4)

## 使文本居中的环境

用于生成居中文本的默认 LaTeX 环境是 `center`. 在 `ragged2e` 中的对应环境称为 `Center` （注意大小写）。下面的示例比较了这些环境的输出：

```latex
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% 强调效果

\subsection*{\texttt{center} 环境：（标准 \LaTeX{}）}

\begin{center}
\blindtext[1]
\end{center}

\subsection*{\texttt{Center} 环境：（\texttt{ragged2e}）}
\begin{Center}
\blindtext[1]
\end{Center}
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Demonstrating+right-aligned+text\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Benglish%5D%7Bbabel%7D%0A%5Cusepackage%7Bragged2e%7D%0A%5Cusepackage%7Bblindtext%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csetlength%7B%5Chsize%7D%7B0.9%5Chsize%7D%25+emphasize+effects%0A%0A%5Csubsection%2A%7B%5Ctexttt%7Bcenter%7D+environment%3A+%28standard+%5CLaTeX%7B%7D%29%7D%0A%0A%5Cbegin%7Bcenter%7D%0A%5Cblindtext%5B1%5D%0A%5Cend%7Bcenter%7D%0A%0A%5Csubsection%2A%7B%5Ctexttt%7BCenter%7D+environment%3A+%28%5Ctexttt%7Bragged2e%7D%29%7D%0A%5Cbegin%7BCenter%7D%0A%5Cblindtext%5B1%5D%0A%5Cend%7BCenter%7D%0A%5Cend%7Bdocument%7D)

![Centrecompare.png](/files/6a0e8861837415bb09f904a1e40050f059de8356)

## 完全两端对齐的文本

该宏包 `ragged2e` 提供了命令 `\justifying` 你可以像下面示例中那样使用它：

```latex
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% 强调效果

\Centering
\blindtext[1]

\vspace{5mm}
\justifying
\blindtext[1]
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Demonstrating+justification+with+ragged2e\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Benglish%5D%7Bbabel%7D%0A%5Cusepackage%7Bragged2e%7D%0A%5Cusepackage%7Bblindtext%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csetlength%7B%5Chsize%7D%7B0.9%5Chsize%7D%25+emphasize+effects%0A%0A%5CCentering%0A%5Cblindtext%5B1%5D%0A%0A%5Cvspace%7B5mm%7D%0A%5Cjustifying%0A%5Cblindtext%5B1%5D+%0A%5Cend%7Bdocument%7D)

![Justifyingdemo.png](/files/c36b74cfca045c607713411dc2c5629594e1e196)

## 参考指南

**文本对齐的环境和命令总结**

| 对齐方式   | 环境           | 切换命令           | ragged2e 环境  | ragged2e 切换命令  |
| ------ | ------------ | -------------- | ------------ | -------------- |
| 左      | `flushleft`  | `\raggedright` | `FlushLeft`  | `\RaggedRight` |
| 右      | `flushright` | `\raggedleft`  | `FlushRight` | `\RaggedLeft`  |
| 居中     | `center`     | `\centering`   | `Center`     | `\Centering`   |
| 完全两端对齐 |              |                | `justify`    | `\justifying`  |

## 进一步阅读

更多信息请参见：

* [段落和换行](/latex/zh-cn/latex-ji-chu/02-paragraphs-and-new-lines.md)
* [段落格式](/latex/zh-cn/ge-shi-hua/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [加粗、斜体和下划线](/latex/zh-cn/latex-ji-chu/03-bold-italics-and-underlining.md)
* [字体大小、字体族和样式](/latex/zh-cn/zi-ti/01-font-sizes-families-and-styles.md)
* [字体字形](/latex/zh-cn/zi-ti/02-font-typefaces.md)
* [使用 XeLaTeX 支持现代字体](/latex/zh-cn/zi-ti/03-xelatex.md)
* [换行与空白](/latex/zh-cn/ge-shi-hua/05-line-breaks-and-blank-spaces.md)
* [列表](/latex/zh-cn/latex-ji-chu/04-lists.md)
* [章节和小节](/latex/zh-cn/wen-dang-jie-gou/01-sections-and-chapters.md)
* [多栏](/latex/zh-cn/ge-shi-hua/09-multiple-columns.md)
* [单面和双面文档](/latex/zh-cn/ge-shi-hua/08-single-sided-and-double-sided-documents.md)
* [LaTeX2ε 不那么简短的介绍](http://www.ctan.org/tex-archive/info/lshort/)
* [ragged2e 宏包文档](http://mirror.hmc.edu/ctan/macros/latex/contrib/ms/ragged2e.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://overleaf-pro.ayaka.space/latex/zh-cn/ge-shi-hua/06-text-alignment.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.
