General"Aligning" the last line in a paragraph

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

"Aligning" the last line in a paragraph

Post by meho_r »

By default, text in a paragraph is justified and the last line is "ragged right". Is there a way to make the last line "ragged left"? There is a need for this in some environments when I use right-to-left languages (often mixed with latin text), but for some reason it doesn't align the last line correctly.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

"Aligning" the last line in a paragraph

Post by Juanjo »

The following example may be helpful:

Code: Select all

\documentclass{article}
\usepackage{lipsum}

% This is the relevant environment
\newenvironment{LastLineToRight}%
  {\setlength{\parindent}{0pt}\setlength{\leftskip}{0pt plus 1fil}\setlength{\rightskip}{0pt plus -1fil}}{\par}

% \parskip increased to better see the end of each paragraph
\addtolength{\parskip}{3ex}

\begin{document}

% Two paragraphs with normal text
\lipsum[1-2]

% Paragraphs with the last line right justified.
\begin{LastLineToRight}
  \lipsum[1-20]
\end{LastLineToRight}

% Normal text again
\lipsum[1-2]
\end{document}
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

Re: "Aligning" the last line in a paragraph

Post by meho_r »

Thanks a lot :) What should be changed to make the last line centered? Or maybe it requires other technique?
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

"Aligning" the last line in a paragraph

Post by Juanjo »

You need only this other enironment:

Code: Select all

\newenvironment{LastLineCentered}%
  {\setlength{\parindent}{0pt}\setlength{\leftskip}{0pt plus 0.5fil}\setlength{\rightskip}{0pt plus -0.5fil}}{\par}
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

Re: "Aligning" the last line in a paragraph

Post by meho_r »

Of course :roll: Thank you very, very much, Juanjo
Post Reply