多列:左對齊並從左側填滿未使用的空間。 (奇數頁。)

多列:左對齊並從左側填滿未使用的空間。 (奇數頁。)

尊敬的社區,我有一個兩列文檔佈局,並且知道通常我可以在

  1. 將文字向左對齊,填充右側未使用的空間,以及
  2. 將文字向右對齊,填滿右側未使用的空間。

如何獲得將文字向左對齊的對齊方式,但根據最長的行將整個文字方塊盡可能放在列中的右側,從而填充左側未使用的空間?

這個解決方案應該會影響奇數頁上的整個列(應該可以集中定義),並且我事先不知道文字的寬度,這對於每列可能都是不同的。

非常感謝您的投入。

奇數頁上的必需行為

答案1

點擊圖像以更高解析度查看

編輯1:新增奇數/偶數頁檢查

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{lipsum}
\usepackage{array}
\usepackage{varwidth}
\usepackage{changepage}
\strictpagecheck
\def\maxwidth{13cm}
\newcolumntype{M}{>{\begin{varwidth}{\maxwidth}}l<{\end{varwidth}}}
\newenvironment{myenvi}
    {%
        \checkoddpage
        \ifoddpage
            \hfill\begin{tabular}{@{}M@{}}
        \fi
    }
    {%
        \checkoddpage
        \ifoddpage
            \end{tabular}\hspace{-3.5pt}
        \fi
    }

\setlength{\parskip}{1ex}
\begin{document}
\lipsum[1]

\begin{myenvi}
Oh, this is what I want!

I really want some lines like this

And this line is a bit too long, so it will be broken to some lines blah blah. And this line is a bit too long, so it will be broken to some lines. And this line is a bit too long, so it will be broken to some lines. And this line is a bit too long, so it will be broken to some lines.
\end{myenvi}

And some more normal lines

\begin{myenvi}
This is a short text for testing whose total width is clearly less than 10cm.

And this is an even shorter text.
\end{myenvi}

\lipsum[2-6]

\begin{myenvi}
This is the environment for check in an even page. It should be like normal text.

Yes, this is normal text!
\end{myenvi}

\lipsum[7]
\end{document}

第 1 頁(奇數):

在此輸入影像描述

第 2 頁(偶數):

在此輸入影像描述

原答案

定義新的tabular列類型並使用\hfill

\documentclass{article}
\usepackage{lipsum}
\usepackage{array}
\usepackage{varwidth}
\newcolumntype{M}{>{\begin{varwidth}{10cm}}l<{\end{varwidth}}}

\setlength{\parskip}{1ex}
\begin{document}
\lipsum[1]

\hfill\begin{tabular}{@{}M@{}}
Oh, this is what I want!

I really want some lines like this

And this line is a bit too long, so it will be broken to some lines blah blah. And this line is a bit too long, so it will be broken to some lines. And this line is a bit too long, so it will be broken to some lines. And this line is a bit too long, so it will be broken to some lines.
\end{tabular}

\lipsum[2]
\end{document}

在此輸入影像描述

相關內容