1

Esteemed community, I have a two column document layout, and am aware that normally I can choose between

  1. justifying text to the left, which fills unused space from the right, and
  2. justifying text to the right, which fills unused space from the right.

How can I get alignment that justifies text to the left, but places the entire box of text as far to the right in the column as possible according to the longest line, thus filling unused space from the left?

The solution should affect whole columns on odd-numbered pages (should be definable centrally), and I don't know the width of the text beforehand, this may be different for every column.

Many thanks for your input.

Requited behaviour on odd page

1
  • Hi Ranavir, providing a minimum example code to work with could help people better understand your problem and give you a better answer :) Commented Mar 8, 2019 at 10:43

1 Answer 1

1

Click on the images to see it in a better resolution

Edit 1: Odd/even page check added

\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}

Page 1 (odd):

enter image description here

Page 2 (even):

enter image description here

Original answer

Define a new tabular column type and use \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}

enter image description here

7
  • Thanks, that's pretty cool, but to make the solution practical for my case, do you think there would there be a way to define this automatically for columns in odd pages only? Also, how can I affect the length of the space at which the long line will be broken?
    – ranavir
    Commented Mar 8, 2019 at 11:33
  • @ranavir For the first question: This is only a \hfill and an environment combined, so you can add this anywhere. If you want this on an odd page, add this on that page!
    – user156344
    Commented Mar 8, 2019 at 11:36
  • @ranavir For the second question: Look at the \newcolumntype. You see a number 10cm. That is the width of the text. You can always change it, of course.
    – user156344
    Commented Mar 8, 2019 at 11:37
  • Ok, I get it. There are unfortunately two reasons why this is not practical. 1) I don't know the width number beforehand, I want it to be set to the longest line in the box. (Imagine this is e.g. a book of songs or poems with short lines.) 2) I don't know in advance which page is odd and which is even. The publication is sorted and rolling, so inserting a new page would mean I have to go and change all the subsequent pages manually.
    – ranavir
    Commented Mar 8, 2019 at 11:40
  • @ranavir (1) Have you checked my code? 10cm is only the maximum width, not the width. Delete the And this line is a bit too long, so it ... and see what happens.
    – user156344
    Commented Mar 8, 2019 at 11:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .