0

I am trying to create a wrapfigure that has two subfigures. Normally when I create subfigures, I use the following structure:

\begin{figure}[h]
\centering
\begin{subfigure}{.5/textwidth}
\includegraphics[width=\textwidth]{image1}
\caption{subfig a}
\label{figa}
\end{subfigure}

\begin{subfigure}{.5/textwidth}
\includegraphics[width=\textwidth]{image2}
\caption{subfig b}
\label{figb}
\end{subfigure}
\caption{figure}
\label{fig}
\end{figure}

I tried to use this in a wrapfigure environment, but it seems I cannot use subfigure inside wrapfigure. I then used some code from the example here, where I checked the first version of the edited answer, since I didn't want to use minipage. I came up with the following (I hardcopied some Lorem Ipsum because I had some problems with the lipsum package and didn't feel like trying to fix that first):

\documentclass[11pt]{report}
\usepackage[margin=2cm]{geometry}
\usepackage[demo]{graphicx}
\usepackage{wrapfig}
\usepackage{caption,subcaption}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus laoreet leo sagittis, porttitor nibh eu, aliquet nunc. Cras suscipit eget velit id tincidunt. Pellentesque quis interdum magna. Nullam urna leo, consectetur non ligula quis, tempor finibus mi. Aliquam rhoncus eleifend sollicitudin. Morbi id nulla malesuada, sollicitudin elit ac, tempus erat. Ut id tincidunt nisl, ac sagittis velit. Quisque vitae odio lobortis, tincidunt nisi ac, malesuada ligula. Sed ullamcorper sapien tincidunt interdum faucibus. Phasellus non orci semper est iaculis ornare. Morbi nulla purus, auctor non consequat ac, aliquet non turpis. Fusce vehicula orci nec orci dignissim, quis aliquam eros lacinia. Duis leo lorem, interdum a mauris sit amet, mollis auctor dolor.

\WFclear
\begin{wrapfigure}{r}{.5\textwidth}
\vspace*{\fill}
\centering\captionsetup[subfigure]{justification=centering}
\includegraphics[width=.5\textwidth]{image1}
\subcaption{subfigure a}
\label{fig:suba}\par\vfill
\includegraphics[width=.5\textwidth]{image2}
\subcaption{subfigure b}
\label{fig:subb}
\caption{figure}
\label{fig:figure}
\end{wrapfigure}

Suspendisse accumsan ex ac turpis vulputate, fermentum pharetra mauris eleifend. Duis dapibus dictum erat pellentesque vestibulum. Quisque eget vestibulum orci, sit amet mollis lorem. Curabitur porta quam quam, vel maximus nisl semper non. Pellentesque at lobortis magna. Nunc efficitur interdum leo. Morbi id ante tristique, rutrum justo nec, hendrerit erat. Fusce congue malesuada nibh, eu interdum sapien laoreet in. Vestibulum sed gravida augue. Donec sit amet ipsum non est lacinia feugiat. Etiam pretium mi eu odio scelerisque volutpat. Donec est nibh, convallis vitae enim vitae, tincidunt consequat lectus. Proin semper, lacus sed pretium venenatis, nisl odio posuere turpis, nec aliquet odio dui sed nisi. Nullam maximus pellentesque faucibus.

Etiam at eleifend enim. Duis tortor ipsum, blandit et odio sed, blandit fermentum mauris. Interdum et malesuada fames ac ante ipsum primis in faucibus. Proin maximus venenatis massa, quis euismod sapien luctus sed. Integer ornare massa at lectus iaculis malesuada. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris et convallis ex. Phasellus erat magna, tristique non urna sed, accumsan vestibulum odio. In convallis urna non leo consequat, ut fringilla arcu tempus. Pellentesque vel efficitur ex. Morbi placerat eget metus non malesuada. Pellentesque sit amet imperdiet mi. Sed cursus fringilla imperdiet. Ut malesuada bibendum dui sed vulputate.

Sed finibus erat at pharetra malesuada. Nunc nec imperdiet ligula. Morbi ut pharetra enim. Cras mattis vehicula erat, eget dapibus tellus dignissim a. Praesent sed lectus a dolor interdum faucibus sed eu ex. Aenean vestibulum finibus dolor nec porta. Integer accumsan ligula commodo, lacinia nibh a, convallis quam. Maecenas non nibh metus. Curabitur pellentesque at magna eget imperdiet. Cras gravida nibh ac nulla blandit, ac pulvinar tortor faucibus. Integer ex urna, maximus in sem sit amet, maximus efficitur ante.
\end{document}

This gives me the following result: Result after compiling

It is almost what I want, but the only problem is that the caption of the whole figure is also a subcaption. How do I fix this? I would prefer to stay as close as possible to the structure that I regularly use, so I prefer not to use minipage and keep it as simple as possible.

3
  • 1
    (1) as the MWE sits here, \subcaption gives warnings in the log. (2) have you tried wrapping the two images and their subcation in a minipage, leaving the figure caption outside?
    – daleif
    Feb 14, 2019 at 11:23
  • 1
    BTW, \vfill and \vspace*{\fill} are used to fill a given space, but in this case the height is not fixed, so they do nothing. Feb 14, 2019 at 17:12
  • After some long process, I solved my problem. The code in the first part of my question that uses subfigure' inside wrapfigure' works fine. In the end, the problem was font-related and was caused by a misplaced \ in my original caption, that is not in the MWE. I failed to test this. The accepted answer by @Raaja below is a perfect solution to the second piece of code in my question.
    – Iris
    Feb 15, 2019 at 10:27

1 Answer 1

1

In the original structure. Just encapsulate your includegraphics environment inside braces {...}.

\documentclass[11pt]{report}
\usepackage[margin=2cm]{geometry}
\usepackage[demo]{graphicx}
\usepackage{wrapfig}
\usepackage{caption,subcaption}

\begin{document}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus laoreet leo sagittis, porttitor nibh eu, aliquet nunc. Cras suscipit eget velit id tincidunt. Pellentesque quis interdum magna. Nullam urna leo, consectetur non ligula quis, tempor finibus mi. Aliquam rhoncus eleifend sollicitudin. Morbi id nulla malesuada, sollicitudin elit ac, tempus erat. Ut id tincidunt nisl, ac sagittis velit. Quisque vitae odio lobortis, tincidunt nisi ac, malesuada ligula. Sed ullamcorper sapien tincidunt interdum faucibus. Phasellus non orci semper est iaculis ornare. Morbi nulla purus, auctor non consequat ac, aliquet non turpis. Fusce vehicula orci nec orci dignissim, quis aliquam eros lacinia. Duis leo lorem, interdum a mauris sit amet, mollis auctor dolor.

    \WFclear
    \begin{wrapfigure}{r}{.5\textwidth}
        \vspace*{\fill}
        \centering
        {\includegraphics[width=.5\textwidth]{image1}
        \subcaption{subfigure a}
        \label{fig:suba}}\par\vfill
        {\includegraphics[width=.5\textwidth]{image2}
        \subcaption{subfigure b}
        \label{fig:subb}}
        \caption{figure}
        \label{fig:figure}
    \end{wrapfigure}

    Suspendisse accumsan ex ac turpis vulputate, fermentum pharetra mauris eleifend. Duis dapibus dictum erat pellentesque vestibulum. Quisque eget vestibulum orci, sit amet mollis lorem. Curabitur porta quam quam, vel maximus nisl semper non. Pellentesque at lobortis magna. Nunc efficitur interdum leo. Morbi id ante tristique, rutrum justo nec, hendrerit erat. Fusce congue malesuada nibh, eu interdum sapien laoreet in. Vestibulum sed gravida augue. Donec sit amet ipsum non est lacinia feugiat. Etiam pretium mi eu odio scelerisque volutpat. Donec est nibh, convallis vitae enim vitae, tincidunt consequat lectus. Proin semper, lacus sed pretium venenatis, nisl odio posuere turpis, nec aliquet odio dui sed nisi. Nullam maximus pellentesque faucibus.

    Etiam at eleifend enim. Duis tortor ipsum, blandit et odio sed, blandit fermentum mauris. Interdum et malesuada fames ac ante ipsum primis in faucibus. Proin maximus venenatis massa, quis euismod sapien luctus sed. Integer ornare massa at lectus iaculis malesuada. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris et convallis ex. Phasellus erat magna, tristique non urna sed, accumsan vestibulum odio. In convallis urna non leo consequat, ut fringilla arcu tempus. Pellentesque vel efficitur ex. Morbi placerat eget metus non malesuada. Pellentesque sit amet imperdiet mi. Sed cursus fringilla imperdiet. Ut malesuada bibendum dui sed vulputate.

    Sed finibus erat at pharetra malesuada. Nunc nec imperdiet ligula. Morbi ut pharetra enim. Cras mattis vehicula erat, eget dapibus tellus dignissim a. Praesent sed lectus a dolor interdum faucibus sed eu ex. Aenean vestibulum finibus dolor nec porta. Integer accumsan ligula commodo, lacinia nibh a, convallis quam. Maecenas non nibh metus. Curabitur pellentesque at magna eget imperdiet. Cras gravida nibh ac nulla blandit, ac pulvinar tortor faucibus. Integer ex urna, maximus in sem sit amet, maximus efficitur ante.
\end{document}

to get

enter image description here

17
  • Thanks. I tried using your answer inside wrapfigure, since that is the whole purpose of this question, and it throws an error: ! Package subcaption Error: This package can't be used in cooperation (subcaption) with the subfig package. Any clues?
    – Iris
    Feb 14, 2019 at 10:33
  • This answer is not suits with the requirement...
    – MadyYuvi
    Feb 14, 2019 at 10:40
  • @Iris Hijne subcaption and subfig are not compatible to one another. You must remove subcaption package to make use of this. Feb 14, 2019 at 11:03
  • @MadyYuvi could you please elaborate in detail why does this answer does not meet the requirements? Feb 14, 2019 at 11:03
  • 1
    The MWE in this answer was working perfectly and the reason my actual piece in my report was not working had, in the end, nothing to do with the wrapfigure or subfigure or subcaption environment whatsoever. It was a font-related issue in my caption due to a misplaced \ . So all problems solved now, learned a lot and this question and answer now contain some nice examples of how you can make wrapfigures with subfigures ;). The piece of code in the very beginning of my question is also an alternative, just like the answer by @Raaja
    – Iris
    Feb 15, 2019 at 10:32

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.