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 some Java source code and want to display it in LaTex:

\lstset{language=Java}
\begin{lstlisting}
    IntentFileter intentfilter = new IntentFilter();
    intentfilter.addAction("com.anguanjia.action.download.START_DOWNLOAD");
    intentfilter.addAction("com.anguanjia.action.download.DOWNLOAD_SUCCESS");
    g = new blm(this, null);
    m.registerReceiver(g, intentfilter);
\end{lstlisting}

But the output is indeed ugly: enter image description here

Did I do something wrong or this is what it ought to be?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

This may serve to be a starting point towards procrastination and beautification:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\lstset{language=Java,
    basicstyle=\footnotesize\ttfamily,
    commentstyle=\ttfamily\itshape\color{gray},
    stringstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    frameround=ffff,
    frame=single,
    rulecolor=\color{black},
    tabsize=1,
    keywordstyle=\color{red}\bfseries,
    columns=fullflexible,
    morekeywords={public, class}
}

\begin{document}
\begin{lstlisting}
    IntentFileter intentfilter = new IntentFilter();
    intentfilter.addAction("com.anguanjia.action.download.START_DOWNLOAD");
    intentfilter.addAction("com.anguanjia.action.download.DOWNLOAD_SUCCESS");
    g = new blm(this, null);
    m.registerReceiver(g, intentfilter);
\end{lstlisting}
\end{document}

enter image description here

You can customise further using the directives inside \lstset. For details, refer to listings manual. For extreme beautification, you may use tcolobox with its listings only option. Search for tcblisting in tcolorbox documentation.

share|improve this answer
    
Your enthusiasm did impress me. And I'm still wondering why there isn't a more convenient way to have a better format of source code, at least a monospace font for example. –  Elderry Mar 7 at 9:01
2  
@Elderry Not choosing a monospace font by default was a precise choice made by the package author, who was apparently fond of listings in normal type. I usually add also columns=fullflexible to avoid spaced letters. –  egreg Mar 7 at 9:56
    
Instead of keywordstyle=\color{red}\bf, you could put keywordstyle=\color{red}\bfseries, to get bold monospaced letters if available (unless you DID want that output). –  Manuel Mar 7 at 10:07
    
@egreg Thanks. I left many things assuming that the list may grow. I added it now. :) –  Harish Kumar Mar 7 at 10:11
    
@Manuel That was a nice suggestion and I did bad copy pasting. Corrected. :) –  Harish Kumar Mar 7 at 10:11

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.