2

I have a custom environment for code like this (it is a modified version of the code here):

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

What I want is to be able to optionally specify the programming language used and the width of the box that holds the code, in a way that the default values for each are:

  • Programming language: python

  • Width: 0.8\textwidth (note: what I want to be able to change is the 0.8, not the \textwidth)

So far, I have only managed to have the programming language as optional, with the required default value, but I have not managed to be able to make the width optional as well.

Is there any way to have multiple optional arguments with multiple default values?

2 Answers 2

1

This is possible with xparse and 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}

enter image description here

3

I'd use styles, rather than piling up arguments.

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

enter image description here

You might want to specify only the fraction of \textwidth: the change is easy (so you see the usefulness of codewidth instead of directly width)

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .