1

For the reasons floats cannot be nested in tcolorbox and I want my (sub)figure to be fancier, I coded new Figure and Subfigure newtcolorbox environments. Here is a simplified version of my code

\documentclass[12pt,a4paper]{book}
\usepackage{lipsum}
\usepackage[skins, breakable]{tcolorbox}


\newcommand{\figref}[1]{Figure \ref{fig:#1}}

\newtcolorbox[auto counter]{Figure}[1]{
    title = {Figure~\thetcbcounter~$-$ #1}
    }
    
\newtcolorbox[
        auto counter,
        number within=tcb@cnt@Figure,
        number freestyle = {(\noexpand\alph{\tcbcounter})}
        ]
        {Subfigure}[3][]{
    width = (\linewidth - 4\dimexpr (#2pt - 1pt)\relax)/#2,
    before =,
    after = \hfill,
    box align = center,
    title = {\thetcbcounter~#3},
    #1
    }


\begin{document}

\begin{Figure}
    {Title figure}
    \begin{Subfigure}[label={fig:a}]{2}
        {Title again}
        \lipsum[1]
    \end{Subfigure}%
    %
    \begin{Subfigure}{2}
        {A nice Subfigure}
        \lipsum[2]
    \end{Subfigure}
\end{Figure}


See \figref{a}.

\end{document}

with the output

MWE

I like the result, but I would like the text to print Figure 1a instead of Figure (a) and so without changing the title of the subfigure. More precisely, I could change the number freestyle argument of newtcolorbox to have 1a as the counter of my subfigure, but the this would change the title from (a) blabla to 1a blabla which I don't want.

Note that I use hyperref package even if I removed it from my Minimal Working Example.

I know a similar question has been asked, but I don't know how close from my question it is since I don't understand what

\makeatletter
\newcommand{\customlabel}[2]{%
\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{}}}}
\makeatother

is doing...

1 Answer 1

2

You can change the label format using \labelformat (see ltnews30.pdf for more information):

\documentclass[12pt,a4paper]{book}
\usepackage{lipsum}
\usepackage[skins, breakable]{tcolorbox}


\newcommand{\figref}[1]{Figure \ref{fig:#1}}

\newtcolorbox[auto counter]{Figure}[1]{
    title = {Figure~\thetcbcounter~$-$ #1}
    }
    
\newtcolorbox[
        auto counter,
        number within=tcb@cnt@Figure,
        number freestyle = {(\noexpand\alph{\tcbcounter})}
        ]
        {Subfigure}[3][]{
    width = (\linewidth - 4\dimexpr (#2pt - 1pt)\relax)/#2,
    before =,
    after = \hfill,
    box align = center,
    title = {\thetcbcounter~#3},
    #1
    }

\labelformat{tcb@cnt@Subfigure}{\arabic{tcb@cnt@Figure}\alph{tcb@cnt@Subfigure}}

\begin{document}

\begin{Figure}
    {Title figure}
    \begin{Subfigure}[label={fig:a}]{2}
        {Title again}
        \lipsum[1]
    \end{Subfigure}%
    %
    \begin{Subfigure}{2}
        {A nice Subfigure}
        \lipsum[2]
    \end{Subfigure}
\end{Figure}


See \figref{a}.

\end{document}

enter image description here

BTW: I would suggest to also use cleveref instead of defining your own \figref.

2
  • Thanks for your answer! Could you motivate the use of cleveref instead of defining my own commands? Commented Jan 12 at 16:24
  • @OursPolaire IMHO this would be another question. See, e.g., tex.stackexchange.com/q/291429/277964
    – cabohah
    Commented Jan 12 at 16:33

You must log in to answer this question.

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