3

I have defined a python environment in my document using tcolorbox. I would like to add a title to the listing. However, the compiler complains about adding the following to pythoncode environment:

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

When I remove the above options and only use \begin{pythoncode}[], the code compiles successfully. In below is a MWE about this problem.

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

\usepackage{amsmath}    
\usepackage[pdftex]{graphicx, color}    
\usepackage{hyperref}       
\hypersetup{ %    
    pdfborder = {0 0 0},    
    colorlinks=true,    
}    

\usepackage[T1]{fontenc} 

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


\newtcblisting{pythoncode}[1][]{    
  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}}    
}    


\definecolor{bg}{rgb}{0.85,0.85,0.85}    

\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}    

Could someone shed light on this problem?

2

You have the argument, #1, in the incorrect place; you currently have it here:

minted options={linenos=true,numbersep=3mm,texcl=true,#1},    

so you are passing the options to the minted package, which is not what you want to do.

Instead, move it to the end of your options list, just after the overlay command:

  overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)    
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
            #1,

Here's a working version - note that hyperref should be loaded last in this case; see Which packages should be loaded after hyperref instead of before? for more details.

% arara: pdflatex: {shell: yes}
\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}[1][]{    
  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},    
  left=5mm,enhanced,    
  overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)    
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
            #1,
}    

\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}    
  • @ cmhughes: Thanks. But this removes the background color and replaces it with white. Only the line numbers are in defined bg color. – AFP May 8 '14 at 22:20
  • @Ahm I don't understand; you call your environment with [colback=red!5!white,colframe=red!75!black.... which changes the background colour to red!5!white which is nearly white. It does not remove the background color – cmhughes May 8 '14 at 22:38
  • @ cmhughes: I am very sorry. You are right. Thank you for your help. How would it be possible to pass one argument to minted options and one to tcolorbox at the same time? – AFP May 8 '14 at 23:12
  • @Ahm that depends - do you want them both optional? If so, that could be tricky (without xparse) – cmhughes May 9 '14 at 2:50

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.