![在列表环境中使用 `\captionof` 将标题左移](https://linux22.com/image/302924/%E5%9C%A8%E5%88%97%E8%A1%A8%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%BD%BF%E7%94%A8%20%60%5Ccaptionof%60%20%E5%B0%86%E6%A0%87%E9%A2%98%E5%B7%A6%E7%A7%BB.png)
我在列表环境中使用\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}
这能解决你的问题吗?