[Tex/LaTex] Combine minted and tcolorbox

environmentsmintedtcolorbox

I have the idea to combine minted with tcolorbox.

At first, I have created my own minted environments:

\newminted[mycsharp]{csharp}{tabsize=2,fontsize=\footnotesize}
\newminted[myjson]{js}{tabsize=2,fontsize=\footnotesize}
\newminted[myxml]{xml}{tabsize=2,fontsize=\footnotesize}
\newminted[myshell]{shell-session}{tabsize=2,fontsize=\footnotesize}
\newminted[mycode]{text}{tabsize=2,fontsize=\footnotesize}

With them I do highlight my code:

\begin{myshell}
[root@localhost ~]# vi /etc/sysconfig/network
\end{myshell}
\captionof{listing}{Load network configuration with vi}

So I get the following:
Example output

Now I want to have a nice frame around. Since minted is not so powerful here, I want to use tcolorbox, so I did this:

\begin{tcolorbox}
\begin{myshell}
[root@localhost ~]# vi /etc/sysconfig/network
\end{myshell}
\end{tcolorbox}
\captionof{listing}{Load network configuration with vi}

And I get this one here:

Example #2

This is nice, but not what I want. After looking in the package documentation of tcolorbox I found out that it supports titles:

Exmaple #3

It's getting better. After reading some more, tcolorbox also has supported for auto numbered titles and a "list of"-support. Unfortunately the provided examples in the documentation do not work, but that's another question.

Okay, now. What do I want to have?

I want to have something like:

\begin{listingsbox}{myshell}{A nice title}
[root@localhost ~]# vi /etc/sysconfig/network
\end{listingsbox}

to get the following output:

Final example

Unfortunately I'm not able to write this by self. I tried it using the \newenvironment macro, but after googeling, it is not easy to integrate minted into a new environment.

Can anyone help me out here?

Best Answer

I had some trouble setting up minted with pygments on Windows, but now it seems to run. So, this is my first minted text - so, please, forgive me, if I'm doing some nonsense here. Nevertheless, I have found something which may be a ground to build on. The following code uses not exactly your syntax, but creates a myshellbox instead. Basically, I use the listings features of tcolorbox and trick minted in at some point:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{minted}

\makeatletter
\tcbset{minted language/.store in=\kvtcb@minted@language}

\def\tcbuselistinglisting{%
  \toks@=\expandafter{\kvtcb@listingoptions}%
  \edef\tcb@temp{\noexpand\inputminted[\the\toks@]}%
  \tcb@temp{\kvtcb@minted@language}{\kvtcb@listingfile}%
}%
\makeatother

\begin{document}

\section{Test}

\newtcblisting[auto counter,number within=section,
  list inside=mypyg]{myshellbox}[2][]{%
  title={Listing \thetcbcounter: #2},
  list entry={\protect\numberline{\thetcbcounter}#2},
  minted language=shell-session,
  listing options={tabsize=2,fontsize=\footnotesize},
  listing only,
  enhanced,colframe=red!50!black,drop fuzzy shadow,
  #1}

\begin{myshellbox}{Load network configuration with vi}
[root@localhost ~]# vi /etc/sysconfig/network
\end{myshellbox}

\begin{myshellbox}{Something else}
[root@localhost ~]# vi /etc/sysconfig/something
\end{myshellbox}

\tcblistof{mypyg}{My Listings}

\end{document}

enter image description here

EDIT: It should me mentioned that the created myshellbox environment takes an optional parameter, where further options can be put in, e.g. minted language=<something> or listing option=<something>.

EDIT: The second solution is a combination of my first solution with Marco Daniel's answer:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{minted}

\newminted[mycsharp]{csharp}{tabsize=2,fontsize=\footnotesize}
\newminted[myjson]{js}{tabsize=2,fontsize=\footnotesize}
\newminted[myxml]{xml}{tabsize=2,fontsize=\footnotesize}
\newminted[myshell]{shell-session}{tabsize=2,fontsize=\footnotesize}
\newminted[mycode]{text}{tabsize=2,fontsize=\footnotesize}

\newtcolorbox[auto counter,number within=section,
  list inside=mypyg]{mintedbox}[2][]{%
  title={Listing \thetcbcounter: #2},
  list entry={\protect\numberline{\thetcbcounter}#2},
  enhanced,colframe=red!50!black,drop fuzzy shadow,#1}

\newenvironment{listingsbox}[3][]
 {%
   \def\listingsboxenvironment{#2}%save the environments
   \VerbatimEnvironment%
   \begin{mintedbox}[#1]{#3}%
     \begin{\listingsboxenvironment}}%
 {%
  \end{\listingsboxenvironment}%
  \end{mintedbox}%
}

\begin{document}

\section{Test}

\begin{listingsbox}{myshell}{A nice title}
[root@localhost ~]# vi /etc/sysconfig/network
\end{listingsbox}

\begin{listingsbox}{myshell}{Something else}
[root@localhost ~]# vi /etc/sysconfig/something
\end{listingsbox}

\begin{listingsbox}{myxml}{XML box}
<hello>World</hello>
\end{listingsbox}

\tcblistof{mypyg}{My Listings}

\end{document}

enter image description here

Update: Newer versions of tcolorbox have integrated support for minted. Therefore, a more elegant solution with the same output is possible now. I also added a macro \mynewminted which creates a style for tcolorbox plus an equally named minted environment, if this is needed somewhere else.

\documentclass{article}
\usepackage[many,minted]{tcolorbox}% version 3.03 or better

\newcommand{\mynewminted}[3]{%
  \newminted[#1]{#2}{#3}%
  \tcbset{myminted/#1/.style={minted language=#2,minted options={#3}}}}

\mynewminted{mycsharp}{csharp}{tabsize=2,fontsize=\footnotesize}
\mynewminted{myjson}{js}{tabsize=2,fontsize=\footnotesize}
\mynewminted{myxml}{xml}{tabsize=2,fontsize=\footnotesize}
\mynewminted{myshell}{shell-session}{tabsize=2,fontsize=\footnotesize}
\mynewminted{mycode}{text}{tabsize=2,fontsize=\footnotesize}

\newtcblisting[auto counter,number within=section,
  list inside=mypyg]{listingsbox}[3][]{%
  listing only,title={Listing \thetcbcounter: #3},
  list entry={\protect\numberline{\thetcbcounter}#3},
  enhanced,colframe=red!50!black,drop fuzzy shadow,myminted/#2,#1}

\begin{document}

\section{Test}

\begin{listingsbox}{myshell}{A nice title}
[root@localhost ~]# vi /etc/sysconfig/network
\end{listingsbox}

\begin{listingsbox}{myshell}{Something else}
[root@localhost ~]# vi /etc/sysconfig/something
\end{listingsbox}

\begin{listingsbox}{myxml}{XML box}
<hello>World</hello>
\end{listingsbox}

\tcblistof{mypyg}{My Listings}

\end{document}
Related Question