0

I want to start by saying I don't fully understand how environments work, so I apologize if this is turns out to be a bit out of topic.

Desired behaviour

I'm trying to create a wrapper environment for tcolorbox with some default parameters, and I have defined a listing environment (powershell) with tcolorbox using \newtcblisting.

The problem:

I tried creating a custom environment using xparse's NewDocumentEnvironment, I'm also using expl3 and ltxdoc because I need those for other sections of the document (I had to make some LaTeX magic to use ltxdoc with a book class).

I tried to locate where the error happens and it seems to be in the powershell environment. Specifically I noticed the following:

  • If I remove the powershell listing from the defaultbox environment it works as expected, and,
  • If I don't use the defaultbox environment and directly use the tcolorbox environment inside the document the powershell environment works as expected.

How to reproduce the error

The following code results on an error.

\RequirePackage{scrlfile}
\ReplaceClass{article}{book}
\documentclass{ltxdoc}
\usepackage{fontspec}
\usepackage[dvipsnames]{xcolor}
\usepackage{minted}
\usepackage[skins, minted, breakable]{tcolorbox}
\usepackage{expl3}
\usepackage{xparse}
\title{Title}

\author{Author}

\date{\today}

\definecolor{LightGray}{gray}{0.9}
\definecolor{Transparent}{gray}{1.0}

\ExplSyntaxOn
% | Creates a new tcolorbox environment with enhanced borders, breakable content, and a title.
\NewDocumentEnvironment { defaultbox } { o +b } {
  \IfNoValueTF {#1} {
    \begin{tcolorbox}[enhanced, breakable]
      #2
    \end{tcolorbox}
  } {
    \begin{tcolorbox}[enhanced, breakable, title=#1]
      #2
    \end{tcolorbox}
  }
} {}
\ExplSyntaxOff

\newtcblisting{powershell}{
   enhanced,
   breakable,
   listing engine=minted,
   minted style=emacs,
   minted language=powershell,
   minted options={autogobble},
   colback=blue!5!white,
   colframe=Cerulean!75!black,
   listing only,
   left=5mm,enhanced,
   overlay={
      \begin{tcbclipinterior}
         \fill[Cerulean!20!white] (frame.south west) rectangle ([xshift=5mm]frame.north west);
      \end{tcbclipinterior}
   }
}

\tcbuselibrary{documentation}

\setmainfont{Minion Pro}
\setmonofont{Fira Mono for Powerline}[Scale=MatchLowercase]

\tcbset{listing engine=minted, colframe=Orchid!75!black}


\begin{document}
  \begin{defaultbox}[Box Title]
    Text
    \begin{powershell}
      [Net.ServicePointManager]::SecurityProtocol = `
        [Net.SecurityProtocolType]::Tls12
      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
      Invoke-WebRequest "https://chocolatey.org/install.ps1" `
        -UseBasicParsing | Invoke-Expression
    \end{powershell}
  \end{defaultbox}
\end{document}

Error output

The above code causes the following output:

This is XeTeX, Version 3.141592653-2.6-0.999993 (MiKTeX 22.1) (preloaded format=xelatex.fmt)
 \write18 enabled.
entering extended mode

(...)

Runaway argument?
! File ended while scanning use of \next.
<inserted text> 
                \par 
<*> ...cs/Escuela/software-design-book-es/test.tex
                                                  
! Emergency stop.
<*> ...cs/Escuela/software-design-book-es/test.tex
                                                  
No pages of output.
Transcript written on test.log.

Any idea why this is happening?

1 Answer 1

0

Your powershell environment is a verbatim environment (declared with \newtcblisting), which means you can't use it in the argument to another command. When you define \NewDocumentEnvironment { defaultbox } { o +b }, it will grab its contents as argument, and break the verbatim environment.

But you don't have to grab the body of the environment. You can define defaultbox as a regular environment:

\NewDocumentEnvironment { defaultbox } { o }
  {%
    \IfNoValueTF{#1}
      {\begin{tcolorbox}[enhanced, breakable]}
      {\begin{tcolorbox}[enhanced, breakable, title=#1]}%
  }
  {\end{tcolorbox}}

or even easier, define defaultbox with \newtcolorbox:

\newtcolorbox{defaultbox}[1][]
  {enhanced, breakable, title=#1}

then it will work:

enter image description here

Full code:

\RequirePackage{scrlfile}
\ReplaceClass{article}{book}
\documentclass{ltxdoc}
\usepackage{fontspec}
\usepackage[dvipsnames]{xcolor}
\usepackage{minted}
\usepackage[skins, minted, breakable]{tcolorbox}
\usepackage{expl3}
\usepackage{xparse}
\title{Title}

\author{Author}

\date{\today}

\definecolor{LightGray}{gray}{0.9}
\definecolor{Transparent}{gray}{1.0}

% \ExplSyntaxOn
% % | Creates a new tcolorbox environment with enhanced borders, breakable content, and a title.
% \NewDocumentEnvironment { defaultbox } { o }
%   {%
%     \IfNoValueTF{#1}
%       {\begin{tcolorbox}[enhanced, breakable]}
%       {\begin{tcolorbox}[enhanced, breakable, title=#1]}%
%   }
%   {\end{tcolorbox}}
% \ExplSyntaxOff

\newtcolorbox{defaultbox}[1][]
  {enhanced, breakable, title=#1}

\newtcblisting{powershell}{
   enhanced,
   breakable,
   listing engine=minted,
   minted style=emacs,
   minted language=powershell,
   minted options={autogobble},
   colback=blue!5!white,
   colframe=Cerulean!75!black,
   listing only,
   left=5mm,enhanced,
   overlay={
      \begin{tcbclipinterior}
         \fill[Cerulean!20!white] (frame.south west) rectangle ([xshift=5mm]frame.north west);
      \end{tcbclipinterior}
   }
}

\tcbuselibrary{documentation}

% \setmainfont{Minion Pro}
\setmainfont{Fira Code}
\setmonofont{Fira Code}[Scale=MatchLowercase]

\tcbset{listing engine=minted, colframe=Orchid!75!black}

\begin{document}
  \begin{defaultbox}
    Text
    \begin{powershell}
      [Net.ServicePointManager]::SecurityProtocol = `
        [Net.SecurityProtocolType]::Tls12
      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
      Invoke-WebRequest "https://chocolatey.org/install.ps1" `
        -UseBasicParsing | Invoke-Expression
    \end{powershell}
  \end{defaultbox}
\end{document}

You must log in to answer this question.

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