5

I know, it's an old topic and I already know about the general functionality of float environments in LaTeX. I want to give it as much freedom as possible and generally like the way of placing floats.

But I have one restriction in writing my thesis: The figure or table must be placed before or on the page of its first references. I.e., I have a reference to figure 1 on page 3, the figure is allowed to be placed on 1, 2 or 3, but not on >=4.

I know that LaTeX will never place the float before its first appearance in the source code, so in the source, the float must be before the reference. But how can I control, that it's not moved too far to the end? Putting all float at the beginning of the section wouldn't make sense, because some of them are then placed too early.

I hope you can help me in this question.

Thank you very much.

  • 1
    the positioning of floats requested here is exactly the reverse of the usual one (that a float must appear on the same page or after its reference). i think the only practical thing to do in this case may be to insert the source of each figure just before the first reference, process it to see what actually happens, then move the figures as necessary earlier in the file until stability is reached. good old trial and error. – barbara beeton Jul 14 '15 at 14:58
  • Thanks for helping. Unfortunately, trial and error is what I'm doing now, but on a >100 pages thesis, I'd prefer an automation.. – ju. Jul 14 '15 at 15:06
  • 2
    @ju. you may just prefer to use minipage and \captionof from the caption or capt-of packages, or [H] from the float package and position your figures "by hand" that way they will stay where you put them. Otherwise you are always going to be fighting the float placement algorithm which is absolutely not designed to help you here. – David Carlisle Jul 14 '15 at 15:45
  • what do you mean by first reference? and first appearance? – touhami Jul 15 '15 at 0:02
  • may be related tex.stackexchange.com/questions/97494/… – touhami Jul 15 '15 at 0:06
1

The floating behaviour and exchange is very intricate. My suggestion would be to lay out your document as you normally would without regard for any of the \label-\ref requirements you mention. And then, at the end, worry about possibly shuffling things around.

What I propose is to tap into \ref and let it give you an indication of whether or not the \label it references is occurs before or after the current position. Below I've redefined \ref to issue a warning if it doesn't meet your restrictions - things is something you can easily check at the end of your compilation. Since the suggested solution uses the \label-\ref system itself, you may have to compile at least twice on the first go.

Here is a minimal example that can be adapted:

\documentclass{article}

\usepackage{lipsum,graphicx,refcount}

\makeatletter
\setrefcountdefault{99999}
\let\oldref\ref
\renewcommand{\ref}[1]{%
  \label{#1*}%
  \ifnum\getpagerefnumber{#1*}<\getpagerefnumber{#1}% \ref on page < \label
    \@latex@warning{...figure `#1' is on page \getpagerefnumber{#1},
      but referenced on \getpagerefnumber{#1*}}%
  \fi
  \oldref{#1}%
}
\makeatother

\begin{document}

\sloppy% Just for this example

\tableofcontents

\listoffigures

\section{A section}

\lipsum[1-50]

Figure~\ref{fig:first}. % This is fine...
Figure~\ref{fig:second}.% ...but this creates a warning.

\begin{figure}[ht]
  \centering\includegraphics[width=.5\linewidth]{example-image}
  \caption{A figure}\label{fig:first}
\end{figure}

\lipsum[1-50]

\begin{figure}[ht]
  \centering\includegraphics[width=.5\linewidth]{example-image}
  \caption{A figure}\label{fig:second}
\end{figure}

\lipsum[1-50]

\begin{figure}[ht]
  \centering\includegraphics[width=.5\linewidth]{example-image}
  \caption{A figure}\label{fig:third}
\end{figure}

\lipsum[1-50]

\begin{figure}[ht]
  \centering\includegraphics[width=.5\linewidth]{example-image}
  \caption{A figure}\label{fig:fourth}
\end{figure}

\lipsum[1-50]

\begin{figure}[ht]
  \centering\includegraphics[width=.5\linewidth]{example-image}
  \caption{A figure}\label{fig:fifth}
\end{figure}

\lipsum[1-50]

\begin{figure}[ht]
  \centering\includegraphics[width=.5\linewidth]{example-image}
  \caption{A figure}\label{fig:sixth}
\end{figure}

\lipsum[1-50]

\begin{figure}[ht]
  \centering\includegraphics[width=.5\linewidth]{example-image}
  \caption{A figure}\label{fig:seventh}
\end{figure}

Figure~\ref{fig:sixth}.  % This is fine...
Figure~\ref{fig:seventh}.% ...but this is not, as the figure was pushed to the following page.
\end{document}

The .log outputs (somewhere):

LaTeX Warning: ...figure `fig:second' is on page 20, but referenced on 10 on in
put line 31.

The warning allows you to identify the label used, the respective page numbers (of the \label and \ref), as well as the line in the input file.

There should be no difference between the first and subsequent \refs for the same \label in the above example.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.