I am trying to add a java
and c++
code in a LaTeX
document. I am using listings
package and I will define a different style for each language using \lstdefinestyle{myStyle}{<options>}
. What I am trying to achieve is the colouring and the emphasis that the editor Kate
uses, as shown in the next image
Look for instance the different styles for comments. When a comment starts with //
it's gray, while when it's enclosed between /**comment*/
it's green.
Or perhaps the different colors for keywords : int
is dark blue while static final int
is brighter.
Also take a look at the numbers: They are yellow. Or for instance the strings are red. I've tried to reproduce those but I wasn't able to do this(the numbers) or the purple methods that are used.
For instance I can't define different coloring for different comments or key words. I can't color at all the numbers and the get***
methods. MY code is
\documentclass{article}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{xcolor}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstdefinestyle{JavaStyle}{
backgroundcolor=\color{lbcolor},
tabsize=4,
% rulecolor=,
language=Java, % choose the language of the code
basicstyle=\scriptsize, % the size of the fonts that are used for the code
upquote=true,
aboveskip={1.5\baselineskip},
columns=fixed,
showstringspaces=false,
extendedchars=false,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=single,
numbers=left,
showtabs=false,
showspaces=false,
showstringspaces=false,
identifierstyle=\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
%commentstyle=\color[rgb]{0.026,0.112,0.095},
commentstyle=\itshape\color{green!40!black},
stringstyle=\itshape\color{red!90!black},
numberstyle=\itshape\color{yellow!50!black}
}
\begin{document}
\begin{lstlisting}[style=JavaStyle]
int[] ones = {1,1};
int points = 10;
int[] region = new int[points];
int timeTicks;
int totalTime;
int LiveTime = 0;
int totcount;
int evType = 1; //The Type ID for these kinds of events
SimpleDateFormat today = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat now = new SimpleDateFormat("HH:mm:ss:SSS");
Map<String,KmaxHist> histograms = new HashMap<String,KmaxHist>();
static final int evSize = 6;//The num of parameters per event of this type
static final int BUF_SIZE = evSize*1000;// Buffer size
static final int LP_MEM_TOP = 0xFFFF00;//Memory size 16MB
static final int READ_START = LP_MEM_TOP - BUF_SIZE;//We start the read/write pointer 1 buffer before the end
int[] blkData = new int[BUF_SIZE];
/**
* The 'init' method is executed at compile time.
*/
public void init(KmaxToolsheet toolsheet) {
tlsh = toolsheet;//Save this reference for use in the toolsheet
dev = tlsh.getKmaxDevice("DEV1");
dataField = tlsh.getKmaxWidget("$R_DATA");
countField = tlsh.getKmaxWidget("$R_COUNT");
liveTime = tlsh.getKmaxWidget("LIVE_TIME");//Live Time
realTime = tlsh.getKmaxWidget("REAL_TIME");//Real Time
deadTime = tlsh.getKmaxWidget("DEAD_TIME");//Dead Time
hist1 = tlsh.getKmaxHist("DATA1");
hist2 = tlsh.getKmaxHist("DATA2");
hist3 = tlsh.getKmaxHist("DATA3");
hist4 = tlsh.getKmaxHist("DATA4");
hist5 = tlsh.getKmaxHist("DATA5");
hist6 = tlsh.getKmaxHist("DATA6");
histoLowX = tlsh.getKmaxHist("HIST_LOW_X");
histoLowY = tlsh.getKmaxHist("HIST_LOW_Y");
histograms.put("DATA1", hist1);
histograms.put("DATA2", hist2);
histograms.put("DATA3", hist3);
\end{lstlisting}
\end{document}
and the output is
Any idea on how to reproduce the code in the first image?
Best Answer
Yes, I've got a few ideas, see below.
However, highlighting numbers (including hexadecimal expressions, such as
0xFFFF00
) in a robust way is, though not impossible, notoriously difficult. If you're determined to implement a solution, you could use my answer to How to highlight all words of the form [0-9][A-Za-z0-9]* immediately following an equal sign? as a starting point.