2

I am trying to present some Python-code in LaTeX using listings and \lstinputlisting, but I have some problems with using backgroundcolor in \lstset. I can't really describe it, so I have included a picture:

enter image description here

The gray block comes from backgroundcolor in \lstset. As you can see it doesn't completely cover the code, and there's some extra gray text that says "bbrreeaakklliinneess" in between the lines. The gray text can be highlighted and copied, even if I change the color to white. If I remove backgroundcolor from \lstset the text goes away, but then the background is white. The code I used to define \lstset is as follows:

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{mygray}{rgb}{0.9,0.9,0.9}
\definecolor{LightGray}{gray}{0.95}
\lstset{frame=tb,
    language=Python,
    aboveskip=3mm,
    belowskip=3mm,
    showstringspaces=false,
    columns=flexible,
    basicstyle={\small\ttfamily},
    numbers=none,
    numberstyle=\tiny\color{gray},
    keywordstyle=\color{blue},
    commentstyle=\color{dkgreen},
    stringstyle=\color{mauve},
    backgroundcolor=\color{mygray}
    breaklines=true,
    breakatwhitespace=true,
    tabsize=3
}

Is there any way I can fix this?

2

Just do not use the following three options for \lstset:

%   breaklines=true, % <================================================
%   breakatwhitespace=true, % <=========================================
%   tabsize=3 % <=======================================================

So with the following complete MWE (package filecontents is used to get a compiling MWE with TeX code and python code):

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.py}
import random
n = 20
to_be_guessed = int(n * random.random()) + 1
guess = 0
while guess != to_be_guessed:
    guess = input("Neue Zahl: ")
    if guess > 0:
        if guess > to_be_guessed:
            print "Zahl zu groß"
        elif guess < to_be_guessed:
            print "Zahl zu klein"
    else:
        print "Schade, Sie geben also auf!"
        break
else:
    print "Glückwunsch! Das war's!"
\end{filecontents*}


\documentclass{scrartcl}

\usepackage{listings}
\usepackage{xcolor}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{mygray}{rgb}{0.9,0.9,0.9}
\definecolor{LightGray}{gray}{0.95}
\lstset{%
    frame=tb,
    language=Python,
    aboveskip=3mm,
    belowskip=3mm,
    showstringspaces=false,
    columns=flexible,
    basicstyle={\small\ttfamily},
    numbers=none,
    numberstyle=\tiny\color{gray},
    keywordstyle=\color{blue},
    commentstyle=\color{dkgreen},
    stringstyle=\color{mauve},
    backgroundcolor=\color{mygray}
%   breaklines=true, % <================================================
%   breakatwhitespace=true, % <=========================================
%   tabsize=3 % <=======================================================
}


\begin{document}

Complete listing:
\lstinputlisting{\jobname.py}

\end{document}

you get the result:

wished result

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.