2

I want to have a lstlisting environment in a tcolorbox. The problem is when there is a page break, with breakable, the colour component of the listings option basicstyle is reset after the page break, the text returns to black. The same problem arises when you just set the colour with \color{<color>}, but this can be fixed by restating the colour declaration.

(Although, this seems not to be that easy, since you need to know where the box will break and for some reason the colour won't reset immediately after the break, I fixed this by adding \ \kern-1ex\color{<color>} to add and remove a space when redeclaring, which is a bit of a hack, but this isn't the problem I'm trying to fix.)

This question is related, but the solution (the coltext option of tcolorbox) isn't viable as I don't want all of the text to change colour. I want some text explaining what the code snippet does in the default colour, and then the text in the lstlisting environment should all be whatever colour I set for the basicstyle option.

I also found discussion on this site about a similar issue but it was related to XeLaTeX, I've tried this on TeXLive 2021, TeXLive 2020 (both overleaf) and on MiKTeX where the relevant package versions are

  • tcolorbox 4.51
  • xcolor 2.12
  • listings 1.8d

I'm pretty sure this issue is related to the default colour works as other options, such as basicstyle=\itshape aren't reset upon the page break. Neither are colours in other options, such as keywordstyle=\color{blue}, this works either side of a page break, assuming you set a language and use a valid key word.

MWE:

\documentclass{article}
\usepackage{xcolor}
\usepackage[listings]{tcolorbox}
\tcbuselibrary{breakable}
\usepackage[top=1cm, bottom=1.5cm]{geometry} % decrease margins to make images smaller
\begin{document}
    ~\vspace{23cm}\obeylines
    % Example
    \begin{tcolorbox}[breakable]
    \color{red}
    1 Test
    2 Test
    3 Test % line breaks here
    % \ \kern-1ex\color{red}4 Test  % replace line below with this to get around colour swapping back
    4 Test
    5 Test
    6 Test
    \end{tcolorbox}
    \vspace{21cm}
    % Standard use case:
    \begin{tcolorbox}[breakable]
        Some text explaining what the code does, in the default black colour.
        \begin{lstlisting}[gobble=12, basicstyle=\color{red}, escapechar=|]
            some code, which should all be the same colour
            this line is after the page break and is back to black
            can't redeclare color: \color{red} still black
            can't escape to redeclare: |\color{red}| still black
        \end{lstlisting}
    \end{tcolorbox}
\end{document}

First tcolorbox: tcolorbox split over page, before page break text is red, after page break text is black

Second tcolorbox: tcolorbox split over page, before page break text is red, after page break text is black, this time text includes listing environment demonstrating that you can't change the colour within the verbatim like environment

1 Answer 1

2

You can use the use color stack key. Be aware that is should come before the breakable key and that is can affect spacing:

\documentclass{article}
\usepackage{xcolor}
\usepackage[listings]{tcolorbox}
\tcbuselibrary{breakable}
\usepackage[top=1cm, bottom=1.5cm]{geometry} % decrease margins to make images smaller
\begin{document}
    ~\vspace{23cm}\obeylines
    % Example
    \begin{tcolorbox}[use color stack,breakable]
    \color{red}
    1 Test
    2 Test
    3 Test % line breaks here
    % \ \kern-1ex\color{red}4 Test  % replace line below with this to get around colour swapping back
    4 Test
    5 Test
    6 Test
    \end{tcolorbox}
    \vspace{21cm}
    % Standard use case:
    \begin{tcolorbox}[use color stack,breakable]
        Some text explaining what the code does, in the default black colour.
        \begin{lstlisting}[gobble=12, basicstyle=\color{red}, escapechar=|]
            some code, which should all be the same colour
            this line is after the page break and is back to black
            can't redeclare color: \color{red} still black
            can't escape to redeclare: |\color{red}| still black
        \end{lstlisting}
    \end{tcolorbox}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .