2

This question is similar to what I'm looking for, but not quite the same: How do I right-align the last line of a justified paragraph? (And BTW I'm not writing in an RTL language.)

I'm writing a glossary in which I would like entries to look like this:

dog: a cute animal

warthog: an animal that is kind of
ugly-cute,  sort of like a dog but
                     not as cuddly

If I take the accepted answer from the question linked above and apply it in my LTR language, single-line entries are right-aligned, which is not what I want:

                dog: a cute animal

How can I accomplish this? Thanks!

Peter Wilson says: "I think that there is a problem here. You want the first lines to be justified but the last to be ragged right. But you also want the first line to be left justified if there are no further lines." That's correct. When you say that's a problem, do you mean that you think it's hard to accomplish that, or do you mean that you think there is something ill-defined or contradictory about what I'm trying to do?

The solution to this question seems very similar to what I want, but is for verse rather than prose, and ragged-right text: How to Left-align first line in a paragraph, right-align others

3
  • Welcome to TeX.SE.
    – Mico
    Jan 19 at 16:15
  • I think that there is a problem here. You want the first lines to be justified but the last to be ragged right. But you also want the first line to be left justified if there are no further lines. Jan 19 at 17:55
  • @PeterWilson: Can't that be handled by typsetting into a box and measure to see if takes up more than one line, and then adapting the formatting? Jan 19 at 21:11

1 Answer 1

1

Peter Grill's comment prompted me to piece together the following based on other answers on this site. This works for me in xelatex. Thanks, all!

\documentclass{article}

\usepackage{ifthen,hyphenat}

\begin{document}

% https://tex.stackexchange.com/a/406722
% For a single paragraph of text, determines how many lines it will take to typeset.
% #1=text, #2=width, #3=variable to store result in
\def\CalcNumber#1#2#3{%
  \setbox0\vbox{\hsize=#2 \noindent#1\par\xdef#3{\the\prevgraf}}}

% https://tex.stackexchange.com/a/43239/261851
% Typeset a paragraph so that the text is justified, and the final line is right-aligned:
\newcommand{\lastlinerightaligned}[1]{\leftskip=0pt plus .5fil\rightskip=0pt plus -.5fil\parfillskip=0pt plus .5fil{}#1}

\newcommand{\multiline}[1]{\lastlinerightaligned{\nohyphens{#1}}}
\newcommand{\oneline}[1]{#1}

% Typeset a paragraph so that if it's a single line, it's left-justified, but if it's multiple lines,
% it's justified except for the last line, which is right-justified.
\newcommand{\trailingrightpara}[1]{\CalcNumber{#1}{\columnwidth}{\NLines}\ifthenelse{\NLines>1}{\multiline{#1}}{\oneline{#1}}}

\twocolumn
\setlength{\parindent}{0pt}

\trailingrightpara{dog: a cute animal}

\trailingrightpara{warthog: an animal that is kind of ugly-cute, sort of like a dog but maybe not as cuddly}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.