latex

Using different tcolorbox colors with minted, depending upon the language


I am using minted to display my code in a LaTeX document. Those codes are displayed inside a tcolorbox. For that, I am using this :

\documentclass{article}

\usepackage{minted}
\usepackage{tcolorbox}

\definecolor{colorLazoYellow1Light}{RGB}{255,248,230}
\definecolor{colorLazoBlue1Light}{RGB}{227,243,255}
\BeforeBeginEnvironment{minted}{\begin{tcolorbox}[colback=colorLazoYellow1Light]}
\AfterEndEnvironment{minted}{\end{tcolorbox}}
\newminted{python}{linenos,numbersep=5pt}
\newmintinline{python}{}
\newminted{ocaml}{linenos,numbersep=5pt}
\newmintinline{ocaml}{}

\begin{document}

\begin{pythoncode}
def f(x):
    return x + 1
\end{pythoncode}

\begin{ocamlcode}
let f x = x + 1
\end{ocamlcode}

\end{document}

But now, I would like to use a different color for the box, depending upon the language I am using. How can I do that?

Best regards


Solution

  • Use \newtcblisting to define new code environments:

    \documentclass{article}
    
    \usepackage{tcolorbox}
    \tcbuselibrary{minted}
    
    \definecolor{colorLazoYellow1Light}{RGB}{255,248,230}
    \definecolor{colorLazoBlue1Light}{RGB}{227,243,255}
    \newtcblisting{pythoncode}{colback=colorLazoYellow1Light,listing only,listing engine=minted,minted language=python}
    \newtcblisting{ocamlcode}{colback=colorLazoBlue1Light,listing only,listing engine=minted,minted language=ocaml}
    
    \begin{document}
    
    \begin{pythoncode}
    def f(x):
        return x + 1
    \end{pythoncode}
    
    \begin{ocamlcode}
    let f x = x + 1
    \end{ocamlcode}
    
    \end{document}