# How can I place tables after first \autoref when tables are all defined in one place or contained in a separate file called by \input?

## Background

I have a set of tables that can be automatically generated (specifically, a set of database table descriptions generated by phpmyadmin).

I would like to include each tables in the document after it is mentioned.

I think that I understand how the table placement commands work [htbp], but these are not the issue.

## Question

Is there a simple way to force float placement after a reference?

## Examples

### This is a MWE of what I have

\documentclass[10pt]{article}
\usepackage{hyperref}

\begin{document}
\input{dbtables.tex}
% in practice, all tables appear here

\section{tables}

\subsection{table1}
\autoref{tab:table1} is for this.
% I want Table 1 to appear here

\subsection{table2}
\autoref{tab:table2} is for that.
% I want Table 2 to appear here

\end{document}


Where the contents of dbtables.tex might be

\begin{table}[hb]
\caption{test table 1}
\label{tab:table1}
\begin{tabular}{ l c r }
1 & 2 & 3 \\
\end{tabular}
\end{table}

\begin{table}[hb]
\caption{test table 2}
\label{tab:table2}
\begin{tabular}{ l c r }
4 & 5 & 6 \\
\end{tabular}
\end{table}


### In the end, the result that I want would be similar to:

\documentclass[10pt]{article}
\usepackage{hyperref}

\begin{document}
\input{dbtables.tex}

\section{tables}

\subsection{table1}
\autoref{tab:table1} is for this.

\begin{table}[hb]
\caption{test table 1}
\label{tab:table1}
\begin{tabular}{ l c r }
1 & 2 & 3 \\
\end{tabular}
\end{table}

\subsection{table2}
\autoref{tab:table2} is for that.

\begin{table}[hb]
\caption{test table 2}
\label{tab:table2}
\begin{tabular}{ l c r }
4 & 5 & 6 \\
\end{tabular}
\end{table}

\end{document}


Except that I want to keep the files separate.

-

If it's possible to generate the table code in dbtables.tex like this

\mytable{1}{
\begin{table}[hb]
\caption{test table 1}
\label{tab:table1}
\begin{tabular}{ l c r }
1 & 2 & 3 \\
\end{tabular}
\end{table}
}

\mytable{2}{
\begin{table}[hb]
\caption{test table 2}
\label{tab:table2}
\begin{tabular}{ l c r }
4 & 5 & 6 \\
\end{tabular}
\end{table}
}


then you can define

\makeatletter
\newcommand{\mytable}[2]{\@namedef{my@table@\romannumeral#1}{#2}}
\newcounter{mytablecnt}
\newcommand{\placetable}{%
\stepcounter{mytablecnt}\@nameuse{my@table@\roman{mytablecnt}}}
\makeatother


\begin{document}
\input{dbtables.tex}
% in practice, all tables appear here

\section{tables}

\subsection{table1}
\autoref{tab:table1} is for this.
\placetable

\subsection{table2}
\autoref{tab:table2} is for that.
\placetable

\end{document}

-
not the quick setting I was hoping to find but a nice solution. Thanks. –  David Jun 9 '11 at 5:44

You can use the flafter package:

\usepackage{flafter}


The package is part of the base LaTeX installation. It takes care that floats will be placed after they have been referenced.

Further useful package regarding this subject are placeins and its command \FloatBarrier, use it for example to avoid floating over subsection borders, and afterpage, for example with \afterpage{\clearpage}.

-
Does the flafter package not force the float placement after its declaration in the source code and not after the first \ref to it? The documentation is sparse. A texdoc flafter just shows the source2e manual. –  Martin Scharrer Jun 2 '11 at 15:01
flafter does not seem to do the trick on my MWE. I will check out placeins –  David Jun 2 '11 at 15:01
\FloatBarrier\afterpage{\clearpage} gives an error, could you give an example of use of the placeins with the MWE above? –  David Jun 2 '11 at 15:07
@David: Indeed, flafter doesn't work like expected. So I would recommend 'storing' the tables code in macros, like egreg did. –  Stefan Kottwitz Jun 2 '11 at 15:27