我希望标题位置位于页面的中心,就像我的例子顶部的那样。
这是我的代码:
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\usepackage[justification=centering]{caption}
\lstdefinestyle{SC}{
language=erlang,
xleftmargin=.2\textwidth,
xrightmargin=.2\textwidth
}
\lstset{
numbers=left,
numberstyle=\tiny,
basicstyle=\scriptsize,
showstringspaces=false,
showspaces=false,
commentstyle=\color{blue},
keywordstyle=\bfseries\color{purple},
captionpos=t,
frame=lines
}
\begin{document}
\begin{lstlisting}[style=SC, caption={test}]
% Stop all containers
docker stop $(docker ps -a -q)
% Delete all containers
docker rm $(docker ps -a -q)
% Delete all images
docker rmi $(docker images -q)
\end{lstlisting}
\end{document}
图像显示了该问题。
如何将标题设置为居中?我在软件包文档中没有找到解决方案。
答案1
该错误已在 caption 包中修复(https://gitlab.com/axelsommerfeldt/caption/commit/0a32b2b8d4f814c58da1969ac7f38c7f9ec8f183) 并且已包含在当前的 texlive 中大约半年了。
答案2
我认为这是 中的一个错误caption
。通过您的xleftmargin
设置,您减少了线宽,但标题并未考虑到列表下方标题的这一情况。
实际上已经有一个错误报告:https://github.com/axelsommerfeldt/caption/issues/1。
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\usepackage[justification=centering,singlelinecheck=false]{caption}
\makeatletter
\def\lst@MakeCaption#1{% #1 is `t' or `b'
\begingroup
\caption@setposition{#1}%
\caption@iftop{%
\ifdim\hsize>\linewidth
\hsize\linewidth
\fi
\@tempdima\belowcaptionskip
\belowcaptionskip\abovecaptionskip
\abovecaptionskip\@tempdima}
{\ifdim\hsize>\dimexpr \linewidth + \lst@xleftmargin + \lst@xrightmargin\relax
\hsize\dimexpr \linewidth + \lst@xleftmargin + \lst@xrightmargin\relax
\fi}%
\caption@setup{rule=0}%
\caption@setoptions{lstlisting}%
\caption@setautoposition{#1}%
\caption@begin{lstlisting}%
\caption@ORI@lst@MakeCaption{#1}%
\caption@end
\endgroup}%
\makeatother
\lstdefinestyle{SC}{
language=erlang,
xleftmargin=.2\textwidth,
xrightmargin=.2\textwidth
}
\lstset{
numbers=left,
numberstyle=\tiny,
basicstyle=\scriptsize,
showstringspaces=false,
showspaces=false,
commentstyle=\color{blue},
keywordstyle=\bfseries\color{purple},
captionpos=t,
frame=lines
}
\begin{document}
\begin{lstlisting}[style=SC, caption={test},captionpos=t]
xxxxxxxxxxxxxxxxxxxxx\\
xxxxxxxxxxxxxxxxxxxxxxxx
\end{lstlisting}
\begin{lstlisting}[style=SC, caption={test},captionpos=b]
% Stop all containers
docker stop $(docker ps -a -q)
% Delete all containers
docker rm $(docker ps -a -q)
% Delete all images
docker rmi $(docker images -q)
\end{lstlisting}
\end{document}
答案3
根据评论,删除标题包并更改为captionpos=b
提供居中标题
\listfiles
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\lstdefinestyle{SC}{
language=erlang,
xleftmargin=.2\textwidth,
xrightmargin=.2\textwidth
}
\lstset{
numbers=left,
numberstyle=\tiny,
basicstyle=\scriptsize,
showstringspaces=false,
showspaces=false,
commentstyle=\color{blue},
keywordstyle=\bfseries\color{purple},
captionpos=b,
frame=lines
}
\begin{document}
\begin{lstlisting}[style=SC, caption={test}]
% Stop all containers
docker stop $(docker ps -a -q)
% Delete all containers
docker rm $(docker ps -a -q)
% Delete all images
docker rmi $(docker images -q)
\end{lstlisting}
\end{document}
这是以下版本的文件
*File List*
article.cls 2014/09/29 v1.4h Standard LaTeX document class
size10.clo 2014/09/29 v1.4h Standard LaTeX file (size option)
color.sty 2016/07/10 v1.1e Standard LaTeX Color (DPC)
color.cfg 2016/01/02 v1.6 sample color configuration
pdftex.def 2018/01/08 v1.0l Graphics/color driver for pdftex
infwarerr.sty 2016/05/16 v1.4 Providing info/warning/error messages (HO)
ltxcmds.sty 2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
listings.sty 2015/06/04 1.6 (Carsten Heinz)
keyval.sty 2014/10/28 v1.15 key=value parser (DPC)
lstmisc.sty 2015/06/04 1.6 (Carsten Heinz)
listings.cfg 2015/06/04 1.6 listings configuration
supp-pdf.mkii
lstlang1.sty 2015/06/04 1.6 listings language file
lstlang2.sty 2015/06/04 1.6 listings language file
lstlang3.sty 2015/06/04 1.6 listings language file
***********