我在 listings 对象中放了一些伪代码,并在其周围加上标题和方框。我已使用此处建议的 fancybox 解决方案来处理 listings 对象:如何将列表居中
一切都很好,除了标题没有居中。
输出如下:
代码如下:
\documentclass[paper=a4, fontsize=11pt]{article} % A4 paper and 11pt font size
\usepackage[top=0.8in, bottom=0.8in, left=0.8in, right=0.8in]{geometry}
\usepackage{fourier} % Use the Adobe Utopia font for the document
\usepackage[english]{babel} % English language/hyphenation
\usepackage{listings}
\lstset{numbers=left, numbersep=10pt, frame = single, framexleftmargin=17pt}
\usepackage{fancybox}
\makeatletter
\newenvironment{CenteredBox}{%
\begin{Sbox}}{% Save the content in a box
\end{Sbox}\centerline{\parbox{\wd\@Sbox}{\TheSbox}}}% And output it centered
\makeatother
\usepackage{sectsty} % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all sections centered, the default font and small caps
\begin{document}
\section{Chapter}
\begin{CenteredBox}
\begin{lstlisting}[caption={Cool code},label=cool,linewidth=6in]
validity_type <- 0,1,2
uid <- '00000'
start_date <- '2014-11-20 00:00:00'
end_date <- '2014-11-21 00:00:00'
sql_query <- "SELECT variable_1
, variable_2
, variable_3
FROM prices
WHERE validity_type = ",validity_type," AND
uid = '",uid,"'
GROUP BY variable_1, variable_2, variable_3
HAVING from_ts >= '",start_date,"' AND from_ts < '",end_date,"'
ORDER BY variable_1 ASC, variable_2 ASC")
\end{lstlisting}
\end{CenteredBox}
\end{document}
任何帮助深表感谢。
答案1
这里有几件事。首先,法线lstlisting
是左对齐的。此外,行号(通常)是左重叠的,因此突出到左边距。最后,OP 的 MWE 任意选择 6in 作为列表行宽,这个数字与 并没有内在联系\textwidth
。
如果不使用CenteredBox
环境,标签将居中。但是,当lstlisting
框居中时,标签会随之向右移动。这需要进行两项更改,既要将标签带回中心,又要将框居中:
首先,我通过在标题后添加空格将标题居中,如
caption={Cool code\hspace{\dimexpr\textwidth-6in+2ex}}
。 部分\textwidth-6in
表示文本宽度和指定宽度之间的差异lstlisting
,而 2ex 是我对列表左边缘两位数标签产生的额外重叠宽度的估计。\hspace{\dimexpr.5\textwidth-3in+1ex}
我做的另一件事是在列表末尾添加空格,以便将列表向左移动。请注意,此距离恰好是添加到标题宽度的 1/2。
\documentclass[paper=a4, fontsize=11pt]{article} % A4 paper and 11pt font size
\usepackage[top=0.8in, bottom=0.8in, left=0.8in, right=0.8in]{geometry}
\usepackage{fourier} % Use the Adobe Utopia font for the document
\usepackage[english]{babel} % English language/hyphenation
\usepackage{listings}
\lstset{numbers=left, numbersep=10pt, frame = single, framexleftmargin=17pt}
\usepackage{fancybox}
\makeatletter
\newenvironment{CenteredBox}{%
\begin{Sbox}}{% Save the content in a box
\end{Sbox}\centerline{\parbox{\wd\@Sbox}{\TheSbox}}}% And output it centered
\makeatother
\usepackage{sectsty} % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all sections centered, the default font and small caps
\begin{document}
\section{Chapter}
\noindent\rule{.5\textwidth}{2pt} this line is .5 textwidth
\begin{CenteredBox}
\begin{lstlisting}[caption={Cool code\hspace{\dimexpr\textwidth-6in+2ex}},%
label=cool,linewidth=6in]
validity_type <- 0,1,2
uid <- '00000'
start_date <- '2014-11-20 00:00:00'
end_date <- '2014-11-21 00:00:00'
sql_query <- "SELECT variable_1
, variable_2
, variable_3
FROM prices
WHERE validity_type = ",validity_type," AND
uid = '",uid,"'
GROUP BY variable_1, variable_2, variable_3
HAVING from_ts >= '",start_date,"' AND from_ts < '",end_date,"'
ORDER BY variable_1 ASC, variable_2 ASC")
\end{lstlisting}
\hspace{\dimexpr.5\textwidth-3in+1ex}
\end{CenteredBox}
\medskip
\noindent\hrulefill\\
the above line is textwidth
\end{document}