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:

    listing engine=minted,minted language=java,minted style=colorful,
    listing file={#2},
    title=Listing (\thetcbcounter) of #2,
listing only,breakable,#1}


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


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' – Dr Krishnakumar Gopalakrishnan 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. – Dr Krishnakumar Gopalakrishnan 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

  listing engine=minted, minted language=java, minted style=colorful,
  listing file={#2}, listing only,

upon which your MWE now gives

enter image description here

as desired.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.