2

I have sort of the same problem as the user benregn in

Listings code snippet has white horizontal lines across it

. But making the numberstyle smaller than the basicstyle does not solve my problem.

Minimum working example:

\documentclass{article}
\usepackage{lipsum}
\usepackage{lstautogobble}
\usepackage{listings}
\lstdefinestyle{myStyle}
{
    tabsize=4,
    autogobble=true,
    mathescape=true,
    numbers=left,
    xleftmargin=9mm,
    framexleftmargin=9mm,
    commentstyle=\color{green!50!black},
    comment=[l]\%\ ,
    backgroundcolor=\color{black!10!white},
    keywordstyle=\color{blue},
    keywords=
    {
        and,
        and,
        case,
        do,
        else,
        elseif,
        end,
        for,
        function,
        if,
        switch,
        to,
        while,
    }
}
\usepackage{xcolor}

\begin{document}
    \lipsum[1]
    \begin{lstlisting}[style=myStyle]
        $abcdefghijklmnopqrstuvwxyz$
        $\left\lfloor \frac{abcdefghijklmnopqrstuvwxyz}{ABCDEFGHIJKLMNOPQRSTUVWXYZ}\right\rfloor$
    \end{lstlisting}
    \lipsum[2]
\end{document}

The result looks like

enter image description here

. How do I fix this problem?

2

This issue is documented in the listings manual.

8.2 Listings with a background colour and LATEX escaped
formulas

At the moment there is only one workaround: • Write your code into an external file <filename>. • Input your code by \lstinputlisting{<filename>} into your document and surround it with a frame generated by \begin{mdframed} . . . \end{mdframed}.

This being said, I tried the shorter route and it appears to work, but no idea if it really does work.

\documentclass{article}
\usepackage{lipsum}
\usepackage{lstautogobble}
\usepackage{listings}
\lstdefinestyle{myStyle}
{
    tabsize=4,
    autogobble=true,
    mathescape=true,
    numbers=left,
    xleftmargin=9mm,
    framexleftmargin=9mm,
    commentstyle=\color{green!50!black},
    comment=[l]\%\ ,
    keywordstyle=\color{blue},
    keywords=
    {
        and,
        and,
        case,
        do,
        else,
        elseif,
        end,
        for,
        function,
        if,
        switch,
        to,
        while,
      }
}
\usepackage{xcolor}

\usepackage{mdframed}

\begin{document}
    \lipsum[1]
\begin{mdframed}[backgroundcolor=black!10!white]
    \begin{lstlisting}[style=myStyle]
        #test&&#$abcdefghijklmnopqrstuvwxyz$
        $\left\lfloor \frac{abcdefghijklmnopqrstuvwxyz}{ABCDEFGHIJKLMNOPQRSTUVWXYZ}\right\rfloor$
    \end{lstlisting}
\end{mdframed}

    \lipsum[2]
\end{document}

enter image description here

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.