# Indent a code listing in LaTeX

For a little LaTeX document I am writing I would like to insert source code fields (in SQL) into my paper.

I found out that I can use the listings package for this, which works well so far:

\usepackage{color}
\definecolor{light-gray}{gray}{0.95}

\usepackage{listings}
\lstset{numbers=right,
numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{light-gray},
numbersep=5pt}
\lstset{language=SQL}


This works well so that I can insert code listings like:

\begin{lstlisting}{insert}
INSERT INTO Tabelle (Spalte1, Spalte2, Spalte3) VALUES (Wert1, Wert2, Wert3);
\end{lstlisting}


What it shows me are light-gray boxes containing the code, but they start at the beginning of the line, just like the text. I now would like to have code listings automatically being indented a bit. So that the gray box doesn't start at the level of the text.

Can anybody tell me how to achieve this? I am quite a beginner at working with LaTeX... would be great if anyone could help!

-

The listings package allows you to change the margins (see section 4.10 of the documentation.)

\lstset{numbers=right,
numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{light-gray},
numbersep=5pt,
xleftmargin=.25in,
xrightmargin=.25in}


-
THank you very much! This works perfectly! Didn't know it allows this attribute... – evident Feb 11 '11 at 13:46

You can do this using the xleftmargin option of listings. See the manual page 33, section 4.10 "Margins and line shapes".

Example:

\documentclass{article}

\usepackage{lipsum}
\usepackage{color}
\definecolor{light-gray}{gray}{0.95}

\usepackage{listings}
\lstset{numbers=right,
numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{light-gray},
xleftmargin=\parindent,
%xleftmargin=1cm,
%xrightmargin=\parindent,
numbersep=5pt}
\lstset{language=SQL}
\begin{document}
\lipsum[1]
\begin{lstlisting}{insert}
INSERT INTO Tabelle (Spalte1, Spalte2, Spalte3) VALUES (Wert1, Wert2, Wert3);
\end{lstlisting}
\lipsum[2]
\end{document}

-