\newtcblisting 具有多个可选参数和默认值

\newtcblisting 具有多个可选参数和默认值

我有一个像这样的代码的自定义环境(它是代码的修改版本这里):

\documentclass[12pt,a4paper]{report}

\usepackage[many]{tcolorbox}
\tcbuselibrary{minted,skins}
\definecolor{Gray}{gray}{0.9}

\newtcblisting{code}[2][python]{
  colback=Gray,
  listing engine=minted,
  minted language=#1,
  listing only,
  skin=tile,
  width=#2\textwidth,
 }
\BeforeBeginEnvironment{code}{\centering}
\AfterEndEnvironment{code}{}



\begin{document}
    \begin{code}[python]{0.8}
        print("This is some sample code")
    \end{code}
\end{document}

我想要的是能够选择性地指定使用的编程语言和保存代码的框的宽度,每个的默认值为:

  • 编程语言:python

  • 宽度:(0.8\textwidth注意:我想要改变的是0.8,而不是\textwidth

到目前为止,我只设法将编程语言设为可选项,并具有所需的默认值,但我还未能使宽度也成为可选项。

有没有办法让多个可选参数具有多个默认值?

答案1

xparse通过和可以实现这一点DeclareTCBListing

\documentclass[12pt,a4paper]{report}
\usepackage{xparse}
\usepackage[many]{tcolorbox}
\tcbuselibrary{minted,skins}
\definecolor{Gray}{gray}{0.9}

\DeclareTCBListing{code}{ !O{python} !O{.8} }{
  colback=Gray,
  listing engine=minted,
  minted language=#1,
  listing only,
  skin=tile,
  width=#2\textwidth,
 }

\begin{document}
  \begin{code}
    print("This is some sample code")
  \end{code}

  \begin{code}[python][1]
    print("This is some sample code")
  \end{code}

  \begin{code}[C]
  #include <stdio.h>
  int main() {
    printf("This is some sample code");
    return 0;
  }
  \end{code}

  \begin{code}[C][.5]
  #include <stdio.h>
  int main() {
    printf("This is some sample code");
    return 0;
  }
  \end{code}
\end{document}

在此处输入图片描述

答案2

我会使用风格,而不是堆积论点。

\documentclass{article}

\usepackage[many]{tcolorbox}
\tcbuselibrary{minted,skins}
%\usepackage{minted}

\definecolor{Gray}{gray}{0.9}

\newtcblisting{code}[1][]{
  colback=Gray,
  listing engine=minted,
  listing only,
  skin=tile,
  sharp corners,
  top=2pt,bottom=2pt,left=2pt,right=2pt,
  codelanguage,
  codewidth,
  #1,
 }
\BeforeBeginEnvironment{code}{\centering}

\tcbset{
  codelanguage/.style={minted language=#1},
  codelanguage/.default=python,
  codewidth/.style={width=#1},
  codewidth/.default=0.8\textwidth,
}


\begin{document}

\begin{code}
print("This is some code example") 
\end{code}

\begin{code}[codewidth=1\textwidth]
print("This is some code example") 
\end{code}

\begin{code}[codelanguage=C]
print("This is some code example") 
\end{code}

\begin{code}[codelanguage=C,codewidth=1\textwidth]
print("This is some code example") 
\end{code}

\end{document}

在此处输入图片描述

您可能只想指定一小部分:更改很容易(因此您会看到直接而不是\textwidth的用处)codewidthwidth

\documentclass{article}

\usepackage[many]{tcolorbox}
\tcbuselibrary{minted,skins}
%\usepackage{minted}

\definecolor{Gray}{gray}{0.9}

\newtcblisting{code}[1][]{
  colback=Gray,
  listing engine=minted,
  listing only,
  skin=tile,
  sharp corners,
  top=2pt,bottom=2pt,left=2pt,right=2pt,
  codelanguage,
  codewidth,
  #1,
 }
\BeforeBeginEnvironment{code}{\centering}

\tcbset{
  codelanguage/.style={minted language=#1},
  codelanguage/.default=python,
  codewidth/.style={width=#1\textwidth},
  codewidth/.default=0.8,
}


\begin{document}

\begin{code}
print("This is some code example") 
\end{code}

\begin{code}[codewidth=1]
print("This is some code example") 
\end{code}

\begin{code}[codelanguage=C]
print("This is some code example") 
\end{code}

\begin{code}[codelanguage=C,codewidth=1]
print("This is some code example") 
\end{code}

\end{document}

相关内容