以下块是下面源的结果。您可以看到垂直间距的差异。它来自哪里以及如何消除它?
\documentclass[a4paper]{beamer}
\usepackage{listings}
\usetheme{Warsaw}
\begin{document}
\lstset{language=Python, columns=fullflexible}
\begin{frame}[fragile]{Test}
\begin{block}{Excessive vertical space both above and below}
\begin{lstlisting}
nums = list(range(1,10))
\end{lstlisting}
\end{block}
\begin{block}{No problems with vertical spacing}
Is there?
\end{block}
\end{frame}
\end{document}
答案1
lstlisting
是一种类似于图形或方程式的环境,其定义中包含了其上方和下方的垂直空间。
您可以使用以下命令控制垂直上方和下方的空间:
\lstset{aboveskip=0pt, belowskip=0pt}
这样你就可以得到如下结果:
\documentclass\[a4paper\]{beamer}
\usepackage{listings}
\usetheme{Warsaw}
\begin{document}
\lstset{language=Python, columns=fullflexible}
\begin{frame}\[fragile\]{Test}
\begin{block}{Excessive vertical space both above and below}
\lstset{aboveskip=0pt,belowskip=0pt}
\begin{lstlisting}
nums = list(range(1,10))
\end{lstlisting}
\end{block}
\begin{block}{No problems with vertical spacing}
Is there?
\end{block}
\end{frame}
\end{document}
创建如下环境lstblock
也可能很有用:
\newenvironment{lstblock}[1]
{\begin{block}{#1}
\lstset{aboveskip=0pt,belowskip=0pt}
\begin{lstlisting}}
{\end{lstlisting}\end{block}}