9

Could someone kindly let me know how I can pass one option to minted options and one to tcolorbox at the same time to a pythoncode tcolorbox listing? I mean, I want some options get passed to minted options, as shown by #1 in the MWE below and some get passed to tcolorbox, as shown by #2.

\documentclass{book}    
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}    

\usepackage{amsmath}    
\usepackage{tcolorbox}    
\tcbuselibrary{minted,skins,breakable}    

\usepackage{hyperref}       
\hypersetup{ %    
    pdfborder = {0 0 0},    
    colorlinks=true,    
}    

\newtcblisting{pythoncode}[2][]{    
  listing engine=minted,    
  breakable,   
  colback=bg,    
  colframe=black!70,    
  listing only,    
  minted style=colorful,    
  minted language=python,    
  minted options={linenos=true,numbersep=3mm,texcl=true,#1},    
  left=5mm,enhanced,    
  overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)    
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
            #2,
}    

\begin{document}    

\section{Python example}    
\begin{pythoncode}[colback=red!5!white,colframe=red!75!black,title=My nice heading]    
# indent your Python code to put into an email    
import glob    
# glob supports Unix style pathname extensions    
python_files = glob.glob('*.py')    
for file_name in sorted(python_files):    
    print '    ------' + file_name    

    with open(file_name) as f:    
        for line in f:    
            print '    ' + line.rstrip()    

    print    
\end{pythoncode}    

\end{document}    
  • 1
    Do you mean that some options (passed to the optional argument of your pythoncode environment) should be interpreted as minted options and others as tcolorbox options? If I guessed wrong, please clarify the question. – jub0bs May 8 '14 at 23:25
  • 1
    @Jubobs: Exactly. I explained it more in the question. – AFP May 8 '14 at 23:27
7

What you did works with small changes. You have to pass options to pythoncode environment like this

\begin{pythoncode}[linenos=true,]{colback=red!5!white,colframe=red!75!black,title=My nice heading}

You have made the options to tcolorbox (i.e., pythoncode) mandatory hence they must be engulfed by braces where as the minted options go inside brakets.

\documentclass{book}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}

\usepackage{amsmath}
\usepackage{tcolorbox}
\tcbuselibrary{minted,skins,breakable}

\usepackage{hyperref}
\hypersetup{ %
    pdfborder = {0 0 0},
    colorlinks=true,
}

\newtcblisting{pythoncode}[2][]{
  listing engine=minted,
  breakable,
  colback=bg,
  colframe=black!70,
  listing only,
  minted style=colorful,
  minted language=python,
  minted options={numbersep=3mm,texcl=true,#1},
  left=5mm,enhanced,
  overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
            #2,
}

\begin{document}

\section{Python example}
\begin{pythoncode}[linenos=true,]{colback=red!5!white,colframe=red!75!black,title=My nice heading}
# indent your Python code to put into an email
import glob
# glob supports Unix style pathname extensions
python_files = glob.glob('*.py')
for file_name in sorted(python_files):
    print '    ------' + file_name

    with open(file_name) as f:
        for line in f:
            print '    ' + line.rstrip()

    print
\end{pythoncode}

\end{document}

enter image description here

Some elaboration:

When you define

\newtcblisting{pythoncode}[2][]{

You are saying that there will be two arguments to the pythoncode environment. With an empty [], you are making the first argument as optional whose default value is none. The optional argument is to be used within brackets or it can be simply omitted in which case the default value (here none) will be used. The second argument is mandatory and put inside braces. If you want to give no values to it simply use {} (unlike the first where you don't need to use any). Hence with no options you have to use

\begin{pythoncode}{}

Bottom line is [2][] means there are two arguments, first argument is optional and has default none.

  • 1
    Thank you. Can we make both optional not mandatory? – AFP May 8 '14 at 23:32
  • 1
    @Ahm easiest is to use \begin{pythoncode}{} if you don't want to give any options. – user11232 May 8 '14 at 23:45
  • Thank you. I would be grateful if you could explain to me what [2][] does in \newtcblisting{pythoncode}[2][]. All I know is that I am defining two options but I dont know what the next empty bracket [] does. – AFP May 9 '14 at 0:11
  • 1
    @Ahm The empty brackets mean that the optional argument is taken as empty if you don't specify any. In other words \begin{pythoncode}{...} is equivalent to \begin{pythoncode}[]{...}. – jub0bs May 9 '14 at 0:30

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.