TransWikia.com

Multiple optional arguments with tcolorbox

TeX - LaTeX Asked on August 5, 2021

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{deftemp{#1} ifxtempempty }

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?

One Answer

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{deftemp{#1} ifxtempempty }
%
%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

Correct answer by Ignasi on August 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP