0

So, I have created a two-column latex document using the multicol package. For the main body of the text, the code looks something like this:

\documentclass[11pt, A4]{article}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{multicol}

\begin{document}
   \begin{multicol*}{2}

   Some text...

   \begin{figure}[H]
     \centering
      \includegraphics[width=\linewidth]{images/test.jpg}
      \captionof{figure}{Caption}
      \label{fig:my_label1}
   \end{figure}

   More text...

   \end{multicol}    
\end{document}

This produces the following result: non-wrapfigure

Now, I want to have the text below the figure (Ut egestas mauris...) to wrap around the figure and fill the previous column as well. I tried using wrapfigure but that produces the following result:

\begin{wrapfigure}{r}{\linewidth}
   \centering
   \includegraphics[width=\linewidth]{images/test.jpg}
   \captionof{figure}{Caption}
   \label{fig:my_label1}
\end{wrapfigure}

wrapfigure

This is close, but the figure should move to the next column while the text stays to fill the gap. Are there any options for wrapfigure that would do this, or any other package? I suppose the only thing it needs to do is have the text only wrap above and below the image, not beside it. However, I am not really familiar with how latex renders these things, so I do not know if it is even possible.

  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – dexteritas Feb 26 '20 at 11:23
  • Change width=\linewidth to width=.5\linewidth. – dexteritas Feb 26 '20 at 11:24
  • Alas, multicol doesn't support afterpage either. BTW, you MWE is still full of errors. – John Kormylo Feb 26 '20 at 14:49
  • Often a wrapfigure is not a very good idea in one column document. In two column documents is directly a bad idea. – Fran Feb 26 '20 at 17:41

1 Answers1

1

This solution uses \split to insert a box into the middle of a paragraph at the start of the next column. It works for both right and left columns.

Note, multicols sets \pagegoal to include all the columns together.

\documentclass[11pt, A4]{article}
%\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{multicol}
\usepackage{caption}
\usepackage{lipsum}% MWE only

\makeatletter
\newcommand{\split}[2]% #1 = text, #2 = insert
{\par\bgroup
  \setbox0=\vbox{\strut #1\strut}% measure height
  \sbox1{#2}% ditto
  \dimen0=\dimexpr \pagegoal-\pagetotal-\col@number\baselineskip\relax% fudge factor
  \dimen1=\pagegoal% compute height of one column
  \ifnum\col@number>1\relax
    \divide\dimen1 by \col@number
  \fi
  \advance\dimen1 by -\baselineskip% fudge factor
  \loop\ifdim\dimen0>\dimen1% space left in this column
    \advance\dimen0 by -\dimen1
  \repeat
  \dimen1=\dimexpr \ht1+\dp1+\textfloatsep\relax% space needed for insert
  \ifdim\dimen1>\dimen0
    \ifdim\ht0>\dimen0
      \setbox2=\vsplit0 to \dimen0
      \unvbox2
      \noindent\box1
      \vskip\textfloatsep
      \unvbox0
    \else% entire text will fit into column
      \unvbox0
      \vskip\textfloatsep% probably ignored
      \noindent\box1
      \vskip\textfloatsep
    \fi
  \else% insert will fit into column
    \vskip\intextsep
    \noindent\box1
    \vskip\intextsep
    \unvbox0
  \fi
\egroup\par}
\makeatother

\begin{document}
   \begin{multicols}{2}

   \lipsum[1]
   %\lipsum[2-4]% to test second column

   \split{\lipsum[2]}{\begin{minipage}{\columnwidth}
      \centering
      \includegraphics[width=\linewidth, height=0.5\textheight]{example-image}
      \captionof{figure}{Caption}
      \label{fig:my_label1}
   \end{minipage}}

   \lipsum[3-6]

   \end{multicols}    
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120