Please I have a question regarding figure and table placement. I am writing my thesis and the guidelines requires a figure or a table to appear after 3 lines of the last sentence.Is there any way to do that in latex. Thanks
[Tex/LaTex] Figure placement
floats
Related Solutions
The problem lives in the code of the float
package: If the placement [H]
is used, \endfigure
will be redefined locally with no respect on the original definition of \endfigure
:
\def\@float@HH#1[H]{%
% Locally redefine the end of the environment.
\expandafter\let\csname end#1\endcsname\float@endH
...
As consequence the following code compiles fine, although \jhzsweiurziuewzriuewzritewt
is (obviously) not defined at all and therefore should give an error when used:
\documentclass{article}
\usepackage{mwe}
\makeatletter
\renewenvironment{figure}[1][\fps@figure]
{\@float{figure}[#1]before}
{\jhzsweiurziuewzriuewzritewt} % not defined but compiles fine anyway ;-)
\makeatother
\usepackage{float}
\begin{document}
\begin{figure}[H]
\includegraphics[width=.8\linewidth]{example-image}
\end{figure}
\end{document}
What can we do about it? Patching \float@endH
as well. We do this inside the redefinition of figure
(and not globally) so the patch does not affect the other floating environments:
\documentclass{article}
\usepackage{mwe}
\makeatletter
\renewenvironment{figure}[1][\fps@figure]
{% Next two code lines: Adaption to "HERE" feature of the float package
\let\float@endH@ORI\float@endH
\def\float@endH{after\float@endH@ORI}%
% ...or \pretocmd{\float@endH}{after}{}{} when using `etoolbox`
\@float{figure}[#1]before}
{after\end@float}
\makeatother
\usepackage{float}
\begin{document}
\begin{figure}[H]
\includegraphics[width=.8\linewidth]{example-image}
\end{figure}
\end{document}
The base latex release includes the flafter
package that ensures floats never float "back" to the top of the current page before their definition in the source. So you just need to place the figure
environment at or after its first \ref
to meet your requirement.
Best Answer
Basically LaTeX handles figures, tables and similar objects as floats. LaTeX's internal algorithm "tries" various positions on a page to find the best place for the float. You can control the places that LaTeX is allowed to use while finding the best position via an optional argument at the beginning of your figure:
Here t means top (of the page), b means bottom, h means here and as far as I know the ! means that LaTeX should ignore the other possible places (in this case none).
Nevertheless this still doesn't solve your problem. Well, there is another possible specifier for the argument, namely H. With H LaTeX ignores all other positions and places the figure at the same position as in your LaTeX source. Thus, you could use this as a dirty trick to place your figure:
Still this is not the recommended way to place a float. Please also note that placing a float that way can cause overfull vertical boxes and therefore is not a good manner.
I recommend to read this post, since it gives more information about floats and generally makes my answer obsolete.