# Combine minted and tcolorbox for code from file (inputminted?)

Thanks to Thomas F. Sturm and Gonzalo Medina I have a nice looking environment for code listings with appropriate ref functionalities. However, I just cannot figure out a way to make the defined listingsbox use a file. I know this this normally works with \inputminted but I do not know how to acieve the same with these special environments.

Any ideas would be gladly apreciated, thanks :-)

Edit: I haven't provided a MWE, I'm stuck at exactly this solution and I fail to edit it to use \tcbinputlisting :-(

• Well, could you please post a version of your document or shall we glue the examples of T.F Sturm and Gonzalo Medina together? Is tcbinputlisting not working? – user31729 Apr 15 '16 at 14:03

UPDATE: I've looked at this question for some hours, checking the tcolorbox and cleveref documentation (I even asked here for something related to this, although the MWE I initially did was a mess and I deleted the question afterwards), but @cfr gave me clues and finally I found a solution to your problem.

You can create and use your own counters, and the tcolorbox package can use them by giving their names when creating the box structure (see the commands \newtcolorbox, \newtcbinputlisting and \newtcblisting in pasted file mwe.tex). Inside the box, e.g. the title property, you have to use \thetcbcounter to retreive the value of the counter you've just linked. And regarding cross-referencing, \crefname is your best friend (see those commands in the given mwe.tex file).

For more information, check the tcolorbox and cleveref documentation.

Here is the working solution (file mwe.tex):

\documentclass{article}

\newcounter{commentCount}
\newcounter{filePrg}
\newcounter{inputPrg}

\usepackage{geometry}
\geometry{paper=letterpaper,margin=2cm}

\usepackage{ifthen}

\usepackage{fontawesome}

\usepackage[dvipsnames]{xcolor}

\usepackage{tabularx}
\newcolumntype{\CeX}{>{\centering\let\newline\\\arraybackslash}X}%
\newcommand{\TwoSymbolsAndText}[3]{%
\begin{tabularx}{\textwidth}{c\CeX c}%
#1 & #2 & #3
\end{tabularx}%
}

\usepackage{minted}

\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{minted}

\newtcolorbox[use counter=commentCount, number format=\arabic]{commentBox}[2]{
size=title,
arc=1.5mm,
breakable,
enhanced jigsaw,
colframe=Black,
coltitle=White,
boxrule=0.5mm,
colback=White,
coltext=Black,
title=\TwoSymbolsAndText{\faCheck}{%
\textbf{Comment \thetcbcounter}\ifthenelse{\equal{#1}{}}{}{\textbf{:} \textit{#1}}%
}{\faCheck},
label=comment:#2,
}

\newtcbinputlisting[use counter=filePrg, number format=\arabic]{\codeFromFile}[4]{%
listing engine=minted,
minted language=#1,
listing file={#2},
minted options={autogobble,linenos,breaklines},
listing only,
size=title,
arc=1.5mm,
breakable,
enhanced jigsaw,
colframe=brown,
coltitle=White,
boxrule=0.5mm,
colback=white,
coltext=Black,
title=\TwoSymbolsAndText{\faCode}{%
\textbf{File program \thetcbcounter}\ifthenelse{\equal{#3}{}}{}{\textbf{:} \textit{#3}}%
}{\faCode},
label=filePrg:#4
}

\newtcblisting[use counter=inputPrg, number format=\arabic]{codeInput}[3]{
listing engine=minted,
minted language=#1,
minted options={autogobble,linenos,breaklines},
listing only,
size=title,
arc=1.5mm,
breakable,
enhanced jigsaw,
colframe=brown,
coltitle=White,
boxrule=0.5mm,
colback=white,
coltext=Black,
title=\TwoSymbolsAndText{\faCode}{%
\textbf{Input program \thetcbcounter}\ifthenelse{\equal{#2}{}}{}{\textbf{:} \textit{#2}}%
}{\faCode},
label=inputPrg:#3
}

\usepackage{cleveref}
\crefname{filePrg}{file program}{file programs}
\crefname{inputPrg}{input program}{input programs}

\begin{document}
\begin{commentBox}{My first comment}{first}
My first comment here! \faTree
\end{commentBox}

\begin{codeInput}{c}{A welcome program.}{code01}
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
\end{codeInput}

\codeFromFile{java}{HelloWorld.java}{Another welcome program.}{code01}

See \cref{comment:first} for details. Also, look for \cref{inputPrg:code01} and \cref{filePrg:code01} for further assistance.
\end{document}


This is the HelloWorld.java file used by mwe.tex (if you're running this example, make sure to have this file in the same folder of mwe.tex):

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}


I compiled it using latexmk -xelatex -shell-escape mwe.tex.

And here is the output of the compiling process:

Hope this helps!

• Oh, good - you found it! I looked for your question again, but couldn't find it ;). – cfr Apr 17 '16 at 3:49
• @cfr Sorry, I deleted it because I just copied from another LaTeX file (in fact I had the same problem as the OP), and realized I didn't include many important commands, so the MWE wouldn't have run :( – user101590 Apr 17 '16 at 3:52
• No problem. You found the bit anyway! – cfr Apr 17 '16 at 4:02
• Thanks so much, this solution is great :-) However, while I was customizing it, I noticed that I am not able to build a list of listings. In the solution I posted a simple \tcblistof{mypyg}{My Listings} was enough for that because of the list inside=mypyg argument of the \newtcolorbox. I cannot simply use the counter, i.e. \tcblistof{filePrg}{My Listings}, to get such a list. Any ideas? – Sickboy Apr 18 '16 at 10:54
• @Sickboy Well, the counter was to demostrate the customization you can have. Have you tried with auto counter on the options (instead of use counter=)? Also, label type can help you to include the codes in the listings list, but make sure you have cleveref loaded and using already known types, such as theorem, algorithm, listing, etc. It should be interesting if one could make a table of personalized elements made from tcolorboxes, though :) – user101590 Apr 20 '16 at 5:19

So, I figured the \tcblistof thing out. It was the title with an image causing the problem. There are two approaches to this issues:

1. Do not use this image in the title definition, but in an overlay. This keeps the title simple and suitable for the list of listings.
2. Use a list entry to define a title for the list of listings.

Here's the solution suited to my needs (and shortened). Tanks so much to JBFWP286 again! :-)

\documentclass{article}

\newcounter{filePrg}

\usepackage{geometry}
\geometry{paper=letterpaper,margin=2cm}

\usepackage[dvipsnames]{xcolor}

\usepackage{minted}

\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{minted}

\newtcbinputlisting[use counter=filePrg, number within=section, list inside=mypyg]{\codeFromFile}[4]{%
listing engine=minted,
minted language=#1,
listing file={#2},
minted options={autogobble,linenos,breaklines},
listing only,
size=title,
breakable,
enhanced jigsaw,
%arc=1.5mm,
%colframe=brown,
%coltitle=White,
%boxrule=0.5mm,
%colback=white,
%coltext=Black,
title={Listing \thetcbcounter : #3 \hfill%
\smash{\raisebox{-11pt}{\includegraphics[width=1cm,height=1cm]{images/code2.png}}}},
list entry=Listing~\thetcbcounter : #3,
%OR next two lines
%title={Listing \thetcbcounter : #3},
%overlay={\node[anchor=north east,outer sep=-9pt] at ([xshift=-25pt]frame.north east) {\includegraphics[width=1cm,height=1cm]{images/code2.png}}; },
label=lst:#4
}

%http://tex.stackexchange.com/questions/217489/numbering-tcolorbox-toc
\makeatletter % no indent for entries
\renewcommand{\l@tcolorbox}{\@dottedtocline{1}{0pt}{2.3em}}
\makeatother

\usepackage{hyperref}

\crefname{filePrg}{Listing}{Listings}

\begin{document}
\section{Section 1}
\codeFromFile{java}{listings/HelloWorld.java}{Another welcome program.}{code01}

See \cref{lst:code01} for details.

\tcblistof{mypyg}{My Listings}
\end{document}


I just ran into the problem of too long titles and my solution just went out off the box. So I reconsidered JBFWP286's answer and tweaked it here and there (I obviously changed the style of my boxes as well). Here's a MWE of it:

\documentclass{book}
\usepackage[utf8]{inputenc}

\usepackage{tabularx}
\newcolumntype{\LeX}{>{\let\newline\\\arraybackslash}X}%
\newcommand{\OneSymbolAndText}[2]{%
\begin{tabularx}{340pt}{l\LeX}%should normally be \textwidth instead of 340pt but fills out the box nicer this way
#1 & #2
\end{tabularx}%
}

\usepackage[most]{tcolorbox}

\newtcolorbox[auto counter,number within=chapter]{Information}[4][]{%
breakable,
enhanced,
arc=0mm,
colback=gray,
colframe=gray,
borderline west={3pt}{0pt}{blue}, % straigh vertical line at the left edge
sharp corners, % No rounded corners
boxrule=0pt, % no real frame,
size=title,
bottom=6pt,
leftupper=12pt,
lefttitle=35pt,
toptitle=6pt,
top=4pt,
list entry=Information~\thetcbcounter : #1,
%fonttitle=\scshape,
fonttitle={\large\bfseries},
coltitle={black},  % Black colour for title
title=\hspace{-36pt}\OneSymbolAndText{\includegraphics[width=1cm,height=1cm]{boximages/infoBulle/info}}{%
\vspace{#4}Information \thetcbcounter : #2},
#1,
label=info:#3
}

\crefname{tcb@cnt@Information}{Information}{Informationen}
\Crefname{tcb@cnt@Information}{Information}{Informationen}

\begin{document}
\begin{Information}{Really really really I mean really long loooooong Title}{labelInf}{-27.5pt}
Test123
\end{Information}
siehe \cref{info:labelInf}

\end{document}


Building upon the prior answers here, I would like to add a more general MWE. Utilizing the all option, tcolorbox automatically loads minted.

Furthermore you can set you global minted settings as usual with \setminted.

With the following \codeFromFile command, you can additionally override the minted and tcolorbox settings by individual settings via

\codeFromFile([minted settings]){language}{file}{Title}{label}{additional tcolorbox settings}

\documentclass[paper=letter]{scrartcl}

\usepackage[all]{tcolorbox}

% Arbitrary minted settings
\setminted[latex]{
autogobble,
fontsize=\scriptsize,
linenos=true,
breaklines}

% Arbitrary tcolorbox settings
\tcbset{
list entry=Listing : #4,
fonttitle=\bfseries,
size=fbox,
arc=1mm,
colback=white}

\newtcbinputlisting[list inside=mypyg]{\codeFromFile}[6][]{%
listing engine=minted,
minted options={#1},
minted language=#2,
listing file={#3},
title={\centering #4},
label=#5,
listing only,
breakable,
#6
}{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
%
\codeFromFile[fontsize=\large]{latex}{minimal.tex}{Program}{code01}{width=10cm,colframe=red!75!black}
\codeFromFile{latex}{minimal.tex}{Another program}{code02}{}
%
\tcblistof{mypyg}{My Listings}
%
\end{document}


P.s.: I found no (working) way to include a second optional argument for the tcb settings. Afaik you need to redefine \newtcbinputlisting using e.g. xparse's \DeclareDocumentCommand... Hence just append a {} if you don't hand over additional tcolorbox settings (see "Another brogram" box).