期望的行为

期望的行为

首先我想说的是,我不完全了解环境是如何运作的,所以如果这有点偏离主题,我深表歉意。

期望的行为

tcolorbox我正在尝试使用一些默认参数创建一个包装器环境,并且我已经使用定义了一个列表环境( powershell)。tcolorbox\newtcblisting

问题:

xparse我尝试使用创建一个自定义环境NewDocumentEnvironment,我也使用expl3和,ltxdoc因为我需要它们用于文档的其他部分(我必须制作一些 LaTeX 魔法才能ltxdoc与书籍类一起使用)。

我尝试找到错误发生的位置,结果似乎是在环境中powershell。具体来说,我注意到了以下几点:

  • 如果我从环境中删除 powershell 列表,defaultbox它就会按预期工作,并且,
  • 如果我不使用defaultbox环境而直接使用tcolorbox文档内的环境,则powershell环境将按预期工作。

如何重现错误

以下代码导致错误。

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

错误输出

上述代码将产生以下输出:

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.

知道为什么会发生这种情况吗?

答案1

您的powershell环境是逐字环境(用 声明\newtcblisting),这意味着您不能将其用作另一个命令的参数。当您定义 时\NewDocumentEnvironment { defaultbox } { o +b },它将抓取其内容作为参数,并破坏逐字环境。

但你不必抓住环境的主体。你可以将其定义defaultbox为常规环境:

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

或者更简单,定义defaultbox如下\newtcolorbox

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

那么它将起作用:

在此处输入图片描述

完整代码:

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

相关内容