4

Hej guys. Our institute has some special format requirements, and I'm wondering if one of these can be automated by some clever LaTex wizardry:

A caption (also sections and subsections) is set ragged-right. However, the line-breaking is in such a way, that the subsequent line is longer than the preceding line (with the exception of the last line, if not possible to maintain). However, it is not intended to have the last line completely \textwith but rather find the first breakpoint which somehow makes the next line a bit longer.

So here are some examples:

This:

This is an example of a very long caption of a figure which might be too long to stay in one line so that it will automatically jump to a new line.

Should become this:

This is an example of a very long caption of a figure which might be too long to stay in one line so that it will automatically jump to a new line instead.

This is an example of a very long caption of a figure which might be too long to stay in one line so that it will automatically jump to a new line instead but now it is even more text.

I am really curious if this kind of formatting can be automated.

All the best, Bernte

  • 5
    Who thinks this stuff up? And why on earth do they think it's necessary to have as a requirement? – Alan Munn Feb 24 '19 at 17:24
  • 2
    Also, is the idea that the last line should be the natural width, and the preceding line shorter? This could lead to weird results, e.g. suppose the line is too long by exactly the width of the first word, you might end up with the first word on the first line and the rest on the second line. So how do you imagine determining the breakpoint? – Alan Munn Feb 24 '19 at 17:32
  • 2
    Apparently you're not the first to ask this. Ragged Right linebreaking, such that the last line is fullest, and the first most empty – Alan Munn Feb 24 '19 at 18:09
  • 2
    Bernte, since it's likely your question will be closed as a duplicate, the best thing for you to do would be to construct a minimal working example using the linked question and show that it doesn't work for your use case. Then it will be easy to reopen the question. – Alan Munn Feb 24 '19 at 20:31
  • 2
    I reopened (it was my answer in the linked question and I agree it doesn't answer this:-) – David Carlisle Feb 24 '19 at 21:54
7

this starts with the first line being half width and then getting longer... It does warn a lot in the log...

enter image description here

\documentclass{article}

\usepackage{shapepar}
\makeatletter

\def\zzshape#1{%
{0}%
{0}b{0}\\%
{0}t{0}{10}\\%
{#1}t{0}{20}\\%
{#1}e{20}%
}
\def\zzzshape{\zzshape\count@}

\def\zz#1{%
\count@=1 %
\loop
\setbox0\hbox{\Shapepar\zzzshape#1\par}%
\ifdim\wd0>\linewidth
\advance\count@\@ne
\repeat
\usebox0
\par}

\begin{document}

\raggedright
\setlength\parskip\bigskipamount

\zz{%
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.}

\zz{%
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.}

\zz{%
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.}

\zz{%
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.}

\end{document}

or with

\def\zzshape#1{%
{0}%
{0}b{0}\\%
{0}t{0}{17}\\%
{#1}t{0}{20}\\%
{#1}e{20}%
}

you get a more rectangular block:

enter image description here

  • Hello, thank you for your submission. As mentioned in the other posts, the idea is not to completely fill the last line but keep the caption in a block to the left. It will take me some time to understand your procedure but I will definitely comment on that in some days. Thank you once more. – Bernte Feb 25 '19 at 21:38
  • @Bernte but that is completely underspecified in that case, you could make every line one word long (or less) and it would be a tight block on the left. Surely you need some target width for line breaking? It does not have to be the whole page width, if you stick the above in a \begin{minipage}{3cm} all the lines will be at most that long. – David Carlisle Feb 25 '19 at 21:42
  • For the case of two line captions, it is kind of trying linebreaks and stick to exactly that one which makes the second line a little bit longer. Three captions is supposedly something iterative? Breaking every word would not work, as the following lines are not necessarily longer. – Bernte Feb 25 '19 at 21:43
  • 1
    yes that's what this is doing more or less (well I suppose you could use {18} rather than {10} for short paragraphs, so it's a steeper line then – David Carlisle Feb 25 '19 at 21:47
  • @Bernte I added a version with a longer first line – David Carlisle Feb 25 '19 at 21:51
2

This tries to create a triangle to replace the extra space on the last line. Not quite perfected, as I would like to avoid hyphenation and always wind up with a full line last. One can fix mistakes by careful use of \rlap. For example, exam\rlap{ple}.

This is probably incompatible with all the packages which modify \@makecaption.

\documentclass[12pt]{article}

\makeatletter
\long\def\@makecaption#1#2{\bgroup
  \vskip\abovecaptionskip
  \sbox\@tempboxa{#1: #2}%
  \ifdim \wd\@tempboxa < \hsize
    \global \@minipagefalse
    \hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
  \else
    \ifdim \wd\@tempboxa < 2\hsize
      \dimen0=\dimexpr \wd\@tempboxa - \hsize\relax
      \parshape=2 0pt \dimen0 0pt \hsize
      #1: #2\par
    \else
      \count1=\numexpr \wd\@tempboxa/\hsize\relax% number lines
      \dimen0=\dimexpr \count1\hsize - \wd\@tempboxa\relax% extra space      
      \ifdim \dimen0 < 0pt
        \advance \count1 by 1
        \advance \dimen0 by \hsize
      \fi
      \count2=\numexpr \count1-1\relax
      \dimen1=\dimexpr 2\dimen0/\count1/\count2\relax% increment
      \ifdim\dimen1 < 2em\relax
        \count2=\count1
        \advance\count1 by 1
        \advance\dimen0 by \hsize
        \dimen1=\dimexpr 2\dimen0/\count1/\count2\relax
      \fi
      \dimen2=\dimexpr \hsize - \count2\dimen1\relax
      \edef\@makecaptionparshapeargs{\the\count1}% \parshape arguments
      \loop\ifnum\count1>0\relax
        \edef\@makecaptionparshapeargs{\@makecaptionparshapeargs\space0pt \the\dimen2}%
        \advance\count1 by -1
        \advance\dimen2 by \dimen1
      \repeat
      \parshape=\@makecaptionparshapeargs
      #1: #2\par
    \fi
  \fi
  \vskip\belowcaptionskip
\egroup}
\makeatother

\begin{document} 
\begin{figure}
\caption{Short cpation.}

\caption{This is an example of a very long caption of a figure which might be
too long to stay in one line.}

\caption{This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.}

\caption{This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.}

\caption{This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.
This is an example of a very long caption of a figure which might be
too long to stay in one line so that it will automatically jump to a
new line.}
\end{figure}
\end{document}

demo

  • Hello. Thank you for your contribution. One thing I forgot to mention is, that the idea is not to have the last line completely full, but rather keep the caption kind of tight in a block at the left. I feel this might be possible by adjusting your code. It will take me some time to understand the code, but I will definitely come back to this. Thank you once more! – Bernte Feb 25 '19 at 21:37

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.