这个问题与我所寻找的类似,但不完全相同:如何使两端对齐的段落的最后一行右对齐?(顺便说一句,我不是用 RTL 语言编写的。)
我正在编写一个词汇表,我希望其中的条目如下所示:
dog: a cute animal
warthog: an animal that is kind of
ugly-cute, sort of like a dog but
not as cuddly
如果我从上面链接的问题中获取可接受的答案并将其应用于我的 LTR 语言,则单行条目将右对齐,这不是我想要的:
dog: a cute animal
我该如何实现这个?谢谢!
Peter Wilson 说:“我认为这里有一个问题。你希望第一行对齐,但最后一行右对齐。但如果没有其他行,你还希望第一行左对齐。”没错。当你说这是一个问题时,你是说你认为很难做到这一点,还是说你认为我试图做的事情有些不明确或矛盾?
这个问题的解决方案看起来与我想要的非常相似,但是针对的是诗歌而不是散文,以及不规则的文本:如何使段落首行左对齐,其他行右对齐
答案1
Peter Grill 的评论促使我根据本网站上的其他答案拼凑出以下内容。这在 xelatex 中对我有用。谢谢大家!
\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}