How to define a new floating environment

Foreword

We all know the two floating environments table and figure which almost all document classes provide. Every once in a while there is the need for another floating environment. For example chemists often need something like a scheme environment. Although the latter is easily obtained using the chemmacros package and its scheme module I want to show the several methods I know for defining new floats.

Manual definition

The manual definition is a bit tedious as there need to be quite a number of internal macros defined for a float type. The code below demonstrates this for a float named “scheme” defined analogous to how article.cls defines figure or table:

\documentclass{article}

\makeatletter
\newcounter{scheme
}% the counter
\renewcommand*\thescheme{\arabic{scheme}}% the default format for the counter
\newcommand*\fps@scheme{tbp}% default floating options
\newcommand*\ftype@scheme{4}% float type number, needs to be a power of 2;
                            % \ftype@figure=1, \ftype@table=2
\newcommand*\ext@scheme{los}% extension of file for the list of schemes
\newcommand*\fnum@scheme{\schemename\nobreakspace\thescheme}% used in caption
\newcommand*\schemename{Scheme}% the name
\newcommand*\listschemename{List of schemes}% the name of the list
\newenvironment{scheme}{\@float{scheme}}{\end@float}% the environment
\newenvironment{scheme*}{\@dblfloat{scheme}}{\end@dblfloat}% the starred
% version for twocolumn documents
\newcommand*\listofschemes{% the list of schemes
  \section*{\listschemename}
  \@mkboth
    {\MakeUppercase\listschemename}
    {\MakeUppercase\listschemename}
  \@starttoc{\ext@scheme}
}
\let\l@scheme\l@figure % layout of list is the same as for figures
\makeatother

\begin{document}

\begin{scheme}
  \caption{bla bla}
\end{scheme}

\listofschemes

\end{document
}

Many details are hardcoded here which means the code isn’t really flexible. But on the other hand it doesn’t need to be as it can be changed at any time (supposing you have it in your preamble).

Using the newfloat package

The newfloat is by the same author as the popular caption and subcaption packages. If you are using the caption package anyway then this is definitely the choice for defining new floating environments. You’ll be able to use the full power of both packages for customization. Also: the code is a lot easier:

\documentclass{article}
\usepackage{newfloat}

\DeclareFloatingEnvironment[
  fileext = los ,
  listname = {List of schemes} ,
  name = Scheme
]{scheme}

\begin{document}

\begin{scheme}
  \caption{bla bla}
\end{scheme}

\listofschemes

\end{document
}

Using KOMA-Script

Instead of the newfloat package you can use the tocbasic package which is used by any KOMA-Script class. So if you are using such a class like scrartcl or scrbook you don’t have to load the package. The code is similarly easy. The following code indicates that there are a number of options for customization:

\documentclass{article}
\usepackage{tocbasic
}% or a KOMA-Script class like `scrartcl'

\DeclareNewTOC[
  type = scheme ,
  % types = schemes ,
  float ,
  % name = Scheme ,
  % listname = {List of Schemes}
]{scheme}

\begin{document}

\begin{scheme}
  \caption{bla bla}
\end{scheme}

\listofschemes

\end{document
}

Floats defined this way obey KOMA-Script’s options for further customization

With the memoir class

The memoir class also brings its own mechanism for defining floats. Unfortunately it is not as straightforward as you would think and also not as easy as the manual says. The following definition is analogous to memoir‘s definition of figure.

\documentclass{memoir}

\newfloat[chapter]{scheme}{los}{Scheme}
\renewcommand*\thescheme{\thechapter.\arabic{scheme}}
\newlistof{listofschemes}{los}{List of schemes}
\newlistentry[chapter]{scheme}{los}{0}
\cftsetindents{scheme}{0em}{2.3em}

\makeatletter
\addtodef\@memfront@floats{\counterwithout{scheme}{chapter}}
\addtodef\@memmain@floats{\counterwithin{scheme}{chapter}}
\addtodef\@memback@floats{\counterwithin{scheme}{chapter}\setcounter{scheme}{0}}
\makeatother
\ifartopt
  \counterwithout{scheme}{chapter}
\fi

\begin{document}

\begin{scheme}
  \caption{bla bla}
\end{scheme}

\listofschemes

\end{document
}

Using the floatrow package

According to documentation of the floatrow package the following code should work:

\documentclass{article}

\usepackage{floatrow}

\DeclareNewFloatType{scheme}{name=Scheme}
\newcommand*\listofschemes{\listof{scheme}{List of schemes}}

\begin{document}

\begin{scheme}
  \caption{bla bla}
\end{scheme}

\listofschemes

\end{document
}

But in fact it doesn’t. While the floating environment itself is defined and works the list stays empty. This strongly suggests a bug in the floatrow package. However, I haven’t been able to debug it.

Using the float package

The float package is the oldest of the packages in this lists but it still works:

\documentclass{article}

\usepackage{float}

\newfloat{scheme}{hbp}{los}
\floatname{scheme}{Scheme}
\newcommand*\listofschemes{\listof{scheme}{List of schemes}}

\begin{document}

\begin{scheme}
  \caption{bla bla}
\end{scheme}

\listofschemes

\end{document
}

I usually don’t recommend it – mostly for personal reasons – but it is usable and makes the list complete.

Leave a Reply

Your email address will not be published. Required fields are marked *