5

I would like to have two consecutive lines of text where the shortest line is justified so that it ends at the end of the other, i.e. both lines should end at the end of the longest one. How can I do this?

Here is an example of what I would like. Imagine you have these two lines:

1) Lorem ipsum dolor sit amet,

2) consectetur adipisicing elit,

These lines are similar, but they do not end perfectly at the same point. I would like that the shortest line, in this case the second, is justified so that it ends at the same point of the first. I tried something with the environment tabular, but I'm not able to obtain the desired result. :(

  • What have you tried? If you're writing a document in LaTeX, this is the default behavior, unless I'm misunderstanding what you're asking ... ? Could you clarify and/or provide a minimal working example (MWE)? Also, welcome to TeX.SX! – Adam Liter Jan 23 '14 at 19:28
5

You can use \makebox with the optional s parameter (for stretch) to align the two lines.

Here is an example:

\documentclass{article}
\usepackage{calc}

\newcommand{\justifytwo}[2]{                                                    
\makebox[\maxof{\widthof{#1}}{\widthof{#2}}][s]{#1}\par                         
\makebox[\maxof{\widthof{#1}}{\widthof{#2}}][s]{#2}}

\begin{document}
\justifytwo{Lorem ipsum dolor sit amet et amet et amet,}
           {consectetur adipisicing elit,}
\end{document}

output

| improve this answer | |
6

Without explicit measuring it can be done in a tabular:

\documentclass{article}
\begin{document}
  \begin{tabular}{l}
    Lorem ipsum dolor sit amet,\hfilneg\null\\
    consectetur adipisicing elit,\hfilneg\null\\
  \end{tabular}
\end{document}

Result

Remarks:

  • \hfilneg cancels the \hfil that is inserted at the right side of the column with type l.

  • \null ensures that \hfilneg survives the internally added \unskip at the end of the cell contents.

plain TeX

In plain TeX it is even simpler, using \halign directly:

\noindent  
\halign{#\cr
  Lorem ipsum dolor sit amet,\cr
  consectetur adipisicing elit,\cr
}
\end

Result

| improve this answer | |
  • 1
    I learn't something new today, \hfilneg. Interesting! :-) – azetina Jan 23 '14 at 19:52

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.