![强制换行,这样每一行都会变得更长](https://linux22.com/image/391121/%E5%BC%BA%E5%88%B6%E6%8D%A2%E8%A1%8C%EF%BC%8C%E8%BF%99%E6%A0%B7%E6%AF%8F%E4%B8%80%E8%A1%8C%E9%83%BD%E4%BC%9A%E5%8F%98%E5%BE%97%E6%9B%B4%E9%95%BF.png)
大家好。我们的研究所有一些特殊的格式要求,我想知道其中是否可以通过一些巧妙的 LaTex 魔法来实现自动化:
标题(以及章节和小节)设置为右侧不对齐。但是,换行方式使得后续行比前一行长(最后一行除外,如果无法保持)。但是,并非旨在使最后一行完全与 \textwith 对齐,而是找到第一个断点,这会以某种方式使下一行稍长一些。
以下是一些例子:
这:
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 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.
我真的很好奇这种格式化是否可以自动化。
祝一切顺利,伯恩特
答案1
一开始第一行只有一半宽度,然后越来越长...它确实在日志中发出了很多警告...
\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}
或
\def\zzshape#1{%
{0}%
{0}b{0}\\%
{0}t{0}{17}\\%
{#1}t{0}{20}\\%
{#1}e{20}%
}
你会得到一个更加矩形的块:
答案2
这会尝试创建一个三角形来替换最后一行的额外空格。还不够完善,因为我想避免使用连字符,并且总是以完整的一行结束。可以通过谨慎使用 来修复错误\rlap
。例如,exam\rlap{ple}
。
这可能与所有修改的包不兼容\@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}