4

I've been trying to use wrapfig inside a tcolorbox environment that has an amount of text that will not cover the height of the figure inside the environment.

The following code resulted in the text after the environment overlapping with the figure. To be more precise the environment did not recognise the height of the figures included within it.

\documentclass[draft]{article}

\usepackage{showframe}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{tikz}

%% To wrap text around figures
\usepackage{wrapfig}
\usepackage{etoolbox}
\BeforeBeginEnvironment{wrapfigure}{%
    \setlength{\intextsep}{-0.5\baselineskip}%
}

%% Measuring Figure Width and Height
\newbox\mybox
\def\mysaver{%
    \pgfmathparse{\pgfpositionnodelatermaxx-\pgfpositionnodelaterminx}%
    \xdef\savednodewidth{\pgfmathresult pt}%
    \pgfmathparse{\pgfpositionnodelatermaxy-\pgfpositionnodelaterminy}%
    \xdef\savednodeheight{\pgfmathresult pt}%
}

\usepackage[skins,breakable]{tcolorbox}

\newenvironment{myEnvironment}{%
    \begin{tcolorbox}[blanker,width=\textwidth-20pt,enlarge left by=20pt,before skip=2em,breakable,overlay unbroken and first={%
        \node[inner sep=0pt,outer sep=0pt,anchor=north east] at ([shift=   {(-10pt,1.5pt)}]frame.north west) {\textcolor{olive}{\rule{10pt}{10pt}}};}]%
        \setlength{\parskip}{1.25em}%
        }{\vspace{\baselineskip}
    \end{tcolorbox}}


\newcommand{\myFigure}[1]{%
\tikz{{\pgfpositionnodelater{\mysaver}\node[inner sep=0pt,outer sep=0pt,align=center]{#1};}}%
\begin{wrapfigure}{R}{\savednodewidth+20pt}
    \hfill \parbox{\savednodewidth}{\hfill #1}
\end{wrapfigure}
}

\begin{document}

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales.
    \begin{myEnvironment}
        \textbf{Lorem Ipsum}
        \myFigure{\includegraphics[scale=0.6]{Fig1}\\
            \includegraphics[scale=0.6]{Fig2}\\
            \includegraphics[scale=0.6]{Fig3}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales. Nunc pharetra est sit amet ligula sodales tempor.

Vestibulum facilisis, enim sed tempor luctus, arcu dolor iaculis ante, semper fringilla magna nibh fermentum tortor.

Nulla eu libero vulputate, dapibus est in, molestie nunc. Morbi ut iaculis lectus.
    \end{myEnvironment}
\myFigure{\includegraphics[scale=0.6]{Fig4}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales. Nunc pharetra est sit amet ligula sodales tempor. Vestibulum facilisis, enim sed tempor luctus, arcu dolor iaculis ante, semper fringilla magna nibh fermentum tortor. Nulla eu libero vulputate, dapibus est in, molestie nunc. Morbi ut iaculis lectus. 

\end{document}

This is the output I need. And I had to use a workaround to get there. But unfortunately I cannot use it as a permanent solution.

3
  • You can't start a new wrapfig until the previous one is done. One solution is to hand insert the wrapfigs into the text, possibly using \WFclear first. Another is to put all of the figures into one wrapfig (common width). – John Kormylo Oct 21 '18 at 14:43
  • The second solution won't give me the layout I want. And I do not understand the first solution [.. using \WFclear first ...]. Would you please explain further? – ένας Oct 22 '18 at 6:09
  • \WFclear is only used if needed. It terminates the previous wrapfig. I could show you how to do it, but I would need image widths. – John Kormylo Oct 22 '18 at 13:25
2
+50

For your revised MWE, you can add the needed space before the end of the tcolorbox. The counter WF@wrappedlines is the number of remaining lines plus one.

It appears that wrapfig doesn't handle \parskip>0pt very well. The number of lines to indent is computed before the number of paragraphs is known.

I left the rule visible for demonstration purposes; in practice one should set the width to 0pt.

BTW, \includegraphics really doesn't like using undefined files, even in draft mode.

\documentclass[draft]{article}

\usepackage{showframe}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{tikz}

%% To wrap text around figures
\usepackage{wrapfig}
\usepackage{etoolbox}
\BeforeBeginEnvironment{wrapfigure}{%
    \setlength{\intextsep}{-0.5\baselineskip}%
}

%% Measuring Figure Width and Height
\newbox\mybox
\def\mysaver{%
    \pgfmathparse{\pgfpositionnodelatermaxx-\pgfpositionnodelaterminx}%
    \xdef\savednodewidth{\pgfmathresult pt}%
    \pgfmathparse{\pgfpositionnodelatermaxy-\pgfpositionnodelaterminy}%
    \xdef\savednodeheight{\pgfmathresult pt}%
}

\usepackage[skins,breakable]{tcolorbox}

\newenvironment{myEnvironment}{%
    \begin{tcolorbox}[blanker,width=\textwidth-20pt,enlarge left by=20pt,before skip=2em,breakable,overlay unbroken and first={%
        \node[inner sep=0pt,outer sep=0pt,anchor=north east] at ([shift=   {(-10pt,1.5pt)}]frame.north west) {\textcolor{olive}{\rule{10pt}{10pt}}};}]%
        \setlength{\parskip}{1.25em}%
        }{\vspace{\baselineskip}
    \end{tcolorbox}}


\newcommand{\myFigure}[1]{%
\tikz{{\pgfpositionnodelater{\mysaver}\node[inner sep=0pt,outer sep=0pt,align=center]{#1};}}%
\begin{wrapfigure}{R}{\savednodewidth+20pt}
    \hfill \parbox{\savednodewidth}{\hfill #1}
\end{wrapfigure}
}

\begin{document}

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales.
    \begin{myEnvironment}
        \textbf{Lorem Ipsum}
        \myFigure{\includegraphics[width=3cm]{example-image}\\
            \includegraphics[width=4cm]{example-image-a}\\
            \includegraphics[width=4cm]{example-image-b}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales. Nunc pharetra est sit amet ligula sodales tempor.

Vestibulum facilisis, enim sed tempor luctus, arcu dolor iaculis ante, semper fringilla magna nibh fermentum tortor.

Nulla eu libero vulputate, dapibus est in, molestie nunc. Morbi ut iaculis lectus.

\par\rule[-\dp\strutbox]{1pt}{\dimexpr \arabic{WF@wrappedlines}\baselineskip-\baselineskip-4\parskip}\WFclear% add extra sapace
    \end{myEnvironment}
\myFigure{\includegraphics[width=4cm]{example-image-c}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales. Nunc pharetra est sit amet ligula sodales tempor. Vestibulum facilisis, enim sed tempor luctus, arcu dolor iaculis ante, semper fringilla magna nibh fermentum tortor. Nulla eu libero vulputate, dapibus est in, molestie nunc. Morbi ut iaculis lectus. 

\end{document}

demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.