19

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?

jub0bs
  • 58,916

2 Answers2

14

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}
  • 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 06: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 usemacro 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 06: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 07: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? http://miktex.org/packages/tcolorbox – Manuel Rauber Jul 22 '13 at 09: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 09:29
  • @Raubi Yes, it is possible to combine the solutions. I've updated my answer with such a combination :-) – Thomas F. Sturm Jul 22 '13 at 10:09
  • This is outstanding! I wish I could upvote more than once :) – Manuel Rauber Jul 22 '13 at 11:19
  • Well, this is embarrassing, but could I ask you for one more thing? Well, actually two things: Sometimes I need to place a footmark within the title. Since the title is showed in the listof it does not work. And a labelling support would be nice too. I'm thinking of something like \begin{listingsbox}[title={I'm a nice title\footmarknote!}, label={lst:mycode}, language={myshell}, listofentry={I'm such a nice title!}]{mycode goes here...}. I haven't done LaTeX programming before and currently I'm trying to do this by myself using the xkeyval package. The learning cure is quite steep :D – Manuel Rauber Jul 22 '13 at 11:46
  • @Raubi I've modified my answer. listingsbox is now able to take options which are passed to the underlying tcolorbox. Therefore, you can use \begin{listingsbox}[label={lst:mycode}, after title={\footmarknote},...]{mintenv}{title}. For '...' you may fill anything tcolorbox offers as options. If you have specific problem, you should ask a new question since comments don't have a lot of room. – Thomas F. Sturm Jul 22 '13 at 12:01
  • Yep, you are right, but I think it's currently all working as I want. Thank you very much, Thomas! You saved me hours :-) – Manuel Rauber Jul 22 '13 at 12:18
  • Since some updates this does not work anymore, gettings errors like: Use of \\smash doesn't match its definition ..., Extra }, or forgotten \endgroup .... Unfortunately I don't know, which updates causes the problem – Manuel Rauber May 21 '14 at 07:07
  • Hm... I do not get any error. My system is a week old. I cannot update MikTeX today, because of http://tex.stackexchange.com/questions/179454/miktex-problem-after-last-update . Nevertheless, I updated my answer with a new solution you could try. But I have no idea where the \smash error may come from (and I cannot reproduce the error). – Thomas F. Sturm May 21 '14 at 11:27
  • @Raubi Even after updating today, I do not get any errors ... – Thomas F. Sturm May 22 '14 at 08:58
  • Currently the MikTeX package repository is down. I'm going to try this, after it is available again. Maybe I get those errors because I'm using some other hacks for minted (Unicode, line breaking). – Manuel Rauber May 22 '14 at 12:18
  • I updated my packages and tried out your example, which works well. I tried to update my document with all the needed changes and can't get it to work. I saw, that I'm using srcreprt as document class. Would be so kind and try to change it in your example and see if it works (or it's a fault of my local miktex installation)? If I do it with your examples pdflatex compiles forever – Manuel Rauber Jun 20 '14 at 09:18
  • @Raubi I think I found the problem with scrreprt. The \numberline has to be replaced by \protect\numberline. I've updated my example above and it works with scrreprt now. – Thomas F. Sturm Jun 23 '14 at 06:38
  • Awesome! I hope someday I can stand you are beer for your help! – Manuel Rauber Jun 23 '14 at 07:26
  • TIL trying to use the language signifyer bash results in only incorrect syntax highlighting - you have to instead use shell-session. I guess I should RTFD, but that's just... weird... – fullStackChris May 09 '23 at 17:33
4

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}

enter image description here

Marco Daniel
  • 95,681
  • 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