4

I knew that floats float around and sometimes are placed far away from where one wants them to be. With the times I learned to work with it, and there are already many posts about it.

I have a kind of different problem. I'm writing my bachelor thesis and defined some environments for definitions and such. And of course I'm using floats as well. But now I have the problem that one of my floats are placed in one of my definitions. This is of course something I really don't want to have. If some of the floats are one page after or before they are referenced, that is ok, but having them in my definitions is no option.

I don't know how to create a MWE for this without posting my whole thesis, but this is as close as I can get:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{graphicx}

\newenvironment{myblock}{% \vspace{.5\baselineskip} \hrule\nopagebreak\vspace{.35\baselineskip}\nopagebreak} {\nopagebreak\vspace{.5\baselineskip}\nopagebreak\hrule\vspace{.5 \baselineskip}\noindent}

\newcounter{definition}[section] \renewcommand*\thedefinition{\thesection.\arabic{definition}} \newenvironment{definition}{% \refstepcounter{definition} \begin{myblock} \noindent\textbf{Definition \thedefinition} } {\end{myblock}}

\begin{document} \Blindtext \section{MWE} \begin{definition} \blindtext \end{definition} \begin{figure} \includegraphics[scale=.5]{lorem} \end{figure} \Blindtext \end{document}

This is the picture I used for this example. The pciture I used in the MWE

Well in this example the picture isn't placed that bad, but in my thesis it is placed inside the definition environment.

So my real question is, how can I keep the figure out of that environemnt?

PS: If you see any problems with my definition environment that is not related to this question, please let me know. I'm always happy to learn something new.

Dave
  • 1,561
  • 1
    you might try putting a blank line before \begin{figure}. i haven't checked, but \end{definition} might check what follows to figure out what to do next. a blank line (or \par) will make it clear that it should "stand alone". another possibility: your definition has a page break in the middle, and the figure is at the top of the second page. that's harder to deal with. – barbara beeton Jul 08 '14 at 18:04
  • 3
    you can use a combination of the flafter package and \FloatBarrier from the placeins package. this kind of thing has been asked in a few other questions, but don't worry, it can be hard to find when you're not sure what to search for :) – cmhughes Jul 08 '14 at 18:05
  • 2
    you should really define definition using \newtheorem (the standard one or theorem or amsthm package variants) doing \noindent\textbf{Definition \thedefinition} you get none of the control pf page breaking that normally happens around latex headings and theorem heads, similarly ending the environment with \noindent} is dangerous as it starts a paragraph so does the wrong thing if a blank line or display environment follows the block. – David Carlisle Jul 08 '14 at 18:19
  • 3
    keeping the figure out of an environment that allows page breaking is a bit hard to do automatically (as the environment is already set by the time the figure is placed) simplest is just not to worry about it until the document is almost complete then use [pb] to prevent the figure going at top of page, or simply move it later in the source. – David Carlisle Jul 08 '14 at 18:21
  • If you put the figure environment after the definition environment with placement parameter [hbp] or before the definition environment with placement [ht], then a definition that spans a pagebreak should normally not be interupted by the figure. – Dan Jul 08 '14 at 19:31
  • I've been playing with that idea (floating definition) but find that [h] works best. Otherwise the definition can appear before the start of the section. – John Kormylo Jul 08 '14 at 19:34
  • 1
    There is no rule that forces you to place a picture in a figure environment; if a picture has to stay somewhere and not to float, it won't need a caption. Just use \includegraphics. – egreg Jul 08 '14 at 20:05

1 Answers1

0

To show what I mean:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{mwe}

\newenvironment{myblock}{%
\vspace{.5\baselineskip}
\hrule\nopagebreak\vspace{.35\baselineskip}\nopagebreak}
{\nopagebreak\vspace{.5\baselineskip}\nopagebreak\hrule\vspace{.5 \baselineskip}\noindent}

\newcounter{definition}[section]
\renewcommand*\thedefinition{\thesection.\arabic{definition}}
\newenvironment{definition}{%
\refstepcounter{definition}
\begin{myblock}
\noindent\textbf{Definition \thedefinition}
}
{\end{myblock}}

\begin{document}
\Blindtext
\section{MWE}
%\lipsum[1-3]% adjusted to force float to next page
\begin{figure}[h]
\begin{definition}
\blindtext
\begin{center}
\includegraphics[scale=.5]{example-image}
\end{center}
\end{definition}
\end{figure}

\Blindtext
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120