多列:左对齐并从左侧填充未使用的空间。(奇数页。)

多列:左对齐并从左侧填充未使用的空间。(奇数页。)

尊敬的社区,我有一个两列文档布局,并且知道通常我可以选择

  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}

在此处输入图片描述

相关内容