# Trouble when using the subfiles and wrapfig packages together

Take 2.

I'm currently trying to write my undergraduate thesis using ShareLaTeX. Because it's a long paper, I spent awhile last night setting up the subfiles package so that each section of the paper gets its own file. Today, however, I tried to add my first figure. The two files look like this (tested to ensure that the bug occurs this time):

main.tex:

\documentclass[11pt,letterpaper]{report}
\usepackage[utf8]{inputenc}
\usepackage[bottom=1in,top=1in,left=1in,right=1in]{geometry}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{subfiles}

\begin{document}
\chapter{Basics of Knot Theory}
\section{History of Knot Theory}
\subfile{1.1_test_section.tex}
\end{document}


1.1_test_section.tex:

\documentclass[main]{subfiles}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\par
\begin{wrapfigure}{r}{0.3\textwidth}
\centering{
\includegraphics[width=0.28\textwidth]{example-image.jpg}
\caption{example caption}
}
\end{wrapfigure}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{document}


There is no error message associated with this bug. When rendering the individual file 1.1_test_section.tex, everything looks great, the image comes up on the right side at the correct width and the text wraps around it beginning with the second paragraph. In the main document, however, the second paragraph ends up printing over the image, and there is no wrapping going on at all.

Here are some pictures for good measure: Note that the warning in the pictures is about Bibtex, nothing related to this problem.

Is this a ShareLaTeX issue? Will it resolve if I switch to an offline compiler (not an option until next week when I get my computer fixed)? Is there another package that I can use in place of wrapfig to do essentially the same thing, but that will be compatible with subfiles? I would like to stick with the subfiles package if at all possible because I put a lot of work into setting it up and I don't want to have to go back and do it again for a new package.

• thanks for making a good test file, I can confirm that I see the same (using a locally installed texlive 2017) – David Carlisle Feb 27 '18 at 23:45
• ooh subfiles is a group... that would explain it. – David Carlisle Feb 27 '18 at 23:47

You need to make sure that the paragraph ends while wrapfig is in scope, subfiles probably ought to make \end{document} end the paragraph but it doesn't so unless you change the package you need a blank line before \end{document} in the included file then

Note this issue is not really related to wrapfig it's a bug in subfiles you see the same thing if the included file is

\documentclass[main]{subfiles}

\begin{document}

\centering
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{document}


The version included into the main document will not have centered lines unless there is a blank line before \end{document}.

A fix would be to change

\newcommand{\skip@preamble}{%
\let\document\relax\let\enddocument\relax%
\newenvironment{document}{}{}%
\renewcommand{\documentclass}[2][subfiles]{}}


to

\newcommand{\skip@preamble}{%
\let\document\par\let\enddocument\par
\renewcommand{\documentclass}[2][subfiles]{}}


so an alternative to adding the blank line would be to add

\renewcommand{\skip@preamble}{%
\let\document\par\let\enddocument\par
\renewcommand{\documentclass}[2][subfiles]{}}


after \usepackage{subfiles}`