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.

I have a listing of my Java code currently the font is too big and the code runs off the page.

Here is what my listing looks like:

enter image description here

share|improve this question
    
To reduce the length of the code you can use the lstset options columns=fullflexible and basicstyle=\small or even basicstyle=\scriptsize. –  guillem Dec 14 '12 at 7:33

1 Answer 1

up vote 6 down vote accepted

You can change basicstyle and linewidth to achieve what you want. I, for example, use the following within my lstlistings-settings:

basicstyle=\footnotesize\ttfamily,
linewidth=\textwidth

Additionally, I import the courier package to get a nicer font for ttfamilyusing \usepackage{courier}

EDIT: I also use Java code within LaTeX documents quite regularly. This is how my total setup looks like:

\lstset{
    basicstyle=\footnotesize\ttfamily,
    linewidth=\textwidth,
    numbers=left,
    numberstyle=\tiny\emptyaccsupp,
    columns=flexible,
    numbersep=5pt,      % Abstand der Nummern zum Text
    tabsize=3,
    breaklines=true,
    keywordstyle=\color{RoyalBlue}\textbf,
    commentstyle=\color{ForestGreen},
    frame=bottomline,
    stringstyle=\color{WildStrawberry}\ttfamily,
    showspaces=false,
    showtabs=false,
    xleftmargin=17pt,
    framexleftmargin=17pt,
    framexrightmargin=5pt,
    framexbottommargin=4pt,
    showstringspaces=false,
    literate=%
        {Ö}{{\"O}}1
        {Ä}{{\"A}}1
        {Ü}{{\"U}}1
        {ß}{{\ss}}2
        {ü}{{\"u}}1
        {ä}{{\"a}}1
        {ö}{{\"o}}1
 }
 \lstloadlanguages{
         Java
 }
share|improve this answer
    
Is there anyway to directly modify the font size? –  CodeKingPlusPlus Dec 14 '12 at 9:09
    
@CodeKingPlusPlus You can use anything from \tiny to \Huge as a setting for basicstyle. Just the usual LaTeX way of setting the font size –  PattaFeuFeu Dec 14 '12 at 9:10
    
What about automatic page breaks? –  CodeKingPlusPlus Dec 14 '12 at 9:12
    
@CodeKingPlusPlus Here is an example of Processing using the style I posted in my answer: Screenshot example –  PattaFeuFeu Dec 14 '12 at 9:35
1  
@CodeKingPlusPlus please have a look at the listings package manual! –  hakaze Dec 14 '12 at 10:30

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.