我有一个列表环境,但标题和列表之间的间距太大。我可以使用 \vspace 来控制它,但在这种情况下,我应该将该命令添加到我的所有列表中。有没有办法改变它们?
]1
\documentclass[12pt]{article}
\usepackage{caption}
\usepackage{tikz}
\usepackage{listings}
\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\definecolor{ogreen}{rgb}{0.0, 0.5, 0.0}
\colorlet{numb}{magenta!60!black}
\DeclareCaptionType{codetype}[Listing][List of mytype]
\newenvironment{code}{}{}
\lstset{ %
basicstyle=\scriptsize\ttfamily,
frame=L,
xleftmargin=\parindent,
}
\lstdefinelanguage{hello}{
emph={grammar, filter, def, class, end, typedef, type, constraint, sentence},
emphstyle=\textbf,
escapechar=\,
basicstyle=\ttfamily\scriptsize,
numbers=left, % <----
numberstyle=\ttfamily\scriptsize,
stepnumber=1,
numbersep=3pt,
showstringspaces=false,
breaklines=true,
frame=lines,
framerule=1pt,
rulesep=1pt,
framexleftmargin=2em,
literate=
*{0}{{{\color{numb}0}}}{1}
{1}{{{\color{numb}1}}}{1}
{2}{{{\color{numb}2}}}{1}
{3}{{{\color{numb}3}}}{1}
{4}{{{\color{numb}4}}}{1}
{5}{{{\color{numb}5}}}{1}
{6}{{{\color{numb}6}}}{1}
{7}{{{\color{numb}7}}}{1}
{8}{{{\color{numb}8}}}{1}
{9}{{{\color{numb}9}}}{1}
{:}{{{\color{punct}{:}}}}{1}
{,}{{{\color{punct}{,}}}}{1}
{\{}{{{\color{delim}{\{}}}}{1}
{\}}{{{\color{delim}{\}}}}}{1}
{[}{{{\color{delim}{[}}}}{1}
{]}{{{\color{delim}{]}}}}{1},
}
\begin{document}
\begin{figure}[htpb]
\begin{lstlisting}[language=hello,firstnumber=1]
function bool priceMatch(produceName, price) = {
return
( produceName == "apple" && 0 <= price <= 1000 )
}
\end{lstlisting}
\captionof{codetype}{Type definitions}
\label{code:function}
\end{figure}
\begin{figure}[htpb]
\begin{lstlisting}[language=hello,firstnumber=1]
function bool priceMatch(produceName, price) = {
return
( produceName == "apple" && 0 <= price <= 1000 )
}
\end{lstlisting}
\vspace{-0.5cm}
\captionof{codetype}{Type definitions}
\label{code:function}
\end{figure}
\end{document}
添加
通过这种改变,间隙问题得到解决,但 \ref 不起作用;它保持原样??
。
\begin{lstlisting}[language=hello,firstnumber=1,
abovecaptionskip=0pt, caption={Type definitions}, captionpos=b]
答案1
我想知道为什么您没有将 lstlistings 的标题键与 captionpos 和 abovecaptionskip 或 belowcaptionskip 一起使用?
针对以上问题,我建议采用以下解决方案。
\documentclass[12pt]{article}
\usepackage{caption}
\usepackage{tikz}
\usepackage{listings}
\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\definecolor{ogreen}{rgb}{0.0, 0.5, 0.0}
\colorlet{numb}{magenta!60!black}
\DeclareCaptionType{codetype}[Listing][List of mytype]
\newenvironment{code}{}{}
\lstset{ %
basicstyle=\scriptsize\ttfamily,
frame=L,
xleftmargin=\parindent,
}
\lstdefinelanguage{hello}{
emph={grammar, filter, def, class, end, typedef, type, constraint, sentence},
emphstyle=\textbf,
escapechar=\,
basicstyle=\ttfamily\scriptsize,
numbers=left, % <----
numberstyle=\ttfamily\scriptsize,
stepnumber=1,
numbersep=3pt,
showstringspaces=false,
breaklines=true,
frame=lines,
framerule=1pt,
rulesep=1pt,
framexleftmargin=2em,
literate=
*{0}{{{\color{numb}0}}}{1}
{1}{{{\color{numb}1}}}{1}
{2}{{{\color{numb}2}}}{1}
{3}{{{\color{numb}3}}}{1}
{4}{{{\color{numb}4}}}{1}
{5}{{{\color{numb}5}}}{1}
{6}{{{\color{numb}6}}}{1}
{7}{{{\color{numb}7}}}{1}
{8}{{{\color{numb}8}}}{1}
{9}{{{\color{numb}9}}}{1}
{:}{{{\color{punct}{:}}}}{1}
{,}{{{\color{punct}{,}}}}{1}
{\{}{{{\color{delim}{\{}}}}{1}
{\}}{{{\color{delim}{\}}}}}{1}
{[}{{{\color{delim}{[}}}}{1}
{]}{{{\color{delim}{]}}}}{1},
}
\begin{document}
\begin{figure}[htpb]
\begin{lstlisting}[language=hello,firstnumber=1]
function bool priceMatch(produceName, price) = {
return
( produceName == "apple" && 0 <= price <= 1000 )
}
\end{lstlisting}
\captionof{lstlisting}{Type definitions}
\label{code:function}
\end{figure}
\begin{figure}[htpb]
\begin{lstlisting}[language=hello,firstnumber=1,
abovecaptionskip=0pt, caption={Type definitions}, captionpos=b, label={code:test}]
function bool priceMatch(produceName, price) = {
return
( produceName == "apple" && 0 <= price <= 1000 )
}
\end{lstlisting}
% \captionof{codetype}{Type definitions}
% \vspace{-1cm}
\label{code:function}
\end{figure}
This is a reference to a code \ref{code:test}.
\end{document}