3

I have a class file that I am writing (it's my first one and I am new to LaTeX). I have a situation where I'd like to know if I am inside a float so that I can prevent attempting to create a nested one. I found How can I detect if I'm inside or outside of a float environment? but this didn't work. I tried this (from the answer to that question):

\makeatletter   
\def\InFloat{\ifnum\@floatpenalty<0\relax in float \else outside float \fi}
\makeatother

But it always displays "in float" wherever I place "\InFloat" in my document. I have also found How to detect if in an exercise environment? and tried this:

\newcommand{\InFloat}{\ifinner inside-float\else outside-float\fi}

but that doesn't work either. I've been trying to find an answer but have been unsuccessful so far...

Updates

Thanks for the replies, I have tried this:

\makeatletter
\def\InFloat{\@ifundefined{\@captype}{OUTSITE}{INSIDE}}
\makeatother

but I got this in the output "Undefined control sequence. \@captype".

I should add a bit of background about what I am trying to do, and why. I have a number of documents that are text based but marked up using some proprietary markings. I want to convert these so I can use LaTex going forwards. The mark-up is such that it is easily translated into LaTeX with a sed script (I am writing commands and environments in a class file make those marked up sections render as needed - the sed script substitutes the mark-up for the commands/environment begin/end that I have created).

Two of the mark-ups are relevant to this question. One defines a box and I have implemented an environment using boxedminipage for that. The other defines an image (or actually potentially multiple images, a caption and some text). I have implemented an environment using figure for that one. They both work fine, except for the possibility that the image mark-up can fall inside the box mark-up. It is this case I want to detect and simply not create the inner figure in the case when the image mark-up appears inside a box mark-up. Hopefully that makes sense.

starfry
  • 585
  • Hello! Would \makeatletter\def\InFloat{\@ifundefined{\@captype}{OUTSITE}{INSIDE}}\makeatother work? (It is untested.) – yo' Feb 25 '13 at 09:36
  • @tohecz suggestion is probably as good as any (it will also be true in some non-float environments if you use \captionof for example, but they are acting like floats so you probably want the in-float behaviour in that case. But you should think hard if you really want to do this. It is best for class files not to change LaTeX conventions to much, and in standard latex markup it is always explicit whether or not an environment is floating. – David Carlisle Feb 25 '13 at 09:46
  • Just a comment, but perhaps there are better ways to do this. You could reformulate the question as to what the actual problem is your trying to prevent (nesting floats), rather than making it seem the problem is specifically checking if you're in a float (which is only your idea of a solution). It might attract additional/other people to the question who have dealt with this before, since it seems to me like a universal class file issue. Or perhaps it isn't an issue at all since it doesn't matter in latex, but I wouldn't know about that ;-) – Mythio Feb 25 '13 at 09:49
  • @DavidCarlisle I posted it as a comment, because if it is a solution, then I don't see any difference between this question and that one. Therefore I would post it as a solution there, and the questions could be linked/merged. – yo' Feb 25 '13 at 09:54
  • @tohecz I agree the question is a duplicate and your answer applies to both (which answer is required depneds n whether float-like things like [H] should be classed as float or not, the penalty test just tests for real floats. – David Carlisle Feb 25 '13 at 10:12
  • I have updated my question with more detail. Regarding this being a dupe I did not think the other question (which I did quote in my question) contained any explanation - just a solution in a block of code which is difficult for a newcomer to LaTeX to understand. So, I wanted to present my own case specifically. I've now updated my question with more information to try and explain what it is that I am trying to do. – starfry Feb 25 '13 at 13:38
  • Please, I made a mistake in my code, it should be \makeatletter\def\InFloat{\@ifundefined{@captype}{OUTSITE}{INSIDE}}\makeatothe‌​r. I will add it as an answer to the other question, and I will possibly explain the answer that is already there. The fact that that answer is not explanatory somehow doesn't influence the fact that they ask the same thing. – yo' Feb 25 '13 at 14:29
  • @tohecz - that you yes that does now satisfy my use-case. I will look out for your answer/explanation on the other question as I may learn something from it. much appreciated. – starfry Feb 25 '13 at 16:25
  • @starfry Done: http://tex.stackexchange.com/a/99885/11002 – yo' Feb 25 '13 at 16:44

0 Answers0