\captionbox 和 \parbox 中的对齐

\captionbox 和 \parbox 中的对齐

软件包中caption有一个宏\captionbox,该宏已在subcaption软件包中记录。该宏实际上是一个。请考虑以下取自软件包\parbox的描述:subcaption

\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{geometry,lmodern}
\geometry{showframe}
\usepackage{caption}

\begin{document}

\begin{verbatim}
\captionbox[list entry]{heading}[width][inner-pos]{contents}
The \captionbox is a \parbox and ...
\end{verbatim}

\begin{figure}[!h]
  \fbox{%
    \captionbox{With \texttt{\string\captionbox}}[0.45\linewidth][c]{%
      inner-pos                            \\
      specifies                            \\
      how the contents                     \\
      will be justified inside the resulting \texttt{\string\parbox};
      it can be either `c' (for \texttt{\string\centering}), `l' (for
      \texttt{\string\raggedright}), `r' (for
      \texttt{\string\raggedleft}), or `s' \\
      (for no special justification).      \\
      The default is `c'.}%
  }%
  \hfill
  \fbox{%
    \parbox[b]{0.45\linewidth}{\centering
      inner-pos                            \\
      specifies                            \\
      how the contents                     \\
      will be justified inside the resulting \texttt{\string\parbox};
      it can be either `c' (for \texttt{\string\centering}), `l' (for
      \texttt{\string\raggedright}), `r' (for
      \texttt{\string\raggedleft}), or `s' \\
      (for no special justification).      \\
      The default is `c'.}%
  }
\end{figure}
\end{document}

结果如下:

在此处输入图片描述

请注意,中的文本\captionbox稍微向左移动,并且只有最后一行居中。您可以将其更改[c][l][r],这对我来说也很奇怪。这是预期的行为还是我遗漏了什么?

答案1

我回答我自己,我认为可以通过修补宏来解决这个问题\caption@iiibox,以便将其保存contents在令牌寄存器中而不是\hbox

\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{geometry,lmodern,caption,graphicx}
\geometry{showframe}

\newcommand\stackfigs[2]{%
  \fbox{%
    \captionbox{#2}[0.3\textwidth][#1]{%
      \includegraphics[width=0.2\textwidth,keepaspectratio]{example-image-a}\\[\smallskipamount]
      \includegraphics[width=0.2\textwidth,keepaspectratio]{example-image-b}\\[\smallskipamount]
      \includegraphics[width=0.2\textwidth,keepaspectratio]{example-image-c}%
    }%
  }%
}

\newcommand\text{%
  inner-pos                            \\
  specifies                            \\
  how the contents                     \\
  will be justified inside the resulting \texttt{\string\parbox};
  it can be either `c' (for \texttt{\string\centering}), `l' (for
  \texttt{\string\raggedright}), `r' (for
  \texttt{\string\raggedleft}), or `s' \\
  (for no special justification).      \\
  The default is `c'.%
}

\begin{document}

\begin{verbatim}
\captionbox[list entry]{heading}[width][inner-pos]{contents}
The \captionbox is a \parbox and ...
\end{verbatim}

\makeatletter
\long\def\caption@iiibox#1#2#3#4[#5]#6{%
  %% start mod
  % \setbox\@tempboxa\hbox{#6}%
  \toks@={}%
  \toks@={#6}%
  %% end mod
  \begingroup
  #1*% set \caption@position
  \caption@iftop{%
    \endgroup
    \parbox[t]{#4}{%
      #1\relax
      \caption@setposition t%
      \vbox{\caption#2{#3}}%
      \captionbox@hrule
      \csname caption@hj@#5\endcsname
      %% start mod
      % \unhbox\@tempboxa
      \the\toks@
      %% end mod
    }%
  }{%
    \endgroup
    \parbox[b]{#4}{%
      #1\relax
      \caption@setposition b%
      \csname caption@hj@#5\endcsname
      %% start mod
      % \unhbox\@tempboxa
      \the\toks@
      %% end mod
      \captionbox@hrule
      \vtop{\caption#2{#3}}}%
  }}
\makeatother

\begin{figure}[!h]
  \makeatletter
  \fbox{%
    \captionbox{With \texttt{\string\captionbox}}[0.45\linewidth][c]{%
      \text
    }%
  }%
  \hfill
  \fbox{\parbox[b]{0.45\linewidth}{\centering\text}}
\end{figure}

\begin{figure}[!h]
  \centering
  \stackfigs l{Left Aligned}
  \hfill
  \stackfigs c{Centered}
  \hfill
  \stackfigs r{Right Aligned}
  \caption{Stacking figures in \texttt{\string\captionbox}}
\end{figure}

\end{document}

在此处输入图片描述

相关内容