自己定义的标题样式与 \captionsetup 设置的标题样式不同

自己定义的标题样式与 \captionsetup 设置的标题样式不同

问题 1:我认为下面两个例子的排版应该是一样的,因为 by 设置的样式内容\DeclareCaptionStyle和 by 设置的样式内容是一样的\captionsetup。但是实际排版(请看附图)显示这两个标题的水平位置不一样。这是为什么呢?

问题2:虽然我设置了format=hang,但是字幕文本的第二行却失去了悬挂效果。如何获取字幕hanged & centering

代码:

\documentclass{article}
\usepackage{caption,pifont}

\DeclareCaptionStyle{tt}{
    format=hang,justification=raggedright
}


\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Example 1:
\captionsetup[table]{
  format=hang,justification=raggedright
}
\begin{table}[htb]
  \centering
  \caption{This is a table\\second line}
  A table
\end{table}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\vskip3cm
Example 2:
\captionsetup[table]{style=tt}
\begin{table}[htb]
  \centering
  \caption{This is a table\\second line}
  A table
\end{table}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}

在此处输入图片描述

答案1

我无法真正改进 frabjous ide(除了修复错误)。我确实弄清楚了如何使用 来实现它\DeclareCaptionTextFormat

\documentclass{article}
\usepackage{caption}

\DeclareCaptionTextFormat{myform}{\def\BODY{#1}\begin{tabular}[t]{@{}c@{}}\BODY\end{tabular}}

\begin{document}
\begin{table}
\captionsetup{textformat=myform}
\caption[unformattted title]{This is a table\\second line}
\end{table}
\end{document}

此版本使用\leftskip\vtop

\documentclass{article}
\usepackage{caption}

\DeclareCaptionFormat{myform}{\vtop{%
  \sbox0{\textbf{#1#2}}%
  \centering
  \advance\leftskip by \wd0
  \hspace{-\wd0}\usebox0#3}\par}

\begin{document}

\begin{table}
\captionsetup{format=myform}
\caption[unformatted title]{This is a table\\second line}
\end{table}
\end{document}

相关内容