2

This question is connected with How to put figures at the end of pdf file. As one can see from @cfr's and mine comments, I hoped, that it was a question, how to put a code of figures, wich are lying in the middle of text, at the end of file.

I have some ideas, how it could be realized. Some commands may be of the form \ifundefined... before they are defined to prevent errors. Additionally commands from the end of a file could be written to external files and read near \begin{document}. As I understand, graphic files are in TikZ/PGF, PostScript, PSTricks or some other tool, which produces graphics during a TeX compilation.

There is no MWE, because I wish rather to be warned by experienced users about potential pitfalls of solving the problem with help of external files. It is also possible, that my idea is naive and there exists completely different solution. For example, the problem Going through permutations of nouns and verbs, which seemed to be really difficult for me, was solved in thirty minutes.

I understand that I might be more precise, but the most important for me is all, what I cannot predict, hence even the sketch of my idea is only a suggestion of a starting point.

  • If you are willing to modify the input format of your figures, you can use the boxhandler package to defer figures (and/or tables) to a later point in the document, including the end. – Steven B. Segletes Nov 07 '14 at 03:18
  • I don't really understand this question either. Why would you want to read the stuff in at the start of the document and what would you use \ifundefined for? Again, is it really the code you want at the end or the figures typeset or both? If I had to guess, I'd guess you want the figures typeset in the middle of the text but the code for the figures at the end of the file. – cfr Nov 07 '14 at 03:29
  • @cfr Yes, the code of figures is at the end of the file, but they are typeset in the middle of text. If I properly understand, I should solve the problem of first run, where there are no generated files/definitions, but they are already called, hence some conditional seems to be needed. – Przemysław Scherwentke Nov 07 '14 at 03:39
  • I don't understand your reasons for not providing an MWE. People will be quick enough to tell you if you are doing something dangerous! Also, why not just put the code in an external file in the first place? Why have LaTeX write it? – cfr Nov 07 '14 at 03:41
  • Note that you could input the file containing \newcommand conditionally and then use \providecommand to cover the first run. Or you could use \newcommand and then input the file containing \renewcommand conditionally. (\IfFileExists) – cfr Nov 07 '14 at 03:45
  • @cfr In the real life I wouldn't move the code in the place after its usage. The first link in my question suggested me, that it may be a good method of improving my skills. As a result, my vision of solution is in statu nascendi. However all these strange assumptions are induced by my initial understanding of the linked question. – Przemysław Scherwentke Nov 07 '14 at 03:56

1 Answers1

2

Here's an implementation of what you seem to want. Note that writing an external file is unavoidable, unless you are keen to read the main file twice, throwing away everything until finding filecontents.

It would be possible to rename the filecontents environment for this particular application.

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

\newcommand\usefigure[2][htp]{%
  \begin{figure}[#1]
  \ifcsname figureatend@#2\endcsname
    \csname figureatend@#2\endcsname
  \else
    \centering
    The figure will be placed at the next run
  \fi
  \end{figure}
}

\NewEnviron{definefigure}[1]{%
  \expandafter\xdef\csname figureatend@#1\endcsname{%
    \unexpanded\expandafter{\BODY}%
  }%
}

\AtBeginDocument{%
  \InputIfFileExists{\jobname.fae}{}{}%
}

\begin{document}

\lipsum[2]

\usefigure[htp]{blurb}

\lipsum[3]

\usefigure[p]{foo}

\lipsum[4-5]



%%% NOW WE SPECIFY THE FIGURES

\begin{filecontents}{\jobname.fae}

\begin{definefigure}{foo}
\centering
\hrule
\vspace{5cm}
\fbox{THIS IS FIGURE foo}
\vspace{5cm}
\hrule

\caption{Foo}
\end{definefigure}

\begin{definefigure}{blurb}
\centering
\fbox{THIS IS FIGURE blurb}
\caption{Blurb}
\end{definefigure}

\end{filecontents}

\end{document}

You give figures a name; the order in which they are specified in the filecontents environment is completely irrelevant. You can extend the code similarly to also allow tables.

egreg
  • 1,121,712