2

I would like to produce boxes from tcolorbox which can take two optional arguments, but I'm not sure where to start with. Here is my current MWE :

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

\def \ifempty#1{\def\temp{#1} \ifx\temp\empty }

\newtcolorbox[auto counter,number within=chapter]{definition}[1][]{title={ Définition \thetcbcounter \ifempty{#1} \else --- #1 \fi}}

\begin{document}

\begin{definition}[Matrices]
\lipsum[1]
\end{definition}

\end{document}

This is made such that every single box has a counter depending on the chapter we're in. An optional argument can be added in case I would want to give a specific name to the box. I would like to add a second optional argument to add a label (to be able to reference to it) to the box. I can add a key label= to the color box but I'm not sure about how to add a second optional argument that would fill that key.

How can I add a second argument to the tcolorbox that would add a label=#2 to the list of arguments of the tcolorbox?

1
  • It is necessary to load the xparse library of tcolorbox \tcbuselibrary{xparse}. The explanations of the multiple options are in the documentation of the xparse package.
    – AndréC
    Commented May 3, 2021 at 3:43

1 Answer 1

1

There's no need to reinvent the wheel. tcolorbox offers \newtcbtheorem to define theorems, definitions, ... environments with only two mandatory parameters which can be left empty. The parameters are a "title" and a "label" (or label suffix).

Here you have two examples. More information in section 17 from tcolorbox documentation.

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

%\def \ifempty#1{\def\temp{#1} \ifx\temp\empty }
%
%\newtcolorbox[auto counter,number within=chapter]{definition}[1][]{title={ Définition \thetcbcounter \ifempty{#1} \else --- #1 \fi}}

\newtcbtheorem[auto counter,number within=chapter]{definition}{Définition}{}{def}

\begin{document}
\chapter{First}

\begin{definition}{Matrices}{a}
\lipsum[2]
\end{definition}

As can be seen in Definition~\ref{def:a} \dots

\begin{definition}{}{b}
\lipsum[2]
\end{definition}

As can be seen in Definition~\ref{def:b} \dots

\end{document}

enter image description here

You must log in to answer this question.

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