如何將兩個選項傳遞給 tcolorbox

如何將兩個選項傳遞給 tcolorbox

有人可以告訴我如何同時將一個選項minted options和一個選項傳遞到tcolorbox 清單嗎?我的意思是,我希望將一些選項傳遞給,如下面的 MWE所示,並將一些選項傳遞給,如 所示。tcolorboxpythoncodeminted options#1tcolorbox#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

您所做的只需稍加改動即可。您必須將選項傳遞給pythoncode這樣的環境

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

您已將選項設為tcolorbox(即pythoncode)強制,因此它們必須用大括號括起來,因為minted選項會進入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}

在此輸入影像描述

一些闡述:

當你定義

\newtcblisting{pythoncode}[2][]{

你是說環境會有兩個爭論pythoncode。如果為空[],則第一個參數optional的預設值為 none。可選參數將在其中使用brackets,或者可以簡單地省略,在這種情況下將使用預設值(此處為無)。第二個參數是強制性的,並放在大括號內。如果你不想給它任何值,只需使用{}(與第一個不同,你不需要使用任何值)。因此,沒有選項你必須使用

\begin{pythoncode}{}

底線[2][]意味著有兩個參數,第一個參數是可選的並且具有預設值none

相關內容