这是我对 lstlisting 环境的选择:
\lstset{language=SQL,frame=ltrb,
keywordstyle=\ttfamily\color{OliveGreen},
identifierstyle=\ttfamily\color{CadetBlue}\bfseries,
commentstyle=\color{Brown},
stringstyle=\ttfamily,
}
这是列出的环境:
\begin{lstlisting}[caption={Creazione tabella 123r}]
create table SP (
SNum varchar(3),
PNum varchar(3),
QTY decimal(5) not null,
constraint SP_PK primary key(SNum, PNum),
constraint SP_FK_S foreign key(SNum) references S(SNum) on delete cascade,
constraint SP_FK_P foreign key(PNum) references P(PNum) on delete cascade
);
\end{lstlisting}
但是列表的框架太小,在某些情况下无法容纳一行文本,就像图片 1 中那样。问题出在图片 2 中。
我怎么解决这个问题?
我如何才能将数字与上面的文本对齐?
答案1
问题的解决方案在于您在环境中使用太多空格来缩进代码lstlisting
。因此,您可以使用literate
以下选项来解决问题:listings:设置制表符大小,同时使用空格进行缩进。
这是一个 MWE,它通过使用包调整空格数量来解决您的问题listings
:
\documentclass[10pt]{article}
\usepackage{geometry}
\usepackage[rgb,dvipsnames]{xcolor}
\definecolor{background}{rgb}{0.94,0.95,0.96}
\usepackage{listings,eulervm,palatino}
\lstset{language=SQL,%frame=ltrb,
backgroundcolor=\color{background},
keywordstyle=\ttfamily\color{OliveGreen},
identifierstyle=\ttfamily\color{CadetBlue}\bfseries,
commentstyle=\color{Brown},
stringstyle=\ttfamily,
tabsize=1,
% literate={\ \ }{{\ }}1, reduce the tab width from double to single spacing
numbers=left,
xleftmargin=2em,
% frame=single,
% framexleftmargin=1.5em % Uncomment these two lines if you prefer to have the frame
}
\begin{document}
\section*{DDL di creazione del database}
\begin{lstlisting}[caption={Creazione tabella 123r}, basicstyle=\small]
create table SP (
SNum varchar(3),
PNum varchar(3),
QTY decimal(5) not null,
constraint SP_PK primary key(SNum, PNum),
constraint SP_FK_S foreign key(SNum) references S(SNum) on delete cascade,
constraint SP_FK_P foreign key(PNum) references P(PNum) on delete cascade
);
\end{lstlisting}
\end{document}
另一种方法可能是使用包minted
,我发现它比更直接listings
。
以下是使用 MWE 的情况minted
:
\documentclass[10pt]{article}
\usepackage{geometry}
\usepackage[rgb,dvipsnames]{xcolor}
\definecolor{background}{rgb}{0.94,0.95,0.96}
\usepackage{minted,eulervm,palatino}
\usemintedstyle[sql]{tango} % tango is the color style
\renewcommand\listoflistingscaption{List of source codes}
% : > pygmentize -L styles # shows all the possible color styles for the package minted
% : > pdflatex -shell-escape file.tex # compile with the -shell-escape option active
\begin{document}
\listoflistings
\section*{DDL di creazione del database}
\begin{listing}[ht]
\begin{minted}[gobble=1,
bgcolor=background,
fontsize=\small,
linenos=true,
xleftmargin=1.5em]{sql}
create table SP (
SNum varchar(3),
PNum varchar(3),
QTY decimal(5) not null,
constraint SP_PK primary key(SNum, PNum),
constraint SP_FK_S foreign key(SNum) references S(SNum) on delete cascade,
constraint SP_FK_P foreign key(PNum) references P(PNum) on delete cascade
);
\end{minted}
\caption{Creation of table 123r}
\end{listing}
\end{document}
其结果如下: