1

I do have to create some floating boxes containing various informations, some text, figures... I need to refer it sometimes using the document in the whole, sometimes I need to be more specific and refer only to a figure or a subfigure.

An exemple:

\documentclass{article}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{newfloat}
\usepackage{tcolorbox}

\newcounter{numdoc}
\renewcommand\thenumdoc{\arabic{numdoc}}
\DeclareFloatingEnvironment[name=Doc.,listname={Liste des documents}]{docuflottant}
%\newsubfloat{docuflottant}

\newenvironment{docu}[1]{
    \refstepcounter{numdoc}
    \begin{docuflottant}
    \begin{tcolorbox}[title={\caption{#1}}]
    }{
    \end{tcolorbox}
    \end{docuflottant}
    }
    
\begin{document}

\begin{figure}
\centering
 Figure 1
 \caption{Figure 1}
\end{figure}

\begin{docu}{An interesting doc}
\lipsum[1]
 
 \begin{minipage}{\columnwidth}
 \begin{center}
  \begin{subfigure}{5cm}
   Figure 2a
   \caption{\label{fig2a}Figure 2a}
  \end{subfigure}
  \begin{subfigure}{5cm}
   Figure 2b
   \caption{\label{fig2b}Figure 2b}
  \end{subfigure}
  \end{center}
 \captionof{figure}{figure 2a et 2b}
 \end{minipage}
 
\end{docu}

I would like a reference to 'Figure 2a' to be figure~2a, and it is figure~\ref{fig2a}\dots



\end{document}

which gives: MWE compiled

I understand that 1a is for document 1, subfigure a, but I would like to have the subfigure condidered as a subfloat of a the figure, not the document.

How to do that? Thanks for reading, and for any usefull help!

1
  • I believe the caption package modifies the figure environment to handle the figure counter and hyperref target at the top of the figure rather than in \caption. Not so with a minipage. Dec 22, 2020 at 14:36

1 Answer 1

1

You could use \captionsetup{type=figure} inside of your minipage that contains the two subfigures. Instead of \captionof{figure}, you can then use \caption, resulting in the following output:

enter image description here

\documentclass{article}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{newfloat}
\usepackage{tcolorbox}

\newcounter{numdoc}
\renewcommand\thenumdoc{\arabic{numdoc}}
\DeclareFloatingEnvironment[name=Doc.,listname={Liste des documents}]{docuflottant}
%\newsubfloat{docuflottant}

\newenvironment{docu}[1]{
    \refstepcounter{numdoc}
    \begin{docuflottant}
    \begin{tcolorbox}[title={\caption{#1}}]
    }{
    \end{tcolorbox}
    \end{docuflottant}
    }
    
\begin{document}

\begin{figure}
\centering
 Figure 1
 \caption{Figure 1}
\end{figure}

\begin{docu}{An interesting doc}
\lipsum[1]
 
 \begin{minipage}{\columnwidth}
 \captionsetup{type=figure}
 \begin{center}
  \begin{subfigure}{5cm}
   Figure 2a
   \caption{\label{fig2a}Figure 2a}
  \end{subfigure}
  \begin{subfigure}{5cm}
   Figure 2b
   \caption{\label{fig2b}Figure 2b}
  \end{subfigure}
  \end{center}
 \caption{figure 2a et 2b}\label{fig:documentfigure}
 \end{minipage}
 
\end{docu}


I would like a reference to 'Figure 2a' to be figure~2a, and it is figure~\ref{fig2a}\dots



\end{document}
1
  • Thank you! I used it to create a new environnement with \newenvironment{NFfigure}{% non-floatting figure \begin{minipage}{\columnwidth} \captionsetup{type=figure} }{ \end{minipage} } which works smoothly within my full work. Dec 22, 2020 at 17:04

You must log in to answer this question.

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