# Problems setting up minted and tcolorbox to include source code and set title

For my PhD thesis, I am using minted package for source code listings. I'd like to have a custom command \mylisting something along the lines of

\mylisting[Name in title]{filename of source code}


or \mylisting{Hello world}{hello_world.java}

I tried reading the tcolorbox manual, and the closest I got to was the following minimal non-working example:

\documentclass{article}
\usepackage[most,minted]{tcolorbox}
\newtcbinputlisting{\mylisting}[2][]{%
listing engine=minted,minted language=java,minted style=colorful,
listing file={#2},
title=Listing (\thetcbcounter) of #2,
listing only,breakable,#1}

\begin{document}
\section{Test}

\mylisting[Hello world]{hello_world.java}  % problematic line!!
\mylisting{Hello world}{hello_world.java}  % problematic line!!

\end{document}


But due to my lack of knowledge in TeX, this fails and produces lots of errors. I particularly found it hard to even include the source code from the hello_world.java file located in the same directory as the mnwe.

public class HelloWorld {
// A 'Hello World' in Java
public static void main(String[] args) {
System.out.println("Hello World!");
}
}


What might be the problem here, and how do I solve this?

• Do you want something like \newtcbinputlisting{\mylisting}[2][]{% listing engine=minted,minted language=java,minted style=colorful, listing file={#2}, title={#1}, listing only,breakable}? The code in the question has two problems: (1) The optional argument is passed on as option to \newtcbinputlisting, so if we write \mylisting[Hello world]{hello_world.java} the macro tries to call a colorbox with the option Hello World. But an option like this does not exist. So you probably wanted title={#1}. ... – moewe May 16 '18 at 15:11
• @moewe, Yes. I have tried that and get the error (Package pgfkeys) I do not know the key '/tcb/Hello world' – Krishna May 16 '18 at 15:14
• ... (2) As posted the macro tries to write the file name given in its second (mandatory) argument as title. But the file name has an underscore in it, that causes problems. – moewe May 16 '18 at 15:14
• Did you really try with \newtcbinputlisting{\mylisting}[2][]{% listing engine=minted,minted language=java,minted style=colorful, listing file={#2}, title={#1}, listing only, breakable}? – moewe May 16 '18 at 15:15
• @moewe, I am sorry. That worked! I am sorry for this issue. – Krishna May 16 '18 at 15:20

There are two issues with the code

1. The optional argument is simply passed on to tcolorbox as a general argument. It seems to be used, however, as a title argument, so #1 should not be written bare, but as value of the title option.
2. title=Listing (\thetcbcounter) of #2 actually uses the second argument (the file name) as title. The filename in your example contains an underscore _, and that has a special meaning.

In the comments it emerged you want

\newtcbinputlisting{\mylisting}[2][]{%
listing engine=minted, minted language=java, minted style=colorful,
listing file={#2}, listing only,
title={#1},
breakable}


upon which your MWE now gives

as desired.