TransWikia.com

I want to wrap text only above and below a figure inside of a column

TeX - LaTeX Asked by JasperJ on September 5, 2021

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.

One Answer

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
{parbgroup
  setbox0=vbox{strut #1strut}% measure height
  sbox1{#2}% ditto
  dimen0=dimexpr pagegoal-pagetotal-col@numberbaselineskiprelax% fudge factor
  dimen1=pagegoal% compute height of one column
  ifnumcol@number>1relax
    dividedimen1 by col@number
  fi
  advancedimen1 by -baselineskip% fudge factor
  loopifdimdimen0>dimen1% space left in this column
    advancedimen0 by -dimen1
  repeat
  dimen1=dimexpr ht1+dp1+textfloatseprelax% space needed for insert
  ifdimdimen1>dimen0
    ifdimht0>dimen0
      setbox2=vsplit0 to dimen0
      unvbox2
      noindentbox1
      vskiptextfloatsep
      unvbox0
    else% entire text will fit into column
      unvbox0
      vskiptextfloatsep% probably ignored
      noindentbox1
      vskiptextfloatsep
    fi
  else% insert will fit into column
    vskipintextsep
    noindentbox1
    vskipintextsep
    unvbox0
  fi
egrouppar}
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.5textheight]{example-image}
      captionof{figure}{Caption}
      label{fig:my_label1}
   end{minipage}}

   lipsum[3-6]

   end{multicols}    
end{document}

Answered by John Kormylo on September 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP