# Keeping the definition of tables/figures separated from main text

Disclamer: This is not a question regarding the micromanagement of tables/figures placement during typesetting, but rather about organization of the original latex file itself.

Prologue: During the preparation of a substantial latex document, I feel that mixing the definition of tables/figures/algorithms/... inside the body of the text results most often in a confusing mess. While writing/proofreading the original latex document (not the typeset file), focusing on the scientific content is difficult when the text is diluted by the numerous commands needed to include the tabulated results or figures (specially for very complicated tables).

To overcome this problem, I ended up encapsulating the definition of my tables/figures inside \newcommand elements. This way, all my tables/figures are grouped at the beginning of the document, and then the more meaningful text appears as a bloc. The following MWE gives an idea:

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\newcommand{\plugFigureApple}{
\begin{figure}[h]
\includegraphics{foo}
\caption{This is the caption for the apple figure.}
\label{fig:apple}
\end{figure}
}

\newcommand{\plugFigureOrange}{
\begin{figure}[h]
\includegraphics{bar}
\caption{This is the caption for the orange figure.}
\label{fig:orange}
\end{figure}
}

\newcommand{\plugTableFruits}{
\begin{table}[h]
\caption{This is the caption for the fruits table.}
\label{tab:fruits}
\begin{tabular}{ l | c }
Fruit & Calories \\
\hline
Apple  & 52 \\
Orange & 47 \\
\end{tabular}
\end{table}
}

This is a paragraph where apples are presented in Figure~\ref{fig:apple}. This is a very serious discussion regarding apples.
\plugFigureApple{}

This is a paragraph where oranges are presented in Figure~\ref{fig:orange}. This is a quite a comprehensive analysis.
\plugFigureOrange{}

This is one final paragraph where fruits are discussed in Table~\ref{tab:fruits}. This is a thorough discussion.
\plugTableFruits{}

\end{document}


This effectively allows to focus on the text with minimal distraction. But as some files have a very important number of tables and figures, scrolling down to find the first meaningful sentence have now become impractical. Ideally, I would like the real text to come first so that the table/figure definitions would end up at the bottom of the latex file. This is not an option using my proposition, since the \newcommand elements have to be defined beforehand.

So here's my question: is there a way (or a package) that would allow me to start with the main text and end with all the table/figure definitions at the bottom of the file, while having some sort of control over the placement of those tables/figures during typesetting (as I don't want those to simply appear as a list at the end of the document). I do not mind manually providing some positional marks (as for example the \plugFigureApple{} in the above MWE), but the tables/figures could also be placed automatically as close as they can from their respective first reference.

• You can write all your commands for tables and figures in an auxiliary file (tables.tex) and write \input{tables} in your preamble. – Ignasi Mar 4 '14 at 8:12
• @Ignasi : Thanks for the tip, but I would prefer to keep the content in a single file (should have mentioned that in the question). – raspoutine Mar 4 '14 at 8:46
• Then you need an editor with folding lines ability, this way you can hide/show some text parts when you want. You can find a list of LaTeX friendly editors in LaTeX editors IDEs – Ignasi Mar 4 '14 at 10:33
• you can put it at the end (with some restrictions) if you don't mind them not being there on the first run, basically you just need to get latex write them to a file that is input at the start. Also of course don't use [h] unless you specifically want the floats to float to the end of the document, latex will normally give a warning and change that to [ht] but [htp] or not using teh option are usually better. – David Carlisle Mar 4 '14 at 12:37
• @DavidCarlisle : How exactly would you do that, can you provide a solution that I could accept as an answer? – raspoutine Mar 4 '14 at 19:48

\documentclass{article}
\usepackage{graphicx}
\usepackage{filecontents}

\begin{document}

\newcommand\inputfloat[1]{\InputIfFileExists{float-#1}{}{\typeout{skipping #1}}}

This is a paragraph where apples are presented in Figure~\ref{fig:apple}. This is a very serious discussion regarding apples.
\inputfloat{apple}

This is a paragraph where oranges are presented in Figure~\ref{fig:orange}. This is a quite a comprehensive analysis.
\inputfloat{orange}

This is one final paragraph where fruits are discussed in Table~\ref{tab:fruits}. This is a thorough discussion.
\inputfloat{fruits}

\begin{filecontents}{float-apple}
\begin{figure}[htp]
\rule{1cm}{2cm}%\includegraphics{foo}
\caption{This is the caption for the apple figure.}
\label{fig:apple}
\end{figure}
\end{filecontents}

\begin{filecontents}{float-orange}
\begin{figure}[htp]
\rule{1cm}{2cm}%\includegraphics{bar}
\caption{This is the caption for the orange figure.}
\label{fig:orange}
\end{figure}
\end{filecontents}

\begin{filecontents}{float-fruits}
\begin{table}[htp]
\caption{This is the caption for the fruits table.}
\label{tab:fruits}
\begin{tabular}{ l | c }
Fruit & Calories \\
\hline
Apple  & 52 \\
Orange & 47 \\
\end{tabular}
\end{table}
\end{filecontents}

\end{document}