1

I'm try to highlight source code with colored background, but I face a bit error.

using listings

enter image description here

first image is created with listings only and that is what I need. You can see on the last image that basicstyle is not applied (still has black text color).

this is the tcolorbox + listings code (to generate the last image).

\documentclass{article}

\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary
    {
        breakable,
        listings
    }

\lstset{%
  language=[LaTeX]{TeX}
}

\begin{document}

    \begin{tcblisting}
        {
            listing only,
            left                        = 3em,
            boxrule                     = 0.1mm,
            breakable,
            parbox                      = true,
            colback                     = gray,
            colframe                    = black,
            listing options             = 
                {
                    basicstyle          = \normalsize\ttfamily\color{white},
                    keywordstyle        = \color{white},
                    stringstyle         = \color{green},
                    identifierstyle     = \color{yellow},
                    commentstyle        = \color{black},
                    numbers             = left,
                    numbersep           = 1em,
                    columns             = fullflexible,
                    breaklines          = true,
                    breakindent         = 0pt,
                    tabsize             = 3,
                    showstringspaces    = false,
                    numberstyle         = \small\color{white},
                },
        }
\documentclass{article}

\usepackage{xcolor}

\lstset{%
    language=[LaTeX]{TeX},
    basicstyle          = \normalsize\ttfamily\color{white},
    keywordstyle        = \color{white},
    stringstyle         = \color{green},
    identifierstyle     = \color{yellow},
    commentstyle        = \color{black},
    numbers             = left,
    numbersep           = 1em,
    columns             = fullflexible,
    breaklines          = true,
    breakindent         = 0pt,
    tabsize             = 3,
    showstringspaces    = false,
    numberstyle         = \small\color{gray},
    backgroundcolor     = \color{gray},
}

\begin{document}


Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.

Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna,
vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse
ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna.
Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.



\end{document}

    \end{tcblisting}


\end{document}
1

The color of the listings basicstyle should match with the text color (coltext or colupper) of the box, because the text color of the box is restored during breaks (to cope with other possible problems).

So, use the following:

...
\begin{tcblisting}
    {
        listing only,
        left                        = 3em,
        boxrule                     = 0.1mm,
        breakable,
        parbox                      = true,
        colback                     = gray,
        colframe                    = black,
        coltext                     = white,% <--- !!
        listing options             = 
            {
                basicstyle          = \normalsize\ttfamily\color{white},% <--- match
...

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.