# Let LaTeX decide where to place figures

There are too many posts that complains LaTeX is not doing a good enough job in placing floats such as figures, and ways to work around them ([!htbp], \usepackage{float}, etc.). Upon curiosity I cannot help thinking it the other way around. If I am satisfied with the mechanism that LaTeX position the floats and I know exactly which figures I want to appear "here" (the first time they are referenced), which I am happy to place on the top of the pages, and which I am happy even if they appear on a separate page, and that I am feeling lazy (in the sense that I want LaTeX to make the decisions for me),

Is there a way to put the source code for all the floats (you may restrict the discussion to figures if you like) in a tex file that is to be \input or \includeed, with all the necessary position specifiers given, and let LaTeX automatically insert them where appropriate by referencing them?

Side question: I don't understand why people should use floats at all if they are using the [H] specifier of the float package.

Edit: I prefer pure LaTeX solutions but I don't mind learning something new if solutions in LuaLaTeX or alike are given.

Edit 2: The current implementation I have in mind is closely related to Move command to the beginning or end of the paragraph.

• I believe the use of [H] is to allow the use of \caption instead of \captionof (or \def\@captype{figure}). Either that or they don't quite understand what the figure and table environments are for. – John Kormylo Apr 23 at 14:56

If I understand you correctly, this can be implemented easily.

\documentclass{article}

% for example images
\usepackage{mwe}

% xparse is no necessary but allows a more flexible interface design
\usepackage{xparse}
\usepackage{graphics}

\makeatletter
\NewDocumentCommand\placefig{m O{} m m}{%
\begin{figure}%
\centering
\includegraphics[#2]{#3}%
\caption{#4}%
\label{#1}%
\end{figure}%
}
\NewDocumentCommand\deffig{m O{} m m}{
\expandafter\gdef\csname fig@r@#1\endcsname{%
\placefig{#1}[#2]{#3}{#4}%
\expandafter\global\expandafter\let\csname fig@r@#1\endcsname\relax
}%
}
\let\old@ref\ref
\renewcommand*\ref[1]{%
\old@ref{#1}%
\ifcsname fig@r@#1\endcsname\csname fig@r@#1\endcsname\fi
}
\makeatother

% define the (possible) figures
\deffig{fig:ex-a}[width=.5\textwidth]{example-image-a}{This is an example image.}
\deffig{fig:ex-b}{example-image-b}{This is another example image.}

\begin{document}

This is the first reference to fig.~\ref{fig:ex-a} in the document.

Here, we reference fig.~\ref{fig:ex-a} again.

\end{document}


If you use packages that modify the referencing mechanism (like hyperref), you will have to adjust the modification of \ref accordingly.

Beware that references are allowed in locations where floats are not (e.g. inside tables). You can add more commands to allow placing a defined float without referencing it to circumvent this problem.

If you do not wish all your floats to follow one pattern (given by \placefig), you could modify \deffig to allow direct input of the float environment.

However, given these caveats, I am unsure of the usefulness of this approach. But I guess you can decide that for yourself.