2

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

enter image description here

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

enter image description here

Any idea on how to reproduce the code in the first image?

  • Something to start with: tex.stackexchange.com/q/34896 But this will also colour digits in comments. – LaRiFaRi Sep 9 '14 at 7:17
  • I'd consider using minted for more complex stuff, but that's just because I'm too lazy to hack listings. :-) – 1010011010 Sep 9 '14 at 7:21
  • @1010011010 : I've install minted but I can't get pygmentize to install. I've installed texlive-latex-extra and python-pygments but I can't get it to work... Perhaps because I don't know how to run pdf text with shell escape in Kile... I don't want to use a terminal, I want to add this command to the quick build! – Thanos Sep 9 '14 at 8:01
  • @1010011010 : Even if I use pdflatex -shell-escape chapter7 it still doens't work. I get ! LaTeX Error: File l3unicode-data.def' not found` – Thanos Sep 9 '14 at 8:16
  • Have you tried the instructions in this answer: tex.stackexchange.com/a/23466/44160 ? This worked for me. – 1010011010 Sep 9 '14 at 8:28
3

Any idea on how to reproduce the code in the first image?

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.

enter image description here

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}

\usepackage{listings}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{lstautodedent} 
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}

\lstdefinestyle{JavaStyle}{
    language=Java,      % choose the language of the code
    deletekeywords={new,public},
    keywords=[2]{HashMap,Map,SimpleDateFormat,String},
    keywords=[3]{getKmaxDevice,getKmaxWidget,getKmaxHist,init},
    basicstyle=\scriptsize\ttfamily,
    keywordstyle=\color[RGB]{69,97,189},
    keywordstyle=[2]{\color{cyan}},
    keywordstyle=[3]\color[RGB]{137,77,155},
    commentstyle=\itshape\color{green!60!black},
    moredelim=[l][\itshape\color{gray}]{//}, %<--- overrides line-comment style
    stringstyle=\color[RGB]{192,8,8},
    numberstyle=\itshape\color{yellow!50!black},
%   backgroundcolor=\color{lbcolor},
    tabsize=4,
%   rulecolor=,
    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,
    autodedent,%<--- removes indentation
}

\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 int 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}

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.