# Text-Wrapped Figures with CatchFileBetweenTags

I've been trying to develop a clutter-free workflow for writing the content of LaTeX documents, and have nearly achieved it using the catchfilebetweentags package (I can write the text in a separate minimal file and then call it into the LaTeX document structure at compile time). However, I cannot get it to behave with text-wrapped figures. Any time a figure is larger than the height of the text section contained between the tags, strange figure placement occurs.

Desired output: when a figure is defined in a text section too short to contain it, it should be allowed to flow into the following section, wrapping text around as needed.

With none of the following configurations have I been able to achieve this.

MWE input file: document.tex

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{catchfilebetweentags}
\usepackage{wrapfig}
\usepackage{graphicx}
\CatchFileBetweenTags{\SectionA}{content.tex}{SectionA}
\CatchFileBetweenTags{\SectionB}{content.tex}{SectionB}
\begin{document}
\section*{Section A}
\SectionA
\section*{Section B}
\SectionB
\end{document}


Example 1: figure inside large text section in content.tex

%<*SectionA>
text
%</SectionA>
%<*SectionB>
text
\begin{wrapfigure}{r}{0.25\linewidth}
\begin{center}
\includegraphics[width=\linewidth]{./graphics/example.png}
\end{center}
\caption{Caption here.}
\label{example_wrap_figure}
\end{wrapfigure}
text
text
%</SectionB>


Output: Figure wraps correctly if the text section is large enough

Example 2: figure inside small text section in content.tex

%<*SectionA>
text
\begin{wrapfigure}{r}{0.25\linewidth}
\begin{center}
\includegraphics[width=\linewidth]{./graphics/example.png}
\end{center}
\caption{Caption here.}
\label{example_wrap_figure}
\end{wrapfigure}
%</SectionA>
%<*SectionB>
text
text
text
%</SectionB>


Output: entire Section B is wrapped to the left, and using the optional "number of narrow lines" wrapfig option does not help (Prevent wrapfigure from wrapping the next section).

Example 3: figure in between sections in document.tex, not content.tex

\section*{Section A}
\SectionA
\begin{wrapfigure}{r}{0.25\linewidth}
\begin{center}
\includegraphics[width=\linewidth]{./graphics/example.png}
\end{center}
\caption{Caption here.}
\label{example_wrap_figure}
\end{wrapfigure}
\section*{Section B}
\SectionB


Output: same as Example 2.

Example 4: figure in between sections in document.tex in its own scope

\section*{Section A}
\SectionA
{
\begin{wrapfigure}{r}{0.25\linewidth}
\begin{center}
\includegraphics[width=\linewidth]{./graphics/example.png}
\end{center}
\caption{Caption here.}
\label{example_wrap_figure}
\end{wrapfigure}
}
\section*{Section B}
\SectionB


Output: forces figure to the end of the document.

I suspect, that catchfilebetweentags executes SectionA before it knows about SectionB. Is there any way to get the desired output? Alternately, is there any way to delay the execution of the graphic environment until after all the text sections have been evaluated, perhaps by running latex multiple times?