4

I have created a new environment based on "tcblisting" from package "tcolorbox" is code (MWE) is as follows:

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

I want an option "above,below,left or right" is always the first obligate,for example:

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

is correct and

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

Wrong. I tried with the "xstring" package but have not been successful,an idea like this:

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

but not if it is correct, or if there is a more effective way to achieve this. Thanked beforehand (kvoptions,etoolbox maybe). Pablo

  • Do you want to raise an error or a warning if the first key is not among above, below, left or right? – egreg Nov 26 '13 at 00:32
  • @egreg:A warning/stop if the first key is not among above, below, left or right. this is part of package i need create see http://tex.stackexchange.com/questions/146308/kvoptions-and-conditional-environment – Pablo González L Nov 26 '13 at 00:46

1 Answers1

2

Here's a way to do it with expl3; we check whether the first key is in the allowed list; if it isn't a warning is issued.

\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}
egreg
  • 1,121,712
  • Thank you very much, works perfect, query, options must be obligatorily between {...}, can I define in [...] example par?, and create an "alias" to "above" is the same that "pos = t". regards. – Pablo González L Nov 27 '13 at 18:43
  • @PabloGonzález Just use \NewDocumentEnvironment{example}{O{}} to make the argument optional (with empty default value). – egreg Nov 27 '13 at 18:51
  • Thanks, my problem is now to create the "alias" to "above" is the same as "pos = t". PS: Forgot to put \usepackage{expl3} in your reply. – Pablo González L Nov 27 '13 at 18:59
  • @PabloGonzález That's a job for pgf keys, which I'm not expert of. The package expl3 is automatically loaded by xparse. – egreg Nov 27 '13 at 19:01