0

I have founded a solution on this page: link

If the figure does not fit it is allowing a page breake, that is working.

But the one extra line appears after the figure, and I do not know why, and I do not know how to solve thi issue.

Code

\documentclass{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{parskip}

\BeforeBeginEnvironment{wrapfigure}{\setlength{\intextsep}{0pt}}

\newsavebox\curwrapfig \makeatletter \long\def\wrapfiguresafe#1#2#3{ \sbox\curwrapfig{#3} \par\penalty-100 \begingroup \dimen@\pagegoal \advance\dimen@-\pagetotal \advance\dimen@-\baselineskip \ifdim \ht\curwrapfig>\dimen@ \break% \fi% \endgroup% \begin{wrapfigure}{#1}{#2} \usebox\curwrapfig \end{wrapfigure} } \makeatother

\begin{document} \lipsum[1-6] \wrapfiguresafe{r}{0mm}{\centering\rule{3cm}{6cm}} \lipsum[1-6] \end{document}

Screenshot:

enter image description here

mala97
  • 312
  • 1
    Which one extra line do you mean? I don't see any line. Or do you mean that you think that there is one short line too much? – Skillmon Jan 01 '23 at 13:46
  • @Skillmon I have just updated my screenshot. – mala97 Jan 01 '23 at 13:51
  • If you add \vfill\hrule to the bottom of the page, you will see that the wrapfigure will overlap the last shortened line of text (text can shrink or expand slightly to fit) – John Kormylo Jan 01 '23 at 18:07

1 Answers1

1

The wrapfigure environment has two optional parameters that your wrapping macro don't support, also the support for wrapfigure's second mandatory argument of your macro was incorrect as well. The following is an altered implementation that should correctly implement a wrapper that supports both optional argument and should behave correct with the second mandatory argument not being equal to 0pt.

The first optional argument of wrapfigure specifies how many short lines the figure should produce. The following sets that parameter to 14 which is correct in your example.

\documentclass{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{parskip}

\BeforeBeginEnvironment{wrapfigure}{\setlength{\intextsep}{0pt}}

\ExplSyntaxOn \cs_new_protected:Npn \ForwardOarg #1#2 { \tl_if_eq:nnTF {#1} {#2} { \tl_clear:N \ProcessedArgument } { \tl_set:Nn \ProcessedArgument {[{#2}]} } } \ExplSyntaxOff

\newsavebox\curwrapfig \makeatletter \NewDocumentCommand\wrapfiguresafe {>{\ForwardOarg{}}O{} m >{\ForwardOarg{}}O{} m m} {% \ifdim#4=\z@ \sbox\curwrapfig{#5}% \else \sbox\curwrapfig{\parbox[b]{#4}{#5}}% \fi \par\penalty-100 \begingroup \dimen@\pagegoal \advance\dimen@-\pagetotal \advance\dimen@-\baselineskip \ifdim \ht\curwrapfig>\dimen@ \break \fi \endgroup \begin{wrapfigure}#1{#2}#3{#4}% \usebox\curwrapfig \end{wrapfigure}% } \makeatother

\begin{document} \lipsum[1-6] \wrapfiguresafe[14]{r}{0mm}{\centering\rule{3cm}{6cm}} \lipsum[1-6] \end{document}

enter image description here

Skillmon
  • 60,462
  • I see, is it mandatory to count down the number of line (in this case 14), or is there another way to make it fully automatic (without manual line counting)? – mala97 Jan 01 '23 at 14:26
  • 1
    @mala97 the fully automatic approach was the one that got the count wrong... – Skillmon Jan 01 '23 at 14:54