2

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

1
  • 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, 2012 at 7:33

1 Answer 1

6

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
 }
5
  • Is there anyway to directly modify the font size? Dec 14, 2012 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 Dec 14, 2012 at 9:10
  • What about automatic page breaks? Dec 14, 2012 at 9:12
  • @CodeKingPlusPlus Here is an example of Processing using the style I posted in my answer: Screenshot example Dec 14, 2012 at 9:35
  • 1
    @CodeKingPlusPlus please have a look at the listings package manual!
    – hakaze
    Dec 14, 2012 at 10:30

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.