新答案

新答案

我想將一段句子置中(也可以左對齊和右對齊)之內一個段落,可以在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

新答案

正如所指出的@坎帕您不必定義自己的對應項,因為它們實際上已經包含在 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伸展或shrinks 只是填滿指定空間所必需的(是一個應與 current 一樣寬的\@@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}

在此輸入影像描述

相關內容