[Tex/LaTex] How to make listings look like minted

codehighlightinglistingsminted

Up until now I used the listings package for my code-listings. Today, I tried minted and I'm pleased with the default look. Since I'd have a lot to do to switch from listings to minted at this stage of my thesis, I'd like to mimic the look.

In the following picture, you see the same source-code in listingz and minted.
Could you help me identify the appropriate listings settings to make the left example look like the right one? I can figure out the colours myself, but the rest (spacing, font, etc.) puzzles me.

enter image description here

There are my styling-settings for listing:

% javascript code listing
\newcommand{\inputjscode}[2][]{{%
    \renewcommand{\lstlistingname}{JavaScript Code}%
    \renewcommand{\addcontentsline}[3]{\oldaddcontentsline{los}{##2}{##3\enskip(\lstlistingname)}}%
        \lstset{
            tabsize=2,
            lineskip=-1pt,
            rulecolor=,
            basicstyle=\footnotesize,
            columns=fullflexible,
            upquote=true,
            aboveskip={1.3\baselineskip},
            columns=fixed,
            showstringspaces=false,
            extendedchars=true,
            breaklines=false,
            prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
            escapechar=@,
            frame=single,
            showtabs=false,
            showspaces=false,
            showstringspaces=false,
            identifierstyle=\ttfamily,
            keywordstyle=\color[rgb]{1.0,0,0},
            keywordstyle=[1]\color[rgb]{0,0,0.75},
            keywordstyle=[2]\color[rgb]{0.5,0.0,0.0},
            keywordstyle=[3]\color[rgb]{0.127,0.427,0.514},
            keywordstyle=[4]\color[rgb]{0.4,0.4,0.4},
            commentstyle=\color[rgb]{0.133,0.545,0.133},
            stringstyle=\color[rgb]{0.639,0.082,0.082},
        }
    \lstinputlisting[
    numbers=left, 
    frame=single, 
    stepnumber=1, 
    inputencoding=latin1, 
    language=JavaScript, 
    basicstyle=\scriptsize,
    #1]{#2}%
}}

Best Answer

I was able to replicate it pretty closely using listings.enter image description here

Ultimately, the part that made it tricky is that you have redundant commands in your \lstset (and presumably also where you defined keywords for Javascript): for example, the colors you provided for different keyword styles were overridden by others that were already defined.

I started with a listings language definition for Javascript, which I got from this TeX.sx post; however, we'll be modifying the colors, so I removed the definitions we won't use.

\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
  ndkeywords={class, export, boolean, throw, implements, import, this},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  morestring=[b]',
  morestring=[b]"
}

Then, we define the style, which comes from setting the colors (and italics for comments) in keywordstyle, commentstyle, etc., along with a few spacing adjustments. I always alphabetize my \lstset call so that I can quickly figure out whether I've already added a command rather than duplicating it.

\lstset{
    aboveskip={1.3\baselineskip},
    basicstyle=\scriptsize\ttfamily\linespread{4},
    breaklines=false,
    columns=flexible,
    commentstyle=\color[rgb]{0.127,0.427,0.514}\ttfamily\itshape,
    escapechar=@,
    extendedchars=true,
    frame=single,
    identifierstyle=\color{black},
    inputencoding=latin1,
    keywordstyle=\color[HTML]{228B22}\bfseries,
    language=JavaScript,
    ndkeywordstyle=\color[HTML]{228B22}\bfseries,
    numbers=left,
    numberstyle=\tiny,
    prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
    showstringspaces=false,
    stringstyle=\color[rgb]{0.639,0.082,0.082}\ttfamily,
    upquote=true
}

Finally, minted highlights the numbers in gray. This TeX.sx answer shows us how to do it in listings:

\definecolor{darkgray}{rgb}{.4,.4,.4}
\lstset{literate=%
   *{0}{{{\color{darkgray}0}}}1
    {1}{{{\color{darkgray}1}}}1
    {2}{{{\color{darkgray}2}}}1
%   ... and so on
    {9}{{{\color{darkgray}9}}}1
}

Here's my whole working example.

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

\definecolor{darkgray}{rgb}{.4,.4,.4}

\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
  ndkeywords={class, export, boolean, throw, implements, import, this},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  morestring=[b]',
  morestring=[b]"
}

\lstset{
    aboveskip={1.3\baselineskip},
    basicstyle=\scriptsize\ttfamily\linespread{4},
    breaklines=false,
    columns=flexible,
    commentstyle=\color[rgb]{0.127,0.427,0.514}\ttfamily\itshape,
    escapechar=@,
    extendedchars=true,
    frame=single,
    identifierstyle=\color{black},
    inputencoding=latin1,
    keywordstyle=\color[HTML]{228B22}\bfseries,
    language=JavaScript,
    ndkeywordstyle=\color[HTML]{228B22}\bfseries,
    numbers=left,
    numberstyle=\tiny,
    prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
    stringstyle=\color[rgb]{0.639,0.082,0.082}\ttfamily,
    upquote=true,
    showstringspaces=false,
}

\lstset{literate=%
   *{0}{{{\color{darkgray}0}}}1
    {1}{{{\color{darkgray}1}}}1
    {2}{{{\color{darkgray}2}}}1
    {3}{{{\color{darkgray}3}}}1
    {4}{{{\color{darkgray}4}}}1
    {5}{{{\color{darkgray}5}}}1
    {6}{{{\color{darkgray}6}}}1
    {7}{{{\color{darkgray}7}}}1
    {8}{{{\color{darkgray}8}}}1
    {9}{{{\color{darkgray}9}}}1
} 

\begin{document}
\lstinputlisting{filename.js}
\end{document}