# How to align source code produced by listings package with main paragraph text

I'm using the latex listings package for the first time to format some HTML. I'm noticing that by default, the html code is being pushed further left than the main paragraph text of my document. Is there any simply option or way of specifying to listings that all source code should have the same margin width on the left as my general paragraph text does?

For reference, my latex code thus far is as follows:

\begin{lstlisting}[language=html]
<html lang=en">
<body>
...
<img src=http://cnn.com/af/pic.gif"></img>
...
</body>
</html>
\end{lstlisting}


edit: I've isolated this down to only happening when I add line numbers to the listing using:

\lstset{
numbers=left
}


Minimum working example:

\documentclass{article}
\usepackage{lipsum,listings}
\begin{document}
\lstset{
numbers=left
}
\lipsum[1]
\begin{lstlisting}[language=html]
<html lang=en">
<body>
...
<img src=http://cnn.com/af/pic.gif"></img>
...
</body>
</html>
\end{lstlisting}
\end{document}


So is there a way to get the line numbers to align with the general paragraph text?

-
Can you provide a minimal working example that demonstrates this? The listings package does a way to indent the code, but as I recall, it defaults to aligning it with the text. – TH. Sep 18 '10 at 11:15
sure, have edited question to include this and a little more detail – Bryce Thomas Sep 18 '10 at 13:38
So it’s just the line numbers that are in the margin? The listing itself is aligned in your example. – Caramdir Sep 18 '10 at 16:33
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Jubobs Feb 27 '14 at 11:31

Works fine for me too! My guess is that you should check your lstset command, you probably need to adjust the margins (xleftmargin). listings have an option for virtually everything:

  \lstset{
%frame=tblr,
framesep=5pt,
basicstyle=\normalsize\ttfamily,
showstringspaces=false,
keywordstyle=\itshape\color{blue},
%identifierstyle=\ttfamily,
stringstyle=\color{Maroon},
rulecolor=\color{Gray},
xleftmargin=5pt,
xrightmargin=5pt,
aboveskip=\bigskipamount,
belowskip=\bigskipamount,
%backgroundcolor=\color{LightGray!.50}
}

-
if frame=single then you also need to adjust xrightmargin. – xport May 30 '11 at 12:36
@xport Quite likely, thanks for coming back to make a comment. – Yiannis Lazarides May 30 '11 at 14:24

You can combine the xleftmargin and numbersep options to line the numbers up. For example, if you have lines 2 digit line numbers, you could do something like this:

\usepackage{calc}
\newlength\listingnumberwidth
\setlength\listingnumberwidth{\widthof{00} + 1em}
\lstset{
numbers=left,
xleftmargin=\listingnumberwidth,
numbersep=1em
}


Of course, if you use the numberstyle option, you'll need to take that into account for the \widthof.

-
if frame=single then you also need to adjust xrightmargin. – xport May 30 '11 at 12:35