Foreword

We all know the two floating environments [cci_latex]table[/cci_latex] and [cci_latex]figure[/cci_latex] 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 [cci_latex]scheme[/cci_latex] environment. Although the latter is easily obtained using the [cci_latex]chemmacros[/cci_latex] package and its [cci_latex]scheme[/cci_latex] 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 [cci_latex]article.cls[/cci_latex] defines [cci_latex]figure[/cci_latex] or [cci_latex]table[/cci_latex]: [cce_latex]\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}[/cce_latex] 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 [cci_latex]newfloat[/cci_latex] is by the same author as the popular [cci_latex]caption[/cci_latex] and [cci_latex]subcaption[/cci_latex] packages. If you are using the [cci_latex]caption[/cci_latex] 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: [cce_latex]\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}[/cce_latex]

Using KOMA-Script

Instead of the [cci_latex]newfloat[/cci_latex] package you can use the [cci_latex]tocbasic[/cci_latex] package which is used by any KOMA-Script class. So if you are using such a class like [cci_latex]scrartcl[/cci_latex] or [cci_latex]scrbook[/cci_latex] 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: [cce_latex]\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}[/cce_latex] Floats defined this way obey KOMA-Script’s options for further customization

With the memoir class

The [cci_latex]memoir[/cci_latex] 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 [cci_latex]memoir[/cci_latex]’s definition of [cci_latex]figure[/cci_latex]. [cce_latex]\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}[/cce_latex]

Using the floatrow package

According to documentation of the [cci_latex]floatrow[/cci_latex] package the following code should work: [cce_latex]\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}[/cce_latex] 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 [cci_latex]floatrow[/cci_latex] package. However, I haven’t been able to debug it.

Using the float package

The [cci_latex]float[/cci_latex] package is the oldest of the packages in this lists but it still works: [cce_latex]\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}[/cce_latex] I usually don’t recommend it – mostly for personal reasons – but it is usable and makes the list complete.

2 thoughts on “How to define a new floating environment

  1. Alexei says:

    Hi Clemens,

    let’s suppose that I have a memoir class document, and I need to add two (or more) types of schemes.

    1
    \begin{scheme}
    \caption{Basic Scheme type}
    \end{scheme}

    2
    \begin{otherScheme}
    \caption{Other Scheme type}
    \end{otherScheme}

    How to create two types of lists apart for each type of schemes?

    I tried to replicate same code with different names and now I have two lists with identical content and they share the same title (of the second list). This way is wrong, right? Can you suggest where to dig?

    1. Clemens says:

      You probably used the same file for the list. I used .los in the example (spelling list of schemes). You might use .lox or something else for your second float.

Leave a Reply

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