1

This is my options for the lstlisting ambient:

\lstset{language=SQL,frame=ltrb,
    keywordstyle=\ttfamily\color{OliveGreen},
    identifierstyle=\ttfamily\color{CadetBlue}\bfseries, 
    commentstyle=\color{Brown},
    stringstyle=\ttfamily,
}

And this is the lstlisting ambient:

\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}

But the frame of lstlisting is too small to hold in some cases the text in a single line as in the picture number 1. The problem is in the picture number 2.

Picture 1

Picture 2

How can I solve this problem?

How can I align numbers with the text above?

  • 3
    Can you add complete MWE which begin with \documentclass and end with \end{document} – Salim Bou Dec 28 '16 at 18:44
2

The solution of your problem lies in the fact that you use too many spaces to indent your code in the lstlisting environment. Hence, you could use the literate option to overcome the problem, as suggested here: listings: Set tab size while using spaces for indentation.

Here is a MWE that solves your problem by adjusting the number of spaces by using the package 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}

which produces the following:enter image description here

Another way could be using the package minted, which I find more straightforward than listings.

Here is a MWE using 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}   

which produces the following:

enter image description here

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.