# Combine minted and tcolorbox

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}


So I get the following:

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}


And I get this one here:

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

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:

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?

• You've mentioned problems with 'auto numbered titles' and 'list of'. I used both of them in my answer for demonstration. – Thomas F. Sturm Jul 19 '13 at 13:20

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,
#1}

[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}


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},

\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}


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},

\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}

• Currently I get Unknown option 'most' for package 'tcolorbox'. I have a miktex 2.9 running. I'm trying to reinstall it. Maybe something went wrong. – Manuel Rauber Jul 22 '13 at 6:32
• Okay, I had an older version of the package installed. I updated it and get following errors: Missing number, treated as zero \newtcblisting[a You can't use macro parameter character #' in vertical mode \newtcblisting[a. Unfortunately I get this error too, when I try to compile your example :/ – Manuel Rauber Jul 22 '13 at 6:56
• Are you sure to have tcolorbox 2.40? Your logfile should contain a line Package: tcolorbox 2013/07/15 version 2.40 text color boxes. – Thomas F. Sturm Jul 22 '13 at 7:13
• Strange. At first I had 2.30, after updating with miktex, it installed 2.33 and now it shows a message box No updates available. It's update mirror's version is from 9. July 2013 - maybe they are outdated? miktex.org/packages/tcolorbox – Manuel Rauber Jul 22 '13 at 9:09
• Okay, I updated the package manually and it's working! The best would be to combine your solution with Marco Daniel's one. I like the idea to write \begin{listingbox}{myshell}{Title} :) – Manuel Rauber Jul 22 '13 at 9:29

Here a first suggestion with your requirements. The new environment listingsbox has two mandatory arguments whereby the first argument is a predefined minted environment and the second argument is the caption:

\documentclass{article}
\usepackage{caption}
\usepackage{xcolor}
\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}

\tcbset{
texexp/.style={colframe=red!50!yellow!50!black, colback=red!50!yellow!5!white,
coltitle=red!50!yellow!3!white,
fonttitle=\small\sffamily\bfseries, fontupper=\small, fontlower=\small}, example/.style 2 args={texexp,
title={Example \refstepcounter{texexp}\label{#2}\thetexexp: #1}},
}
\newenvironment{listingsbox}[2]
{%
\def\listingsboxenvironment{#1}%save the environments
\captionsetup{skip=0pt,font={color=white}}%
\VerbatimEnvironment%
\begin{tcolorbox}[title=\captionof{listing}{#2}]%
\begin{\listingsboxenvironment}}%
{%
\end{\listingsboxenvironment}%
\end{tcolorbox}%
}
\begin{document}

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

\end{document}


• You told me once before to consider minted ;-) After some first steps, it looks like it could be possible to have a kind of drop in replacement for listings` in my package. Your solution here seems not to need an auxiliary file which is quite nice. – Thomas F. Sturm Jul 19 '13 at 13:10