Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

During the draft phase of an article, when the text is being developed and things move around a lot, I place all my figures at the end of the document so that I'm not greeted with sudden layout changes. Later, when I'm more comfortable with the text (but still liable to change), I bring the figures upfront one by one.

My question is: Instead of having to lug around the entire

\begin{figure}
    \centering
    \includegraphics[...]{...}
    \label{fig:test}
    \caption{...}
\end{figure}

block to reposition the figure, is it possible to leave them where they are (i.e. towards the end of the document) and "place" them where I want by simply referencing them in some custom way, such as:

\placehere{fig:test}

which will place the float (defined later in the document) at the current position? This way, I can move the figures around with a single line instead of several.

The resulting document layout would look something like:

\documentclass{article}
\usepackage{lipsum,graphicx}
\begin{document}
\section{Lorem}
\lipsum[5]
\placehere{fig:test1}

\section{Dolor}
\lipsum[5]
\placehere{fig:test2}

\section{Final section}
\lipsum[5]

\begin{figure}
    \centering
    \includegraphics[...]{...}
    \label{fig:test1}
    \caption{...}
\end{figure}

\begin{figure}
    \centering
    \includegraphics[...]{...}
    \label{fig:test2}
    \caption{...}
\end{figure}
\end{document}
share|improve this question
2  
Use the endfloat package. –  Ulrike Fischer Jun 9 '13 at 9:04
    
Just make a macro for each figure, e.g., \newcommand{\figureone}{\begin{figure}...\end{figure}}. Then use \figureone wherever you want the figure. –  Matthew Leingang Jun 9 '13 at 11:05
add comment

1 Answer 1

up vote 4 down vote accepted

The following implementation requires that you prepare your figures and tables in a different file, which is anyway better for the application you have in mind.

In the example code the figure environments have been typed in a filecontents environment. You don't need it, just prepare a file with the same name as your main LaTeX file and extension .flo.

\begin{filecontents}{\jobname.flo}
\begin{figure}[htp]
    \centering
    ABC
    \caption{...}
    \label{fig:test1}
\end{figure}

\begin{figure}[htp]
    \centering
    DEF
    \caption{...}
    \label{fig:test2}
\end{figure}
\end{filecontents}

\documentclass{article}
\usepackage{environ}
\usepackage{lipsum}

\AtBeginDocument{%
  \begingroup
  \InputIfFileExists{\jobname.flo}{\setupprefloats}{}%
  \endgroup
}
\makeatletter
\newcommand{\setupprefloats}{%
  \let\figure\relax\let\endfigure\relax
  \let\table\relax\let\endtable\relax
  \prefloat@rename{figure}\prefloat@rename{table}%
}

\newcommand{\prefloat@rename}[1]{%
  \NewEnviron{#1}{%
    \let\label\prefloat@label
    \renewcommand\caption[2][]{#2}%
    \setbox\z@=\vbox{\BODY}
    \toks@=\expandafter{\BODY}
    \expandafter\xdef\csname prefloat@\theprefloat@label\endcsname{%
      \noexpand\begin{#1}\the\toks@\noexpand\end{#1}}%
  }%
}
\newcommand{\prefloat@label}[1]{%
  \gdef\theprefloat@label{#1}%
}
\newcommand{\placehere}[1]{%
\expandafter\show\csname prefloat@#1\endcsname
  \@nameuse{prefloat@#1}%
}
\makeatother

\begin{document}
\section{Lorem}
\lipsum[5]
\placehere{fig:test1}

\section{Dolor}
\lipsum[5]
\placehere{fig:test2}

\section{Final section}
\lipsum[5]

\end{document}

At \begin{document} the file will be read in and the figure and table environments are temporarily redefined to store their contents in a special command that is later delivered with the \placehere command.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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