2

I often find myself copying and pasting large chunks of {figure} and {table} code around my documents, trying to improve the placement of my figures. It's error prone and bit annoying.

I know about the [H] argument for a float environment. Here, I’m not trying to make the float position in the resulting e.g PDF document match exactly the location of my float environment. I just want a simpler way of moving code for floats around my tex file that doesn’t involve copying and pasting big chunks of code around.

I wonder, can it be possible to make some system with new environments and macros, where I can do something like this

\begin{placer}{tag}
\begin{figure}
...
\end{figure}
\end{placer}

My idea is that the new placer environment just captures everything and saves it with a name based on the argument tag.

I then put my figure where I want by using a new macro

\place{tag}

Ideally, it would be possible use \place{tag} before the above code snippet, and still get the desired result. And ideally, it would not rely on writing files or any additional compilation steps. I thinky it would be very nice if by default the float was placed by default at the \begin{placer}{tag} spot if \place{tag} was never called.

Is this kind of system feasible?

If it is, I’d want to go even further. I’d like to redefine the figure and table float environments to automatically cache their contents, as above, and so it could be placed via the place macro as above using some labels.

I could load some package that implemented that and start moving figures and tables around a document by just adding the \place{fig:whatever} commands with the argument corresponding to some tag of the relevant figure. Something like this

\usepackage{placer}
\begin{document}

This is a document with one figure.

\begin{figure}
...
\label{fig:whatever}
\end{figure}

Actually, let’s put the figure code below this text.

\place{fig:whatever}.
8
  • It seems that you looking for H placement defined in the float package, i.e. you like to stop working of float mechanism (on some complicated way). Or I miss understood you?
    – Zarko
    Sep 9, 2020 at 3:49
  • no, I want a command \place{tag} to have the effect of cutting and pasting the whole figure to that place
    – innisfree
    Sep 9, 2020 at 4:06
  • 1
    Partial answer: tex.stackexchange.com/q/5338/323
    – Aditya
    Sep 9, 2020 at 5:36
  • 1
    it isn't at all clear what you want to do, it seems like \newcommand\foo[2]{\begin{figure}\includegraphics{#1}\caption{#2}\end{figure}} then \foo{mypic}{the caption} ?? Sep 9, 2020 at 6:43
  • I tried once to adjust (normal) float placement depending on a xifthen boolean. I had to define a new command the way @DavidCarlisle did. Sep 9, 2020 at 8:35

1 Answer 1

2

You can use xparse to define an environment that stores its contents. A property list seems to be the appropriate tool.

\documentclass{article}
\usepackage{xparse}

\usepackage{graphicx}
\usepackage{booktabs}

\usepackage{lipsum} % just for mock text

\ExplSyntaxOn

\prop_new:N \g_innisfree_floats_prop

\NewDocumentEnvironment{placer}{m +b}
 {
  \prop_gput:Nnn \g_innisfree_floats_prop { #1 } { #2 }
 }
 {}

\NewDocumentCommand{\place}{m}
 {
  \prop_item:Nn \g_innisfree_floats_prop { #1 }
 }

\ExplSyntaxOff

%%% define the objects (can be in a different file to be \input)

\begin{placer}{fig:example}
\begin{figure}[htp]
\centering
\includegraphics[width=4cm]{example-image}

\caption{A figure\label{fig:example}}
\end{figure}
\end{placer}

\begin{placer}{tab:example}
\begin{table}[tp]
\centering

\caption{A table\label{tab:example}}

\begin{tabular}{lc}
\toprule
X & Y \\
\midrule
A & 1 \\
B & 2 \\
\bottomrule
\end{tabular}
\end{table}
\end{placer}

\begin{document}

\lipsum[2]

\place{fig:example}

\lipsum[3-5]

\place{tab:example}

\lipsum

\end{document}

enter image description here

3
  • This looks great. Do the {placer} environments for the figure code etc have to go before begin document?
    – innisfree
    Sep 9, 2020 at 11:40
  • PS the ExplSyntaxOn features are so powerful!
    – innisfree
    Sep 9, 2020 at 11:46
  • @innisfree No, they don't have to be in the preamble. But, of course, before being used.
    – egreg
    Sep 9, 2020 at 11:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .