# What is the exact purpose of \ftype@<TYPE>?

I currently working on some float related code an saw that the standard float environments figure and table define (beside others) the macros \def\ftype@figure{1} and \def\ftype@table{2}.

The LaTeX source documentation source2e "describes" these macros as:

\ftype@TYPE : The type number for floats of type TYPE.


which is not that meaningful.

The only place these macros are used seem to be in \@xfloat which is used by the standard floats. There is defines the value of a count register (Pseudo code from source2e documentation):

\count\@currbox :=G 32*\ftype@TYPE + bits determined by PLACEMENT


I figured that it has something to do with the float placement and/or boxing process. I looked into some float related packages like float and caption and learned that each new float type requires a number which is a power of 2, i.e. the next float would need to be 4, then 8 etc. This makes \count\@currbox look like a bitmap to me. However, I have difficulties to understand it further.

What is the the exact reason and usage of \ftype@<TYPE> and \count\@currbox? If I define my own floats is it OK if I exponentially increase it for every new float type?

-
IMO, unlike tex-core a question which concerns LaTeX core functions doesn't need an additional tag. –  lockstep Oct 22 '11 at 15:30
@lockstep: I hesitated adding it. I know we shouldn't have a latex tag, but normal LaTeX questions aren't about the real internals of LaTeX, so the ones which are are special and therefore latex-core might actually make sense. –  Martin Scharrer Oct 22 '11 at 15:34
Yes it might, but a) such a tag is likely to be misused by new users b) the tag should probably be added to dozens of old questions. Especially because of a), I suggest you ask a question at meta about the usefulness of latex-core before (re-)creating the tag. –  lockstep Oct 22 '11 at 15:41
@lockstep: I was thinking about both of that [I mean a) and about a meta-post] but then decided to be simply pre-active about it. Sorry for that. Anyway, having that tag isn't important to me. BTW, I was thinking to add macros as well (which you did now) but the tag description didn't really fit the question. –  Martin Scharrer Oct 22 '11 at 15:47
No need to be sorry -- the tag is a potentially useful one, and so I left a comment when I deleted it. –  lockstep Oct 22 '11 at 15:51

## 3 Answers

Each floating box is stored in a box register \bx@A, \bx@B and so on. To each of these box registers there corresponds a counter that is loaded with a number that represents the float type and the position specifiers. So for each float LaTeX can know what were the position specifiers (h, t, b, p or !) and decide what to do with them according to the constraints imposed by the page to output. The first five bits are reserved for the specifiers, then comes the type.

For example, with

\begin{figure}
a
\end{figure}
\begin{figure}[h]
b
\end{figure}
\begin{table}
c
\end{table}


we get

\count\bx@A=62
\count\bx@B=49
\count\bx@C=94


In binary form 62 is 111110, 49 is 110001, 94 is 1011110.

In the first we see that h is not specified, while tbp are (the default for article): h is bit 0, t is bit 1, b is bit 2 and p is bit 3. The ! subtracts 16 and indeed \begin{figure}[!htbp] gives 41, that is 101111. So, a zero in bit 4 means "relax the constraints".

By the way, \bx@A is 252, \bx@B is 251, and so on. The insertion classes 254 and 253 are, respectively, \@mpfootins and \footins.

-
Very interesting. That explains the lower bits of \count\@currbox. However, I still don't understand why \ftype@<TYPE> must be a power of two. –  Martin Scharrer Oct 22 '11 at 16:33
I too: it's not written in source2e and to get the float type it's sufficient to divide \count\bx@X by 32, since TeX truncates. This division is performed by \@xtryfc. –  egreg Oct 22 '11 at 16:45
Is there a back mapping from the number to the float type? Or is this number only required to keep floats from different types distinguishable? –  Martin Scharrer Oct 22 '11 at 17:08
@MartinScharrer Not that I know and I can't find any trace of it. –  egreg Oct 22 '11 at 17:13
@Martin: AFAIK it does not need to be a power of two. (I had copied this from the float package in the past, and never have changed it.) And the implementation within the float package is buggy anyway, giving each new float type defined with \newfloat the same number. –  Axel Sommerfeldt Oct 22 '11 at 17:58

Just to expand a bit on what egreg wrote.

LaTeX needs to construct an efficient way of storing flags for both type of float identifiers as well as placement identifiers. So the LaTeX Team combined them into one bit string. The first 5 bits are reserved for the specifiers and the rest for the floats.

     Position  76543210
Value     0010010

Bit      Meaning
---      -----------
0       1 if the float may go where it appears in the text
1       1 if the float maytop of page
2       1 if the float may go on bottom of page
3       1  if the float may go on float page
4       1 umless the placement includes a  !
5       float type figures
6       float type tables


In order to add a different float type, one needs to move by one position to the left. A left arithmetic shift by n is equivalent to multiplying by 2^n and hence in this case is in powers of two.

Type 1=2^0 is a figures, type 2=2^1 tables, type 4=2^1 code, type 8=2^3 something else etc...

Oberdiek's flags macros, now called bitset, might be useful if you are going to be tinkering with this sort of thing. PDF's use similar flags for pdf objects to set properties.

Since TeX’s number limit of 2^31-1 and the first 5 bits are taken by the float identifiers there remain 26 available float types for the adventurous.

-
But why are bit 5 and up not used as a simply counter? A float hardly is a figure and a table at the same time! –  Martin Scharrer Oct 23 '11 at 0:04
Think of it as an object attached to each float. When all floats are in a list it is more efficient to use bitor to determine if there is a float of a certain type and the lists are manipulated via @elt. If you use a counter, you will need more than 1, so I think it was done this way to minimize counters. They shouldn't have minimized the documentation though:) –  Yiannis Lazarides Oct 23 '11 at 1:59
Another way could be with a conditional, but wasting three control sequences per float type. When LaTeX2e was released such optimizations were necessary. –  egreg Oct 25 '11 at 21:44
@egreg the algorithm wasn't invented for 2e it dates back to Leslie's original design for LaTeX2.09. And yes space and efficiency was extremly important in those days. –  Frank Mittelbach Feb 29 '12 at 16:30
@FrankMittelbach At what level would the OTR be handled in LaTeX3? I see we have a tex_output, but no code. –  Yiannis Lazarides Feb 29 '12 at 16:36

I'm fairly sure that there is a lot of improvement possible in the LaTeX2e source documentation. However, the statement given in the question that there isn't any useful information about the float type number is not quite true. The documentation section on floats (in ltfloats.dtx) starts of with

% \section{Floats}
%
%  The different types of floats are identified by a \meta{type} name,
%  which is the name of the counter for that kind of float.  For
%  example, figures are of type figure' and tables are of type table'.
%  Each \meta{type} has associated a positive \meta{type number}, which
%  is a power of two.  E.g.,\\
%  figures might be have type number~1, tables type number~2, programs
%  type number~4, etc.


So it is clearly stated that the type has to be a power of 2 even if there is no reason given or an explicit documentation at this point how the algorithm makes use of this number.

There is also (quite technical :-) documentation in the pseudo code by Leslie on the original algorithm, e.g.,

%  \@bitor\NUM\LIST : Globally sets switch @test to the disjunction for
%         all I of bit  log2 \NUM of the float specifiers of all the
%         floats in \LIST.
%         I.e., @test is set to true iff there is at least one
%         float in \LIST having bit  log2 \NUM  of its float specifier
%         equal to 1.
%
%  Note: log2 [(\count I)/32] is the bit number corresponding to the
%  type of float I.  To see if there is any float in \LIST having
%  the same type as float I, you run \@bitor with
%    \NUM = [(\count I)/32] * 32.


Now my position here is that this wasn't forseen as an area where anybody was (and is) supposed to hook in on a low-level and that such technical parts therefore do not need further explanation other than providing info that one has to provide 4 interface macros with a certain characteristics per type of float, e.g.,

\def\fps@table{tbp}
\def\ftype@table{2}
\def\ext@table{lot}
\def\fnum@table{\tablename\nobreakspace\thetable}


The other 5 people in the the world that were expected to look in detail into the algorithm are those that write a package like floats and given that the technical aspect of the algorithm is actually not that badly documented.

And now the answers by @egreg and @Yiannis on this site give a very good technical summary what is happening in LaTeX2.09 and LaTeX2e.

-
Thanks -- Just to add a comment that there is a more layman's description at tex.stackexchange.com/questions/8212/…. You may want to have a look for any mistruths:) Would appreciate additional links. –  Yiannis Lazarides Feb 29 '12 at 17:19
@Yiannis I have booked marked that one to study in detail and perhaps write up an answer one day –  Frank Mittelbach Feb 29 '12 at 17:23
Thanks - Looking forward to your write up:) –  Yiannis Lazarides Feb 29 '12 at 17:26
@Yiannis I'm not saying there is one, first study then decide –  Frank Mittelbach Feb 29 '12 at 17:38