2

I am currently writing my own document class (which is very inspired by the book class) and I am facing a problem when using the caption and subcaption packages.

Here's a part of my classemphasized text:

\RequirePackage{caption}
\RequirePackage{subcaption}

\newcounter{figure}

\newenvironment{figure}{%
    \@float{figure}%
}
{\end@float}

\renewcommand\thefigure{%
    \@arabic\c@figure%
}

\renewcommand\thesubfigure{%
    \arabic{subfigure}%
}

\newcommand\figurename{Fig}

\def\fps@figure{tbp}
\def\ftype@figure{1}
\def\ext@figure{lof}
\def\fnum@figure{\figurename\nobreakspace\thefigure}

\DeclareCaptionLabelFormat{format}{\textbf{\textsc{#1.~#2. -- }}}

\captionsetup[figure]{
    name={Fig},
    labelsep=none,
    labelformat=format,
    textformat=simple,
    justification=justified,
    singlelinecheck=true,
    font=footnotesize,
    textfont=it,
    position=bottom,
}

\captionsetup[subfigure]{
    name={Fig},
    labelsep=none,
    labelformat=format,
    textformat=simple,
    justification=justified,
    singlelinecheck=true,
    font=footnotesize,
    textfont=it,
    position=bottom,
}

For the simple figures, no problem, everything goes perfectly well. But when I try to insert subfigures I get the following error concerning the document class:

Command \thesubfigure undefined

Do you know where this mistake came from? I've read the documentation of both packages but I'm not very advanced. . .

This is the first time I write my own document class, it's quite different from a "classical" use of LaTeX, so please excuse me if my class is a makeshift job. Thanks

PS : Here is link for the document class : MyClass.cls

The figure environment is defined on lines 382 to 410 and the caption on lines 452 to 510.

And here is the link for .tex file : Test.tex

4
  • 1
    Move \newcounter{figure} before \RequirePackage{subcaption} or manually use \DeclareCaptionSubType[*]{figure} (see doc of subcaption, sec. 5). When loading subcaption, \DeclareCaptionSubType is called automatically for figure and table, if the corresponding counters are defined. Commented Mar 23, 2020 at 15:45
  • Hello @muzimuzhiZ, thank you for your help, however, even doing so, the error message persists... :/
    – B Legrand
    Commented Mar 23, 2020 at 15:58
  • It seems you are defining a new document class from scratch. Can you provide a complete example, containing the contents of both .cls and .tex files? Commented Mar 23, 2020 at 16:21
  • @muzimuzhiZ Yes indeed I made this class from scratch to be able to fully customize it and better understand how LaTeX works. Be careful, my class document is still in work, so it is not very "clean" and it contains a lot of comments in French.
    – B Legrand
    Commented Mar 23, 2020 at 16:49

1 Answer 1

2
  • When loading subcaption, it automatically calls \DeclareCaptionSubType{figure} if counter figure is defined.
  • Inside \DeclareCaptionSubType{figure},
    • command \ext@figure is expanded. (Actually this happens inside the expansion of \caption@@@@declaresublistentry, defined in caption3.sty)
    • new counter subfigure is defined, hence the corresponding command \thesubfigure is defined.

For counter table, similar things happen.

So the key is to provide counter <float type> and command \ext@<float type> before calling \DeclareCaptionSubType[*]{<float type>}.

A full example

\documentclass{minimal}

\newcounter{figure}
\newcounter{table}

\makeatletter
\def\ext@figure{lof}
\def\ext@table {lot}
\makeatother

\RequirePackage{subcaption}

\renewcommand{\thesubfigure}{...}
\renewcommand{\thesubtable} {...}

\begin{document}
abc
\end{document}

You must log in to answer this question.

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