Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

My code is being formatted in a way that makes it drift off the page when it's too long rather than just jump to a new line. I have tried lots of different things, pasted all kinds of examples from the answers on here for code formatting but none of it seems to stop the lengthy bits from going over. I don't know why it is happening so I am at a loss on how to fix it.

Here is an example of the worst offending (it just goes right out the page for no good reason)

 \documentclass{article}
 \usepackage[margin=2cm]{geometry}
 \usepackage[svgnames]{xcolor}
\usepackage{changepage}
\usepackage{framed}
\usepackage{tikz}
\usepackage{listings}
\usepackage{bbding}


    \lstset{backgroundcolor=\color{LightSteelBlue!20},
    frame=trbl,
    rulecolor=\color{black!30},
    xrightmargin=7pt}

   \begin{document}
   \begin{lstlisting}


public void write(double step, double max, PrintWriter locusOutFile, PrintWriter impOutFile) throws IOException {

\end{lstlisting}
\end{document}

Thanks in advance.

share|improve this question
2  
You haven't used breaklines=true –  David Carlisle Dec 19 '12 at 16:02
add comment

1 Answer

up vote 8 down vote accepted

enter image description here

As noted in comment on the question you were missing breaklines=true

 \documentclass{article}
 \usepackage[margin=2cm]{geometry}
 \usepackage[svgnames]{xcolor}
\usepackage{changepage}
\usepackage{framed}
\usepackage{tikz}
\usepackage{listings}
\usepackage{bbding}


    \lstset{breaklines=true,backgroundcolor=\color{LightSteelBlue!20},
    frame=trbl,
    rulecolor=\color{black!30},
    xrightmargin=7pt}

   \begin{document}
   \begin{lstlisting}


public void write(double step, double max, PrintWriter locusOutFile, PrintWriter impOutFile) throws IOException {

\end{lstlisting}
\end{document}
share|improve this answer
    
Genius! That worked. –  Magpie Dec 19 '12 at 16:06
    
I just searched for long in the listings manual. –  David Carlisle Dec 19 '12 at 16:08
    
why did you search for long? –  Magpie Dec 19 '12 at 16:14
    
first few letters of "long line" lucky guess? But really most verbatim listing packages will not line break by default just like a pre in html does not (as you see in your question) –  David Carlisle Dec 19 '12 at 16:19
    
@cmhughes note added as requested –  David Carlisle Dec 19 '12 at 17:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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