在列表环境中使用 `\captionof` 将标题左移

在列表环境中使用 `\captionof` 将标题左移

我在列表环境中使用\captionof来自caption包的命令。我希望标题相对于页面居中,或者由于列表的缩进而向右移动。然而,标题向左移动(见下面的例子,其中有四个列表来夸大问题)。如何解决这个问题?

\documentclass{article}
\let\oldcaption\caption
\usepackage{caption}
\begin{document}
\begin{figure}
  Within a float:
  \oldcaption{Default \LaTeX's caption. OK.}
\end{figure}
\begin{figure}
  Within a float:
  \caption{\textsc{Caption}'s \texttt{\string\caption}. OK.}
\end{figure}
\noindent Outside of a float:
\bgroup
\captionof{figure}{\textsc{Caption}'s \texttt{\string\captionof}. OK.}
\egroup
\begin{itemize}
\item
  \begin{itemize}
  \item
    \begin{itemize}
    \item
      \begin{itemize}
      \item
        \begin{figure}[h]
          Within a float, within lists:
          \caption{\textsc{Caption}'s \texttt{\string\caption}. OK.}
        \end{figure}
        \noindent
        Outside of a float, within lists:
        \bgroup
        \captionof{figure}{\textsc{Caption}'s \texttt{\string\captionof}. Left shift.}
        \egroup
      \end{itemize}
    \end{itemize}
  \end{itemize}
\end{itemize}
\end{document}

答案1

我不知道这种行为的原因,但如果你把你的\captionof内部\parbox宽度\linewidth

\parbox{\linewidth}{\captionof{figure}{\textsc{Caption}'s \texttt{\string\captionof}. Left shift.}}

标题位于剩余线宽的中心,即按预期向右移动:

在此处输入图片描述

完整 MWE:

\documentclass{article}
\let\oldcaption\caption
\usepackage{caption}
\begin{document}
\begin{figure}
  Within a float:
  \oldcaption{Default \LaTeX's caption. OK.}
\end{figure}
\begin{figure}
  Within a float:
  \caption{\textsc{Caption}'s \texttt{\string\caption}. OK.}
\end{figure}
\noindent Outside of a float:
\bgroup
\captionof{figure}{\textsc{Caption}'s \texttt{\string\captionof}. OK.}
\egroup
\begin{itemize}
\item
  \begin{itemize}
  \item
    \begin{itemize}
    \item
      \begin{itemize}
      \item
        \begin{figure}[h]
          Within a float, within lists:
          \caption{\textsc{Caption}'s \texttt{\string\caption}. OK.}
        \end{figure}
        \noindent
        Outside of a float, within lists:

        \vspace*{\baselineskip}
        \parbox{\linewidth}{\captionof{figure}{\textsc{Caption}'s \texttt{\string\captionof}. Left shift.}}
      \end{itemize}
    \end{itemize}
  \end{itemize}
\end{itemize}
\end{document} 

还要注意,第四个标题不在列表之内...这可能就是为什么你不会遇到这种奇怪行为的原因。

答案2

我在你最后的标题中添加了 \centering,这似乎解决了问题,尽管它仍然与其他标题不一致。

    \documentclass{article}
\let\oldcaption\caption
\usepackage{caption}
\begin{document}
\begin{figure}
  Within a float:
  \oldcaption{Default \LaTeX's caption. OK.}
\end{figure}
\begin{figure}
  Within a float:
  \caption{\textsc{Caption}'s \texttt{\string\caption}. OK.}
\end{figure}
\noindent Outside of a float:
\bgroup
\captionof{figure}{\textsc{Caption}'s \texttt{\string\captionof}. OK.}
\egroup
\begin{itemize}
\item
  \begin{itemize}
  \item
    \begin{itemize}
    \item
      \begin{itemize}
      \item
        \begin{figure}[h]
          Within a float, within lists:
          \caption{\textsc{Caption}'s \texttt{\string\caption}. OK.}
        \end{figure}
        \noindent
        Outside of a float, within lists:
        \bgroup
        \centering\captionof{figure}{\textsc{Caption}'s \texttt{\string\captionof}. Left shift.}
        \egroup
      \end{itemize}
\end{itemize}
      \end{itemize}
\end{itemize}
\end{document}

这能解决你的问题吗?

相关内容