This is the style I use to highlight my Java code in LaTeX documents with the Listings package, with Eclipse colors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | %ColorsforcodehighlightingaccordingtotheEclipsedefaulttheme \definecolor{keywordColor}{rgb}{0.5,0,0.33} \definecolor{commentColor}{rgb}{0.25,0.5,0.35} \definecolor{stringColor}{rgb}{0.16,0,1} \definecolor{numberColor}{rgb}{0,0,0} %Colorstyle \lstdefinestyle{mystyle}{ flexiblecolumns=true, keepspaces=true, tabsize=4, showstringspaces=false, basewidth={0em,0em},, numbers=left, basicstyle=\scriptsize, numberstyle=\color{numberColor}, commentstyle=\color{commentColor}\itshape, stringstyle=\color{stringColor}\ttfamily, keywordstyle=\color{keywordColor}\bfseries, } |
And this is an example of a document, where I show the same listing both with black and white colors an Eclipse colors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | \documentclass{article} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc}% not needed on XeLaTeX/LuaLaTeX \usepackage{xcolor} \usepackage{listings} %ColorsforcodehighlightingaccordingtotheEclipsedefaulttheme \definecolor{keywordColor}{rgb}{0.5,0,0.33} \definecolor{commentColor}{rgb}{0.25,0.5,0.35} \definecolor{stringColor}{rgb}{0.16,0,1} \definecolor{numberColor}{rgb}{0,0,0} %Colorstyle \lstdefinestyle{mystyle}{ flexiblecolumns=true, keepspaces=true, tabsize=4, showstringspaces=false, basewidth={0em,0em},, numbers=left, basicstyle=\scriptsize, numberstyle=\color{numberColor}, commentstyle=\color{commentColor}\itshape, stringstyle=\color{stringColor}\ttfamily, keywordstyle=\color{keywordColor}\bfseries, } %Blackandwhitestyle \lstdefinestyle{mystylebw}{ flexiblecolumns=true, keepspaces=true, tabsize=4, showstringspaces=false, basewidth={0em,0em},, numbers=left, basicstyle=\scriptsize, commentstyle=\itshape, stringstyle=\ttfamily, } \begin{document} \section*{Demo} Withthedefaultblackandwhitestyle: \begin{lstlisting}[language=Java,style=mystylebw] //AsimpleJavaprogram classDemo{ publicstaticvoidmain(String[]args){ System.out.println("Hello,world!"); } } \end{lstlisting} \noindent Withthecolorstyle: \begin{lstlisting}[language=Java,style=mystyle] //AsimpleJavaprogram classDemo{ publicstaticvoidmain(String[]args){ System.out.println("Hello,world!"); } } \end{lstlisting} \end{document} |
Here’s the result:
