12

using \raggedright roughly speaking will result in linebreaking when ever there is not enough space on the line to put the next word. This in turn results in all lines, except the last, being mostly full.

I want to achieve the opposite. I wish for the last line to be full, and the first line to be partially empty.

In the example below I have achieved this for the second heading using manual line breaking.

\documentclass{article}
\begin{document}

    \newcommand{\myheading}[1]{{\textbf{\raggedright #1}}\\}
\begin{minipage}{3cm}
    \myheading{This is the first heading}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi.\\
    \bigskip

    \myheading{This is\\ the last heading}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi.
\end{minipage}
\end{document}

example

12

enter image description here

\documentclass{article}
\begin{document}

    \newcommand{\myheading}[1]{{\bfseries\raggedright\parfillskip=-\rightskip #1\par}}% why not define as a heading?
\begin{minipage}{3cm}
    \myheading{This is the first heading}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi.
\end{minipage}
\end{document}

The above ensures that on the last line there is no stretch glue added so it is always fully justified. This may not be desired in all cases, specifically if the paragraph is single line you probably want a flush left setting and also if the last line is short you may prefer it to be ragged rather than over-stretch to justify. You can add \hspace*{\fill} at the end of the text in these cases, but below is a more complicated version but sets the heading in a box so that it can remove the last line of the paragraph and in the case the paragraph had just one line, or the natural length of the last line is less than 80% of the line width, then the last line is set at its natural width, flush left and the justified last line is discarded.

\documentclass{article}
\begin{document}

    \newcommand{\myheading}[1]{%
\xdef\tmpdepth{\prevdepth\the\prevdepth}%
\setbox0\vbox{{%
\tmpdepth
\bfseries\raggedright\parfillskip=-\rightskip #1\par
\xdef\tmpdepth{\prevdepth\the\prevdepth}%
\setbox0\lastbox
\setbox2\hbox{\unhcopy0}%
\nointerlineskip
\ifnum\prevgraf=1 \box2 \else
\ifdim\wd2<.8\wd0 \box2 \else
\box0
\fi\fi
}}%
\unvbox0
\tmpdepth
}% why not define as a heading?
\begin{minipage}{3cm}
    \myheading{This igs}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit,


    \myheading{This is longer igs}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit,


    \myheading{This is much longer and covers several lines igs}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit,

    \myheading{This is much much longer and covers several lines igs}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit,

    \myheading{This is much much longer ad covers sevezzzzzl lines gs}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit,

\end{minipage}
\end{document}

enter image description here

  • 3
    +1 Sometimes I am amazed at how easy it is to do the weirdest things with LaTeX! – CarLaTeX Mar 7 '18 at 10:35
  • @CarLaTeX Actually, this very thing (breaking paragraphs into lines, using the elegant box-glue-penalty model) is, in some sense, the core/heart of TeX, so it makes sense that this would be easy. :-) – ShreevatsaR Mar 7 '18 at 16:59
  • Awesome. Can you explain how it works a bit more? – Lyndon White Mar 8 '18 at 1:32
  • Doesn't quiet work right, if the heading is only on one line. try: \myheading{short head} It is not ragged, it is kinda justified (via interword spacing) – Lyndon White Mar 8 '18 at 7:22
  • I am messing around with it, \hfill doesn't help at all. (I'm trying ragged2e but that doesn't work ideally for \myheading{medium len head} which it breaks unnecessarily – Lyndon White Mar 8 '18 at 8:04

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.