新答案

新答案

我想将一段句子居中(也可以左对齐和右对齐)之内一个段落,这在 Word 中可以通过 轻松完成[newline] + [center this line] + [newline]。然而,在 LaTeX 中,该centering命令会影响整个段落,而要仅影响组,它需要\par在该组的开头和结尾处有一个。同时,center环境增加了额外的垂直空间。我知道有一个centerline命令可以满足我的需求,但似乎没有任何与“左对齐”或“右对齐”相对应的命令。这是 MWE:

\documentclass{article}

\begin{document}
I want to center\\
"This piece of text"\\
within this paragraph.

I don't want to break the
{\par\centering paragraph like\par}
this.

How to achieve the effect\\
\centerline{without \texttt{centerline}}\\
or \begin{center}
the \texttt{center} environment?
\end{center}
\end{document}

在此处输入图片描述

答案1

新答案

正如@campa你不必定义自己的对应项,因为它们实际上已经包含在 LaTeX 中,名为\leftline\rightline。因此,你可以使用

\documentclass[]{article}

\begin{document}
\hsize=5cm % just so that line breaks are a bit earlier
This is some longish text that is long and needs me to centre some
material\\
\centerline{here is center}
Also I'd like to display some stuff to the left\\
\leftline{here is left}
and to put something on the right\\
\rightline{here is right.}
\end{document}

旧答案

为后人保留。

您可以通过定义来获取左对齐和右对齐的对应项。以下应该可以做到。请注意,只有宽度不超过单行的内容才能按预期工作,在\@@line发生自动换行。

如果你想知道\hss胶水是否H水平地s伸展或s根据要填充的指定空间进行必要的收缩(\@@line是一个单一的\hbox,应该与当前的一样宽\hsize,所以是文本的宽度)。

\documentclass[]{article}

\makeatletter
\newcommand\leftalignline[1]{\@@line{#1\hss}}
\newcommand\rightalignline[1]{\@@line{\hss#1}}
\makeatother

\begin{document}
\hsize=5cm % just so that line breaks are a bit earlier
This is some longish text that is long and needs me to centre some
material\\
\centerline{here is center}
Also I'd like to display some stuff to the left\\
\leftalignline{here is left}
and to put something on the right\\
\rightalignline{here is right.}
\end{document}

在此处输入图片描述

相关内容