15 Comments

  1. Hi tom!
    Thanks for your code examples. Have you seen that white lines/stripes between the lines of code? – Do you know a solution for disabling them?

    Best regards!

    • Hi Kai,

      Thanks for your comment. I am aware of the problem. I found the following solution on stackoverflow and modified it slightly adding line numbers:

      \documentclass{minimal}
      \usepackage[pdftex]{xcolor}
      \usepackage[a4paper,hmargin=6cm]{geometry}
      \usepackage{listings}
      \usepackage{framed}
      \lstset{numbers=left,
      stepnumber=1,
      numbersep=1pt,
      numberstyle=\color{black},
      basicstyle=\ttfamily}
      \begin{document}
      \definecolor{shadecolor}{named}{gray}
      \begin{shaded}
      \begin{lstlisting}
       if (a < b)
       {
          printf("A is smaller than  B!\n");
       }
       a = b;
      \end{lstlisting}
      \end{shaded}
      \end{document}

      Best, Tom.

      • Thanks, Tom! – I have seen this on so, too. But the shaded box is (a little bit) ugly… 😐

        I have seen some examples that use “backgroundcolor” for the listings _without_ the white lines. Also I found an answer on tex.sx… But my listings are always striped… 🙁

      • Omar Sweidan

        Sorry, I forgot.

        \documentclass{report}
        \usepackage{xcolor}
        \usepackage{listings}          % for source code snippets.
        \definecolor{pblue}{rgb}{0.13,0.13,1}
        \definecolor{pgreen}{rgb}{0,0.5,0}
        \definecolor{pred}{rgb}{0.9,0,0}
        \definecolor{pgrey}{rgb}{0.46,0.45,0.48}
        \lstdefinestyle{Javastyle}{  
            showspaces=false,
            showtabs=false,
            breaklines=true,
            showstringspaces=false,
            breakatwhitespace=true,
            commentstyle=\color{pgreen},
            keywordstyle=\color{pblue},
            stringstyle=\color{pred},
            basicstyle=\ttfamily\singlespacing,
            moredelim=[il][\textcolor{pgrey}]{\$\$},
            moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%},
            rulecolor= \color{black} 
        }
        
        \begin{document}
        
        %\lstset{style=Javastyle}
        \begin{lstlisting}[language=Java]
        @NonNull
        public static byte[] longToBytes(long x) {
        buffer.clear();
        buffer.putLong(0, x);
        return buffer.array();
        }

        When I remove the % before \lstset{}, I get an error message: Undefined control sequence. \begin{lstlisting}[language=Java]

      • tom

        I can’t run your example. It would be useful if you provided a working example. A quick glance suggests that you have to load the setspace package to use \singlespacing.

        Best, Tom

    • tom

      Hi Omar,

      I removed your code, as there were again some lines missing towards the end. Anyway, as I said before, you’ll have to load the setspace package to use \singlespacing in your style definition:

      \usepackage{setspace}

Leave a Reply