0

I have 3 new environments based on tcolorbox, Figure, SFigure and Subfigure.

When I use environment Figure, the first 2 boxes are numbered 1.1 and 1.2. Then when I use SFigure with Subfigure, the next 2 boxes are also numbered 1.1 and 1.2. They should be numbered 1.3 and 1.4.

As for the last box numbered 1.3 (should be 1.5), I use the standard environment subfigure, but the references show 1a and 1b (should be 1.5a and 1.5b).

How can I fix that?

enter image description here

\documentclass[12pt]{article}
\usepackage[left=1.5cm,top=1.2cm,bottom=1.2cm,right=1.5cm]{geometry}

\usepackage{caption,subcaption}
\usepackage{tcolorbox}
\tcbuselibrary{many}

\usepackage{cleveref}

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

\newtcolorbox[auto counter, number within=section,crefname={figure}{figures}]
    {Figure}[2][]{title={Figure~\thetcbcounter~$-$ #2},#1}

\newtcolorbox[auto counter,number within=section,crefname={figure}{figures}]
    {SFigure}[1]{title={Figure~\thetcbcounter~$-$ #1}}

\newtcolorbox[auto counter,number within=tcb@cnt@Figure,number freestyle = {(\noexpand\alph{\tcbcounter})},crefname={figure}{figures}]
    {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{section}.\arabic{tcb@cnt@Figure}\alph{tcb@cnt@Subfigure}}

\begin{document}

\section{First section}

\begin{Figure}[label={fig:a}]{AAAA}
\captionsetup{type=figure}
\centering
\rule{3cm}{1cm}
\end{Figure}
See \figref{fig:a}, \cref{fig:a}, \Cref{fig:a}

\begin{Figure}[label={fig:b}]{BBBB}
\captionsetup{type=figure}
\centering
\rule{3cm}{1cm}
\end{Figure}
See \figref{fig:b}, \cref{fig:b}

\begin{SFigure}{XXXX}
\begin{center}
\captionsetup{type=figure}
\begin{Subfigure}[label={fig:1a}]{2}{aaaa}
\centering
\rule{3cm}{1cm}
\end{Subfigure}%
\begin{Subfigure}[label={fig:1b}]{2}{bbbb}
\centering
\rule{3cm}{1cm}
\end{Subfigure}
\end{center}
\end{SFigure}
See \figref{fig:1a}, \figref{fig:1b}

\begin{SFigure}{YYYY}
\begin{center}
\captionsetup{type=figure}
\begin{Subfigure}[label={fig:2a}]{2}{cccc}
\centering
\rule{3cm}{1cm}
\end{Subfigure}%
\begin{Subfigure}[label={fig:2b}]{2}{dddd}
\centering
\rule{3cm}{1cm}
\end{Subfigure}
\end{center}
\end{SFigure}
See \figref{fig:2a}, \figref{fig:2b}

\begin{SFigure}{ZZZZ}
\centering
\captionsetup{type=figure}
\begin{subfigure}{0.4\textwidth}
\centering
\rule{3cm}{1cm}
\caption{eeee}
\label{fig:3a}
\end{subfigure}%
\hfill
\begin{subfigure}{0.4\textwidth}
\centering
\rule{3cm}{1cm}
\caption{ffff}
\label{fig:3b}
\end{subfigure}
\end{SFigure}
See \figref{fig:3a}, \figref{fig:3b}

\end{document}
1
  • 1
    [auto counter] causes the counters in the color boxes to have nothing to do with each other. You can fix that with [use counter from Figure], but you also want to use the caption package. For that you will need [use counter=figure] or [use counter=subfigure] which totally confuses your current label system. Commented May 30 at 14:49

0

You must log in to answer this question.

Browse other questions tagged .