向 tcolorbox 添加标题时出现问题

向 tcolorbox 添加标题时出现问题

我使用 在我的文档中定义了一个 python 环境tcolorbox。我想为列表添加一个标题。但是,编译器抱怨将以下内容添加到pythoncode环境中:

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

当我删除上述选项并仅使用时\begin{pythoncode}[],代码编译成功。下面是关于此问题的 MWE。

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

有人可以解释一下这个问题吗?

答案1

您的论点,,#1在错误的地方;你现在在这里:

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

因此,您正在将选项传递给minted包,但这不是您想要做的。

相反,将其移动到选项列表的末尾,紧接着命令overlay

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

这是一个工作版本 - 注意,hyperref在这种情况下应该最后加载;参见哪些包应该在 hyperref 之后加载而不是之前加载?更多细节。

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

相关内容