使用 KOMA-Script 和 ragged2e 制作对齐字幕

使用 KOMA-Script 和 ragged2e 制作对齐字幕

我刚刚偶然发现了一些现象。我正在使用 KOMA-Script 书scrbook并加载了包ragged2e。通常,我会使用选项加载该包newcommands,以便能够使用命令\raggedright并进行连字符连接。

实际文档中有一些浮动图像,它们\caption也包含 -command。有些甚至包含besidescaptionKOMA-Script 类的 -environment。

有一次,我偶然发现一个页面上有两个浮动元素。一个使用 normal \caption,一个使用besidescaption-environment 。在后者中,标题文本是对齐的,但在前一个浮动元素中,标题文本左侧齐平,右侧边缘参差不齐。

以下是 MWE:

\documentclass[10pt]{scrbook}

%% This works as expected
% \usepackage{ragged2e}
%% This gives strange results
\usepackage[newcommands]{ragged2e}

\usepackage{graphicx}

\begin{document}
\chapter{We need justification}
\label{cha:justification}


\begin{figure}
  \centering
  \includegraphics{example-image-a}
  \caption[short caption]{Some random text, just ordinary text, which
    should be some words and lines long.  This text should be
    presented in justified form, as is all the rest of the text of
    this book.  Be careful.  Have a closer look.  This text is flush
    left instead of justified.  What on earth is going on?}
  \label{fig:flushleft}
\end{figure}

\begin{figure}
  \begin{captionbeside}
    [Another short story]{This is a smaller image, where the caption
      is not given below, but beside the image.  Thanks to Markus
      Kohm, who did a great job with this often requested feature.
      This caption text is presented in justified form, as you would
      and should expect.}
    \includegraphics[width=5cm]{example-image-b}
  \end{captionbeside}

\end{figure}
\end{document}

结果如下。首先是显示结果,出乎意料:

错误结果

它应该看起来像这样:

在此处输入图片描述

我觉得这是一个错误?

答案1

\setcaptionalignment{J}加载包后使用ragged2e。对齐J方式为完全对齐ragged2e(参见KOMA-Script 文档)。

\documentclass[10pt]{scrbook}
\usepackage[newcommands]{ragged2e}
\setcaptionalignment{J}% <- added
\usepackage{graphicx}
\begin{document}
\chapter{We need justification}
\label{cha:justification}
\begin{figure}
  \centering
  \includegraphics{example-image-a}
  \caption[short caption]{Some random text, just ordinary text, which
    should be some words and lines long.  This text should be
    presented in justified form, as is all the rest of the text of
    this book.  Be careful.  Have a closer look.  This text is flush
    left instead of justified.  What on earth is going on?}
  \label{fig:flushleft}
\end{figure}
\begin{figure}
  \begin{captionbeside}
    [Another short story]{This is a smaller image, where the caption
      is not given below, but beside the image.  Thanks to Markus
      Kohm, who did a great job with this often requested feature.
      This caption text is presented in justified form, as you would
      and should expect.}
    \includegraphics[width=5cm]{example-image-b}
  \end{captionbeside}
\end{figure}
\end{document}

在此处输入图片描述

您也可以替换\centering\LaTeXcentering

\documentclass[10pt]{scrbook}
\usepackage[newcommands]{ragged2e}
\usepackage{graphicx}
\begin{document}
\chapter{We need justification}
\label{cha:justification}
\begin{figure}
  \LaTeXcentering% <- changed
  \includegraphics{example-image-a}
  \caption[short caption]{Some random text, just ordinary text, which
    should be some words and lines long.  This text should be
    presented in justified form, as is all the rest of the text of
    this book.  Be careful.  Have a closer look.  This text is flush
    left instead of justified.  What on earth is going on?}
  \label{fig:flushleft}
\end{figure}
\begin{figure}
  \begin{captionbeside}
    [Another short story]{This is a smaller image, where the caption
      is not given below, but beside the image.  Thanks to Markus
      Kohm, who did a great job with this often requested feature.
      This caption text is presented in justified form, as you would
      and should expect.}
    \includegraphics[width=5cm]{example-image-b}
  \end{captionbeside}
\end{figure}
\end{document}

使用\LaTeXcentering也适用于标准类:

\documentclass[10pt]{book}% standard class
\usepackage[newcommands]{ragged2e}
\usepackage{graphicx}
\begin{document}
\chapter{We need justification}
\label{cha:justification}
\begin{figure}
  \LaTeXcentering% <- changed
  \includegraphics{example-image-a}
  \caption[short caption]{Some random text, just ordinary text, which
    should be some words and lines long.  This text should be
    presented in justified form, as is all the rest of the text of
    this book.  Be careful.  Have a closer look.  This text is flush
    left instead of justified.  What on earth is going on?}
  \label{fig:flushleft}
\end{figure}
\end{document}

答案2

ragged2e 手册,第 6 页,解释了选项用newcommands替代;如果您在未启用该选项的情况下手动执行此操作(即使用而不是不加载),则会产生相同的输出。\centering\Centering\Centering\centeringnewcommands

一个快速的解决方法是\centering在加载之前简单地保存旧的\usepackage[newcommands]{ragged2e},然后\centeringfigure环境中使用它:

\let\oldcentering\centering
\usepackage[newcommands]{ragged2e}

编辑:这实际上正是\LaTeXcentering(见@esdd 的回答)是,参见ragged2e手册第 7.10 节(以下摘录)

截屏


或者,按照ragged2e手册第 3 页,您可以\justifying在标题环境内使用来重新打开对齐,如\caption{\justifying Lorem ipsum}

这两种修复方法都应该可以避免Underfull \hbox (badness 10000)出现您可能收到的警告(至少我是这样)。


编辑:有关使用 KOMA-Script 的更简单的解决方案,请参阅@esdd 的回答。或者,这应该适用于 KOMA-Script 和常规课程,您可以使用caption包裹:

\usepackage[newcommands]{ragged2e}

% see page 25 of the `caption` manual:
% http://mirrors.ctan.org/macros/latex/contrib/caption/caption-eng.pdf
\usepackage{caption}
\DeclareCaptionJustification{justified}{\justifying}
\captionsetup{justification=justified}

MWE 用于scrbook班级:

\documentclass[10pt]{scrbook}

\usepackage[newcommands]{ragged2e}

\usepackage{caption}
\DeclareCaptionJustification{justified}{\justifying}
\captionsetup{justification=justified}

\usepackage{graphicx}

\begin{document}

\begin{figure}
  \centering
  \includegraphics{example-image-a}
  \caption{Some random text, just ordinary text, which should
    be some words and lines long.  This text should be
    presented in justified form, as is all the rest of the text of
    this book.  Be careful.  Have a closer look.  This text is flush
    left instead of justified.  What on earth is going on?}
  \label{fig:flushleft}
\end{figure}

\end{document}

截屏


命令的范围\Centering也包括命令的内容\caption,嗯,这可能是一个错误。我不知道。

附言:“我们需要辩解”这句话很搞笑,我很喜欢。

相关内容