wrapfigure 环境中的 \raggedright

wrapfigure 环境中的 \raggedright

由于标准的左右对齐,wrapfigure 中的较小宽度有时会使标题的间距变得难看。如何让 \raggedright 在标题中发挥作用?通过将 \raggedright 放在标题内,我得到了所需的结果,但排版会提前停止。示例:

\documentclass{book}
\usepackage{wrapfig}
\begin{document}
\begin{wrapfigure}{R}{.3\textwidth}
\caption{\raggedright The wordlengths in this caption are the problem.}
\vskip20pt
(figure)
\vskip20pt
\end{wrapfigure}
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
Now is the time for all good men to  come to the aid of their country.
\end{document}

在这种情况下,在回答问题和一些我不认识的代码时按四次 RETURN 键后,生成的文档会显示所需的 raggedright 标题。如何在没有这些额外的 RETURN 键的情况下获得此最终结果?

答案1

你的 MWE 工作正常,所以不清楚你的问题是什么?肯定不是由单词“wordlength”引起的。

在此处输入图片描述

我认为上述结果并不令人满意。我会在您的 MWE 中做以下更改:

  • 使用caption包定义字幕样式,
  • 切勿在标题文本中定义标题样式。请在文档前言中或在\captionsetup标题之前进行此操作。
  • 借助etoolbox包,您可以为选定的环境定义标题样式。例如,在您的案例中
\usepackage{etoolbox}           % for environment hooks
\AtBeginEnvironment{wrapfigure}{% for caption style in wrapfigure
        \captionsetup{font=small,
                      justification=RaggedRight}
                                }
  • 删除\setlength{\intextsep}{0pt}}前后多余的垂直空间wrapfigure。可以按照与标题样式类似的方式将其添加到序言中:
\BeforeBeginEnvironment{wrapfigure}{% remove vertical space around wrapfigue
        \setlength{\intextsep}{0pt}}
  • 对于狭窄环境中的字幕,例如wrapfigure,较长单词的智能连字功能可以实现更好的字幕文本格式
  • 如果默认连字模式不包含某些长单词的建议,则在序言中添加这些单词的列表是明智的。例如,在您的案例中
\hyphenation{word-lengths} 
  • microtype通过使用包可以实现文本(以及标题)中更好的单词间距

考虑上述建议后,您的 MWE 是:

\documentclass{book}
\usepackage{lipsum}% For dummy text. Don't use in a real document

\hyphenation{word-lengths}      % here collect long words which not hyphenate

\usepackage{wrapfig}
% added packages needed in this MWE
\usepackage{microtype}
\usepackage{graphicx}
\usepackage{ragged2e}           % used in definition od caption style (at least) in wrapfigure
\usepackage{caption}            % for defining caption style
\usepackage{etoolbox}           % for environment hooks
\BeforeBeginEnvironment{wrapfigure}{% remove vertical space around wrapfigue
        \setlength{\intextsep}{0pt}}
\AtBeginEnvironment{wrapfigure}{% for caption style in wrapfigure
        \captionsetup{font=small,
                      justification=RaggedRight}
                                }

\begin{document}
    \begin{wrapfigure}{r}{0.3\textwidth}
        \caption{The wordlengths in this caption are the problem.}
        \label{fig:a}
        \includegraphics[width=\linewidth]{example-image-duck}
    \end{wrapfigure}
\lipsum[44]
\end{document}

在此处输入图片描述

这个结果你能接受吗?

相关内容