8

I'm trying to redefine an existing environment subfigure defined by the subcaption package. To do this, I'm using xparse's DeclareDocumentEnvironment since that's suppose to declare a new environment no matter if it already exists or not. But for some reason I still get a compilation warning that the \subfigure command already exists.

Here's a MWE:

\documentclass{article}

\usepackage{subcaption}
\usepackage{xparse}

\DeclareDocumentEnvironment{subfigure}{m}{%
  % Do nothing
}{%
  \subcaption{#1}%
}

\begin{document}
\begin{figure}
  \begin{subfigure}{The caption}
    The figure
  \end{subfigure}
\end{figure}
\end{document}

Why doesn't this work as intended?

A workaround is of course to simply use another name for my custom environment, but I'm curious why this doesn't work as I want it to.

1
  • The error comes from subcaption that wants to define subfigure when \AtBeginDocument.
    – egreg
    Jun 24, 2015 at 12:41

1 Answer 1

8

The subcaption package delays the definition of subfigure and subtable when executing the \AtBeginDocument hook. So the error is because \newenvironment{subfigure} wants to define an already existing environment. Indeed, you get exactly the same error if you use \NewDocumentEnvironment.

If you do

\AtBeginDocument{%
  \DeclareDocumentEnvironment{subfigure}{m}{%
    % Do nothing
  }{%
    \subcaption{#1}%
  }%
}

you get no error.

5
  • Hm. Any idea why subcaption does that? Why not define the environments directly as subcaption.sty is being read?
    – gablin
    Jun 24, 2015 at 12:58
  • Interestingly, subcaption.sty doesn't define a subfigure environment – at least none that I can see… Jun 24, 2015 at 13:09
  • @gablin The package wants to make several checks, so it must ensure as hard as it can that all packages are already loaded when it does them.
    – egreg
    Jun 24, 2015 at 13:24
  • @SeanAllred Yes, it does \newenvironment{sub#1}{...}{...} for #1 equal to the already defined and registered float types.
    – egreg
    Jun 24, 2015 at 13:25
  • I saw that and thought the same, but I couldn't for the life of me find the origin of that. Usings emacs' up-list function stopped well before I saw any indication of a macro definition. Am I making any sense? Edit: Unless that's what \caption@For does, akin to expl3's map-inline functions? Jun 24, 2015 at 13:54

You must log in to answer this question.

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