# Centering certain sentences within a paragraph

I'd like to center (also left-align and right-align) a piece of sentence within one paragraph, which can be easily done in Word by [newline] + [center this line] + [newline]. In LaTeX, however, the centering command affects the whole paragraph, and to affect only groups, it requires a \par at the start and the end of that group. Meanwhile, the center environment adds additional vertical space. I know there's a centerline command that fits my need, but it seems there isn't any counterparts in terms of "left-align" or "right-align". Here's the 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}


• Could you adds some background information about the intended use cases of such an alignment? – leandriis Nov 13 '20 at 6:27

As pointed out by @campa you don't have to define your own counterparts, as they are actually already contained in LaTeX and called \leftline and \rightline. So you can use

\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}


Kept for posterity.

You can get the counterparts for left- and right-alignment by defining them. The following should do. Note that only contents not wider than a single line work as intended, inside of \@@line no automatic linebreaking will occur.

In case you're wondering \hss is a glue that horizontally stretches or shrinks just as necessary for the specified space being filled (\@@line is a single \hbox which should be as wide as the current \hsize, so the width of the text).

\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}


• Uhm, your \leftalignline and \rightalignline already exist in the LaTeX kernel with (almost) precisely those definitions. They are called \leftline and \rightline. – campa Nov 13 '20 at 14:07
• @campa oh, I didn't know these... Thank you! – Skillmon Nov 13 '20 at 14:22
• I think they are not really documented LaTeX macros. They belong to the bunch of plain TeX macros which have been more or less silently inherited. – campa Nov 13 '20 at 14:23
• @campa well, the same is true for \centerline, afaik, but I just didn't know that \leftline and \rightline did make it into LaTeX as well. – Skillmon Nov 13 '20 at 14:24
• That's why I said "silently" :-) – campa Nov 13 '20 at 14:25