How to have two optional parameters in a tcolorbox?

A slight variation of the OP's self answer, shifting the 2nd optional argument to the end of the argument list, i.e. it will become the 3rd. argument and transforming the o - type of the first argument to O.

A syntax of \foo[A][B]{...} is bad design and difficult to handle by TeX/LaTeX, since it cannot decide what

\foo[A]{}

is supposed to be. Is A the first optional or the second optional argument? This is unclear.

The o - type specifier from xparse gives a way to decide whether the 1st or the 2nd opt. argument is omitted, but alas, it does not allow for default arguments.

\foo[A]{...}[B] however is clearly recognizable and TeX can distinguish between #1 (being A here) and #3 (being B), so \foo[A]{...}and\foo{...}[B]` is well-defined

\documentclass{article}
\usepackage{lipsum}
\usepackage{xfrac}
\usepackage[most]{tcolorbox}


\NewDocumentCommand{\currentschoolyear}{+O{\the\year}}{%
  \ifnum#1=\the\year%
  \ifnum\month<9%
  \sfrac{\the\numexpr#1-1}{#1}%     #1=2018 --> 2017/2018
  \else
  \sfrac{#1}{\the\numexpr#1+1}%      #1=2018 --> 2018/2019
  \fi
  \else
  \sfrac{#1}{\the\numexpr#1+1}%      
  \fi
}


% For checking reasons only...
%\year=2017
%\month=6

%%%%%%%%%%%%%%%%%%%%%%%%%%%
% boîte resumé

\def\couleurresume{red}%

%% style du titre « résumé du cours »
%\tcbset{titreresume/.style={
%    boxed title style={
%        colframe=#2!50,%
%        colback=#2!10,%
%        coltext=blue,%
%    leftrule=1.5mm,rightrule=1.5mm,toprule=1.5pt,bottom=0pt,boxsep=1pt}}
%}
%
%% style des soustitres des résumés
%\tcbset{soustitre/.style={
%    subtitle style={%
%    colback=#2!7,%
%    colframe=#2!50,%
%    boxsep=1mm,
%    fontupper={\sffamily\bfseries\large\selectfont\color{#2!63!black}}}}
%}

% boîte créant la boîte résumé
\NewTColorBox[auto counter]{resume}{+O{\the\year}+m+O{red}}{%
    minipage boxed title*=-106mm,
    attach boxed title to top center={yshift=-3mm,xshift=-\linewidth/5},
    enhanced,
    nobeforeafter,
    lower separated=false,
%    IfValueTF={#1}{%
    before upper={\textcolor{#3!63!black}{\currentschoolyear[#1] \hfill{#2}\hfill\thetcbcounter/\ref{nombre-de-resume}}},
    % }{%
    %   before upper={\textcolor{#2!63!black}{\currentschoolyear \hfill{#2}\hfill\thetcbcounter/\ref{nombre-de-resume}}},
    % },
    colframe=#3!50,%
    colback=white,%
    coltitle=#3!63!black,%
    leftrule=3mm,rightrule=3mm,
    toprule=2pt,bottomrule=2pt,
    left=3pt,right=0pt,top=3mm,
    fonttitle=\sffamily\bfseries\large,
%    titreresume,
    boxed title style={
        colframe=#3!50,%
        colback=#3!10,%
        coltext=blue,%
    leftrule=1.5mm,rightrule=1.5mm,toprule=1.5pt,bottom=0pt,boxsep=1pt},
%    soustitre,
    subtitle style={%
    colback=#3!7,%
    colframe=#3!50,%
    boxsep=1mm,
    fontupper={\sffamily\bfseries\large\selectfont\color{#3!63!black}}},
    title={\normalsize Résumé}
}

\usepackage{atveryend}
\makeatletter
\AfterLastShipout{%
  \immediate\write\@auxout
  {\string\newlabel{nombre-de-resume}{{\thetcb@cnt@resume}{}}}%
}
\makeatother
\begin{document}

% No optional arguments -> defaults to current year and red
\begin{resume}{chapitre 1}
  \tcbsubtitle{Définition}
  \lipsum[1]
\end{resume}


% both optional arguments -> 2016/2017 and blue
\begin{resume}[2016]{chapitre 1}[blue]
\tcbsubtitle{Définition}

\lipsum[1]
\end{resume}


\newpage

% Only the first optional argument -> 2020 and red
\begin{resume}[2020]{chapitre 1}

\tcbsubtitle{Propriétés}
\lipsum[2]
\end{resume}

% Only the second optional argument  -> current year and green
\begin{resume}{chapitre 1}[green]

\tcbsubtitle{Propriétés}
\lipsum[2]
\end{resume}




%\show\tcbcounter
\end{document}

enter image description here enter image description here


I just found, just put each optional argument in square brackets.

\begin{resume}[2018][blue]{chapitre 1}

I have not yet managed to make sure that I can call this box by indicating only the color (without giving the school year).

Code:

\documentclass{article}
\usepackage{lipsum}
\usepackage{xfrac}
\usepackage[most]{tcolorbox}

\NewDocumentCommand{\currentschoolyear}{+o}{%
  \IfValueTF{#1}{%
    \sfrac{#1}{\the\numexpr#1+1}
  }{%
    \ifnum\month<9%
    \sfrac{\the\numexpr\year-1}{\the\year}%
    \else
    \sfrac{\the\year}{\the\numexpr\year+1}%
    \fi
  }%
}

%\month=7
%\day=31

%%%%%%%%%%%%%%%%%%%%%%%%%%%
% boîte resumé

\def\couleurresume{red}%

%% style du titre « résumé du cours »
%\tcbset{titreresume/.style={
%    boxed title style={
%        colframe=#2!50,%
%        colback=#2!10,%
%        coltext=blue,%
%    leftrule=1.5mm,rightrule=1.5mm,toprule=1.5pt,bottom=0pt,boxsep=1pt}}
%}
%
%% style des soustitres des résumés
%\tcbset{soustitre/.style={
%    subtitle style={%
%    colback=#2!7,%
%    colframe=#2!50,%
%    boxsep=1mm,
%    fontupper={\sffamily\bfseries\large\selectfont\color{#2!63!black}}}}
%}

% boîte créant la boîte résumé
\NewTColorBox[auto counter]{resume}{+o+O{red}+m}{%
    minipage boxed title*=-106mm,
    attach boxed title to top center={yshift=-3mm,xshift=-\linewidth/5},
    enhanced,
    nobeforeafter,
    lower separated=false,
    IfValueTF={#1}{%
      before upper={\textcolor{#2!63!black}{\currentschoolyear[#1] \hfill{#3}\hfill\thetcbcounter/\ref{nombre-de-resume}}},
    }{%
      before upper={\textcolor{#2!63!black}{\currentschoolyear \hfill{#3}\hfill\thetcbcounter/\ref{nombre-de-resume}}},
    },
    colframe=#2!50,%
    colback=white,%
    coltitle=#2!63!black,%
    leftrule=3mm,rightrule=3mm,
    toprule=2pt,bottomrule=2pt,
    left=3pt,right=0pt,top=3mm,
    fonttitle=\sffamily\bfseries\large,
%    titreresume,
    boxed title style={
        colframe=#2!50,%
        colback=#2!10,%
        coltext=blue,%
    leftrule=1.5mm,rightrule=1.5mm,toprule=1.5pt,bottom=0pt,boxsep=1pt},
%    soustitre,
    subtitle style={%
    colback=#2!7,%
    colframe=#2!50,%
    boxsep=1mm,
    fontupper={\sffamily\bfseries\large\selectfont\color{#2!63!black}}},
    title={\normalsize Résumé}
}

\usepackage{atveryend}
\makeatletter
\AfterLastShipout{%
  \immediate\write\@auxout
  {\string\newlabel{nombre-de-resume}{{\thetcb@cnt@resume}{}}}%
}
\makeatother
\begin{document}

\begin{resume}[2017]{chapitre 1}
\tcbsubtitle{Définition}

\lipsum[1]
\end{resume}

\newpage

\begin{resume}[2018][blue]{chapitre 1}

\tcbsubtitle{Propriétés}
\lipsum[2]
\end{resume}

%\show\tcbcounter
\end{document}

boite rouge par défaut

boîte bleu


What about using different delimiters for the two optional parameters? Like this, for example:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xparse}
\NewDocumentCommand\Test{D(){red}O{2018}+m}{%
    Color: #1; Year: #2; mandatory: #3\par
}
\begin{document}
\Test{only mandatory}
\Test(blue){with color}
\Test[2020]{with year}
\Test(green)[1999]{with everything}
\end{document}

which works and, in my opinion, is even easier to remember.

enter image description here

You can combine it with Christian Hupfer's answer to have the current school year (changing the O here to a o).

You can also add a third optional argument with, for example, angle brackets that will be a comma separated string to add to the tcolorbox options.

Applied to your nsMWE:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{xfrac}
\usepackage[most]{tcolorbox}


\NewDocumentCommand{\currentschoolyear}{+O{\the\year}}{%
  \ifnum\month<9%
  \sfrac{\the\numexpr#1-1}{#1}%     #1=2018 --> 2017/2018
  \else
  \sfrac{#1}{\the\numexpr#1+1}%      #1=2018 --> 2018/2019
  \fi
}

\NewTColorBox[auto counter]{resume}{D<>{}D(){red}O{\the\year}+m}{%
    minipage boxed title*=-106mm,
    attach boxed title to top center={yshift=-3mm,xshift=-\linewidth/5},
    enhanced,
    nobeforeafter,
    lower separated=false,
    before upper={\textcolor{#2!63!black}{\currentschoolyear[#3] \hfill{#4}\hfill\thetcbcounter/\ref{nombre-de-resume}}},
    colframe=#2!50,%
    colback=white,%
    coltitle=#2!63!black,%
    leftrule=3mm,rightrule=3mm,
    toprule=2pt,bottomrule=2pt,
    left=3pt,right=0pt,top=3mm,
    fonttitle=\sffamily\bfseries\large,
%    titreresume,
    boxed title style={
        colframe=#2!50,%
        colback=#2!10,%
        coltext=blue,%
    leftrule=1.5mm,rightrule=1.5mm,toprule=1.5pt,bottom=0pt,boxsep=1pt},
%    soustitre,
    subtitle style={%
    colback=#2!7,%
    colframe=#2!50,%
    boxsep=1mm,
    fontupper={\sffamily\bfseries\large\selectfont\color{#2!63!black}}},
    title={\normalsize Résumé}, 
    #1
}

\usepackage{atveryend}
\makeatletter
\AfterLastShipout{%
  \immediate\write\@auxout
  {\string\newlabel{nombre-de-resume}{{\thetcb@cnt@resume}{}}}%
}
\makeatother
\begin{document}

% No optional arguments -> defaults to current year and red
\begin{resume}{chapitre 1}
  \tcbsubtitle{Définition}
  \lipsum[1]
\end{resume}


% both optional arguments -> 2021 and blue
\begin{resume}(blue)[2021]{chapitre 1}
\tcbsubtitle{Définition}

\lipsum[1]
\end{resume}


\newpage

% Only the first optional argument -> 2020 and red
\begin{resume}[2020]{chapitre 1}

\tcbsubtitle{Propriétés}
\lipsum[2]
\end{resume}

% Only the second optional argument  -> current year and green
\begin{resume}(green){chapitre 1}

\tcbsubtitle{Propriétés}
\lipsum[2]
\end{resume}

%  New optional argument - add whatever to the options to the tcolorbox 
\begin{resume}<colback=red, toprule=1cm>(green){chapitre 1}

\tcbsubtitle{Propriétés}
\lipsum[2]
\end{resume}



%\show\tcbcounter
\end{document}

page1 page2

Tags:

Tcolorbox