[Tex/LaTex] Tcolorbox + minted source code listing that works in beamer

beamerincompatibilitymintedtcolorbox

I copied and modified the code snippet in the accepted answer here to highlight cpp code:

\usepackage{tcolorbox}
\tcbuselibrary{minted,skins}

\newtcblisting{cppcode}[1][]{
    listing engine=minted,
    colback=bg,
    colframe=black!70,
    listing only,
    minted style=colorful,
    minted language=cpp,
    minted options={linenos=true,numbersep=3mm,texcl=true,#1},
    left=5mm,enhanced,
    overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}}
}
\definecolor{bg}{rgb}{0.85,0.85,0.85}

Then, I use it in a Beamer frame like this:

\begin{frame}[fragile]
\frametitle{Title}
\framesubtitle{Subtitle}

\begin{cppcode}    
int main() {
    int x, y; 
}
\end{cppcode}

\end{frame}

However, only an empty box with a gray margin shows up, with no code in it.

I have the following beamer class and theme:

\documentclass[xcolor=table]{beamer}

\usetheme{CambridgeUS}
\usecolortheme{beaver}

How can I make a similar tcolorbox code listing show up in beamer? Ideally, I would also like it to be breakable across frames.

Update: the author suggested in a comment to change the name of bg. I changed it to bgg, and now the background is also (a lighter) gray, but the code still does not show up.

Best Answer

It turns out that the name bg for the background color conflicts with beamer. Just name it to something else

\newtcblisting{cppcode}[1][]{
    listing engine=minted,
    colback=cppcodebg,
    colframe=black!70,
    listing only,
    minted style=colorful,
    minted language=cpp,
    minted options={linenos=true,numbersep=3mm,texcl=true,#1},
    left=5mm,enhanced,
    overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}}
}
\definecolor{cppcodebg}{rgb}{0.85,0.85,0.85}

enter image description here

Minimal example, to be run with

pdflatex -shell-escape test

or minted can not run pygmentize:

\documentclass{beamer}

\usepackage{tcolorbox}
\tcbuselibrary{minted,skins}

\newtcblisting{cppcode}[1][]{
    listing engine=minted,
    colback=cppcodebg,
    colframe=black!70,
    listing only,
    minted style=colorful,
    minted language=cpp,
    minted options={linenos=true,numbersep=3mm,texcl=true,#1},
    left=5mm,enhanced,
    overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}}
}
\definecolor{cppcodebg}{rgb}{0.85,0.85,0.85}

\begin{document}

\begin{frame}[fragile]
\frametitle{Title}
\framesubtitle{Subtitle}

\begin{cppcode}    
int main() {
    int x, y; 
}
\end{cppcode}

\end{frame}

\end{document}
Related Question