梅威瑟:
\documentclass{article}
\usepackage[stable]{footmisc}
\usepackage{footnote}
\usepackage{caption}
\usepackage{hyperref}
\usepackage{listings}\usepackage{genmpage}
\begin{document}
\begin{minipage}{\linewidth}
\centering
\begin{lstlisting}[frame=single]
mycode;
\end{lstlisting}
\captionof{figure}{Short.}
\end{minipage}
\medskip
\begin{minipage}{\linewidth}
\centering
\begin{lstlisting}[frame=single]
mycode;
\end{lstlisting}
\captionof{figure}{Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words.}
\end{minipage}
\end{document}
在第一个例子中,标题完美地居中对齐。然而,在第二个例子中,它根本没有居中对齐。尽管标题上有一个 \centering,但它看起来像是左对齐的。
编辑:
使用图形来封装列表会导致小页面混乱。
\documentclass{article}
\usepackage[stable]{footmisc}
\usepackage{footnote}
\usepackage[justification=centering]{caption}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{genmpage}
\usepackage{graphicx}
\begin{document}
\begin{minipage}{\linewidth}
\centering
\includegraphics[width=\linewidth]{example-image-a.jpg}
\captionof{figure}{Figure at \#1.}
\end{minipage}
\begin{figure}
\begin{lstlisting}[frame=single]
Figure at #2;
\end{lstlisting}
\caption{Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words.}
\end{figure}
\begin{figure}
\begin{lstlisting}[frame=single]
Figure at #3;
\end{lstlisting}
\caption{Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words.}
\end{figure}
\end{document}
图片使用 minipages 的原理来源于此:http://tex.stackexchange.com/a/54449/161008
列表中图形的使用源自此处: https://tex.stackexchange.com/a/19668/161008
答案1
这是 caption 包的默认行为。如果标题少于一行,则居中,否则两端对齐。
如果您希望所有字幕都居中,您可以使用 修改默认行为justification=centering
。
\documentclass{article}
\usepackage[stable]{footmisc}
\usepackage{footnote}
\usepackage[justification=centering]{caption}
\usepackage{listings}
\usepackage{genmpage}
\usepackage{hyperref}
\renewcommand{\lstlistingname}{Figure}
\begin{document}
\begin{lstlisting}[frame=single,caption={Short},captionpos=b]
mycode;
\end{lstlisting}
\medskip
\begin{lstlisting}[frame=single,caption={Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words.},captionpos=b]
mycode;
\end{lstlisting}
\end{document}