强制首先选择 newtcblisting 环境

强制首先选择 newtcblisting 环境

我已经基于包“tcolorbox”中的“tcblisting”创建了一个新环境,其代码(MWE)如下:

\documentclass[10pt]{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable,skins}
\tcbset{
above/.style={colback=lightgray,colbacklower=white,listing and text,center lower},%
below/.style={colback=white,colbacklower=lightgray,text and listing,center upper},%
left/.style={colback=lightgray,colbacklower=white,listing side text,valign=center},%
right/.style={colback=white,colbacklower=lightgray,text side listing,valign=center},%
frame/.style={colframe=#1},%
ratio/.style={lefthand ratio=#1},%
noframe/.style={frame hidden,arc=0mm},%
number/.style={listing options={}},%
nonumber/.style={listing options={}},%
myexample/.style={breakable,skin=bicolor,
                  %above,%
                  %number,%
                  frame=gray,%
                  title style={draw=none,fill=none}%
                  }%
}%
\newtcblisting{example}[1]{myexample,#1}
\begin{document}

\begin{example}{above}
My example above in \LaTeX
\end{example}

\end{document}

我希望“上、下、左或右”选项始终是第一个必需的,例如:

\begin{example}{above,noframe}
My example above in \LaTeX
\end{example}

正确且

\begin{example}{noframe,above}
My example above in \LaTeX
\end{example}

错了。我尝试使用“xstring”包,但没有成功,想法如下:

\NewDocumentEnvironment{example}{...}{%
  \IfStrEqCase{#1}{%
    {% Case 1
     % Case 2
     % Case 3
     % Case ...
    }%
     %
    \begin{example}}% or \begin{tcblisting}{#1}}
    {\end{example}} % or \end{tcblisting}

但不知道它是否正确,或者是否有更有效的方法来实现这一点。提前致谢(kvoptions,etoolbox 可能)。Pablo

答案1

这是一种实现方法expl3;我们检查第一个键是否在允许列表中;如果不在,则会发出警告。

\documentclass[10pt]{article}

\usepackage{xparse}

\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable,skins}
\tcbset{
  above/.style={colback=lightgray,colbacklower=white,listing and text,center lower},
  below/.style={colback=white,colbacklower=lightgray,text and listing,center upper},
  left/.style={colback=lightgray,colbacklower=white,listing side text,valign=center},
  right/.style={colback=white,colbacklower=lightgray,text side listing,valign=center},
  frame/.style={colframe=#1},
  ratio/.style={lefthand ratio=#1},
  noframe/.style={frame hidden,arc=0mm},
  number/.style={listing options={}},
  nonumber/.style={listing options={}},
  myexample/.style={
    breakable,skin=bicolor,
    %above,
    %number,
    frame=gray,
    title style={draw=none,fill=none}
  }
}

\newtcblisting{exampleinner}[1]{myexample,#1}

\ExplSyntaxOn
\NewDocumentEnvironment{example}{ m }
 {
  \pablo_test_options:n { #1 }
  \exampleinner{#1}
 }
 {
  \endexampleinner
 }

\cs_new_protected:Npn \pablo_test_options:n #1
 {
  \str_case:xnF { \clist_item:nn { #1 } { 1 } }
   {
    {above}{}
    {below}{}
    {left}{}
    {right}{}
   }
   {
    \PackageWarning{pablo}{Invalid~first~key} % or whatever you like
   } 
 }
\cs_generate_variant:Nn \str_case:nnF { x }
\ExplSyntaxOff

\begin{document}

\begin{example}{above}
My example above in \LaTeX
\end{example}

\begin{example}{above,noframe}
My example above in \LaTeX
\end{example}

\begin{example}{noframe,above}
My example above in \LaTeX
\end{example}

\end{document}

相关内容