28

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!

30

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} 

Adjust as necessary.

15

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}

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.