# Unequivocally force first reference to same page as the referred item?

## Background

Writing a manual using LyX. All tables, figures, and source listings are always referenced in the paragraph (or enumerated step) that immediately precedes them. The preamble to the manual includes \raggedbottom to prevent large vertical spaces between list items.

## Problem

The problem can be seen here:

The heading "1.1 Hierarchies" (no orphaned headings allowed) and the reference to "Figure 1.1" should be at the top of Page 2, on the same page as the image.

## LaTeX Code

The images are added to floats so that they can have captions and cross-references. The LyX documents contain the following commands:

\float_placement H


I have tried changing the commands to:

\float_placement !htbp


These affect figure placement, but do nothing for the cross-references.

## LyX Code

This is the code that LyX generates:

\begin{figure}[H]
\includegraphics{drawings/directory-hierarchy-01}

\caption{\label{fig:Example-Directory-Organization}Recommended Directory Organization}

\end{figure}


## Possible Solutions

1. \begin{samepage} ... \end{samepage}
2. \clearpage

The \clearpage option is possible when the manual is complete. The samepage environment needs tweaking.

## Question

What needs to be added to the document preamble to force the first cross-reference to remain on the same page as the item that is being referenced?

## Related

-
Possible duplicate Keeping tables/Figures close to where they are mentioned –  Juan A. Navarro Jan 22 '11 at 22:37
1) don't use floats if you don't want things to float. 2) if the layout is such that it's impossible for the reference and image to be on the same page don't expect TeX to perform magic. 3) if it's not covered in the answers to the above linked question, it's probably not going to be possible. –  Seamus Jan 22 '11 at 22:44
(1) Using the caption package. –  Caramdir Jan 23 '11 at 4:01
@Dave, also have a look at Force figure placement in text for a solution using the float package. If the answers there are still not good for you, please edit your question to indicate why. Otherwise this question should be closed as duplicate. –  Juan A. Navarro Jan 24 '11 at 10:56
@Dave From the wiki you linked to: "How do I get LyX to put the figure exactly where I want it? Select Document→Settings.... In the Float Placement section, unset "Use Default Placement" and select "Top of Page", "Page of Floats", "Here, if possible" and "Ignore LaTeX rules". [...] If you really insist, you can also simply select "Here definitely". This will insert the figure by all means at the current place. Note, though, that chances are high that this leads to ugly unbalanced pages. Usually, LaTeX does a rather good job in float placement if you do not restrict it too much." –  Seamus Jan 24 '11 at 12:36

## 1 Answer

Looking at the relevant entry on the UK's TeX FAQ, there is probably no way to automatically and conveniently do this.

Indeed, the two most simple solutions would be to use the samepage environment (which may not work in all situations) or manually insert \clearpage as needed.

My advise would be to leave the figures wherever they happen to fall, and only in the end add \clearpage's as appropriate to break pages exactly at the places you want.

Sorry, there seems to be no “keep with next” in (La)TeX.

-
Not really a viable option. The manual is going to have multiple published revisions (adding new sections and chapters), so "in the end" will not happen. I tried \samepage, but it did not work as expected. Thank you for taking the time to look into this, though. –  Dave Jarvis Jan 24 '11 at 19:24