0

Inside a macro I would like to establish a new type of float. One of the parameters to be set during that process is \ftype@TYPE, where TYPE is the float type: figure, table, and so on. I know, that \ftype@figure = 1 and \ftype@table = 2 normally are already set in the classes, e.g. report.cls, and I know, that each following float type gets a number, that is double the one before, so the next one would be 4, then 8, then 16 and so on.

What I don't know, though: How can one inside a macro find out, which is the highest float type number already in use -- for not using one twice?

13
  • \newfloat from float package? Commented Jul 18, 2023 at 23:43
  • See Defining a new type of floating environment
    – Werner
    Commented Jul 19, 2023 at 0:03
  • @DavidCarlisle Why does it double each time? (I'm just curious.)
    – cfr
    Commented Jul 19, 2023 at 1:18
  • 1
    @cfr the float type and !htbp allowed positions are packed into a 32 bit integer each item contributing 1 bit ie 1 power of 2 Commented Jul 19, 2023 at 1:55
  • 1
    what you are asking is exactly what \newfloat from the float package does, so I was suggesting you use that. Core latex does not keep record of already allocated floats, a class just has to "know", so using the float (or its related newfloat) package to handle the allocation is best. Commented Jul 19, 2023 at 15:27

1 Answer 1

4

Unfortunately latex does not provide a good answer here. Effectively the float package provides a standard \newfloat allocator but it has to guess the initial state, if you load it after floats other than figure and table are defined then they will be over-written by \newfloat.


If you don't want to force float to be loaded then I would suggest

a) test if \newfloat is defined, and if so use it.

b) if it is not defined set a value to 1 then test the usual suspects and double the value each time one is defined, to get the free number so perhaps test \c@figure\c@table\c@listings\c@algorithm


listings package does a simpler version of the same idea with

\AtBeginDocument{%
\@ifundefined{c@float@type}%
    {\edef\ftype@lstlisting{\ifx\c@figure\@undefined 1\else 4\fi}}
    {\edef\ftype@lstlisting{\the\c@float@type}%
     \addtocounter{float@type}{\value{float@type}}}%
}

so if \newfloat is defined in the preamble it uses the next free number from that allocation, otherwise hope for the best and use 1 or 4 depending whether figures are defined or not.

4
  • Starting from the listings package you cited, I added a definition of the counter float@type (if missing): \AtBeginDocument{% \@ifundefined{c@float@type}% {\global\newcounter{float@type}% \ifx\c@figure\@undefined% \setcounter{float@type}{1}% \else% \setcounter{float@type}{4}% \fi}% {\relax}% \edef\ftype@lstlisting{\the\c@float@type}% \addtocounter{float@type}{\value{float@type}}% }% So, if package float is used, like listings it uses the counter, if not, it defines it. Might that be better?
    – ThorstenL
    Commented Jul 21, 2023 at 20:45
  • I still have problems with writing comments: The one above somehow dropped the "@David Carlisle" and I don't know how to format the code ...
    – ThorstenL
    Commented Jul 21, 2023 at 20:53
  • @ThorstenL You can only @ ping once in a comment and since you did not code-quote the code, the @s are used up. You can inline quote `like this` but it loses all line breaks so is not good for code Commented Jul 21, 2023 at 21:00
  • Uhmm, I understand ... And how do I code-quote in a comment? In a question or an answer I have the symbol "{}" for that, I can click on, but I missed that in the comment ... I thought, it wasn't possible -- but I see from your comment, it is ... somehow ...
    – ThorstenL
    Commented Jul 22, 2023 at 22:30

You must log in to answer this question.

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