1

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.

Iris
  • 321
  • 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 '19 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. – John Kormylo Feb 14 '19 at 17:12
  • After some long process, I solved my problem. The code in the first part of my question that uses subfigure' insidewrapfigure' 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 '19 at 10:27

1 Answers1

2

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

  • 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 '19 at 10:33
  • This answer is not suits with the requirement... – MadyYuvi Feb 14 '19 at 10:40
  • @Iris Hijne subcaption and subfig are not compatible to one another. You must remove subcaption package to make use of this. – Raaja_is_at_topanswers.xyz Feb 14 '19 at 11:03
  • @MadyYuvi could you please elaborate in detail why does this answer does not meet the requirements? – Raaja_is_at_topanswers.xyz Feb 14 '19 at 11:03
  • @Raaja I just realised this too, but removing it screws up all my other figures with subfigures (not wrapfigures) that are already in my report. Although I'm happy with your help, I understand MadyYuvi 's comment since your code doesn't allow me to use subcaption for my other figures. – Iris Feb 14 '19 at 11:11
  • @Iris Hijne This is one of the easiest way I know to achieve this. However, if you stick with subcaptions, may be other possible ways could be there (which, I honestly don't know about) ;-). – Raaja_is_at_topanswers.xyz Feb 14 '19 at 11:13
  • I understand, thanks. I do like the fact that it is clean like my 'old' way. If I don't find another option anytime soon, I might change all other figures in my report to use subfig.. Oh my am I looking forward to that! :p – Iris Feb 14 '19 at 11:19
  • @Iris Hijne Updated my answer, please check. Dont worry, you don't have to change. – Raaja_is_at_topanswers.xyz Feb 14 '19 at 11:19
  • @MadyYuvi Updated my answer to retain the original structure. Could you check? – Raaja_is_at_topanswers.xyz Feb 14 '19 at 11:20
  • @Raaja thank you, it looks great! For me it also works in the MWE as above, but when I do the exact same thing in my report I get some errors: ! Undefined control sequence. <argument> ...\tmspace +\thinmuskip {.1667em}\FMS /futm/m/n/10 \char 2}{}{} ... l.9 ...misbafomp}.\relax }}{15}{figure.caption.11} and ! Improper discretionary list. <recently read> } l.9 ...misbafomp}.\relax }}{15}{figure.caption.11} Any idea what could cause this? – Iris Feb 14 '19 at 11:32
  • @IrisHijne I am not sure, it says something like a font structure ;) I would really need you entire .log file to look in to this error. Because, it might be due to other packages in line with your entire document. – Raaja_is_at_topanswers.xyz Feb 14 '19 at 11:35
  • @Raaja yes, now this is a perfect one... – MadyYuvi Feb 14 '19 at 11:51
  • @IrisHijne I will open a chat session here, please cut-copy-paste here ;) – Raaja_is_at_topanswers.xyz Feb 14 '19 at 12:35
  • Please lose the original solution and leave Update1. – John Kormylo Feb 14 '19 at 17:10
  • @JohnKormylo Done!! I tried to catch u via chat, bit failed in doing so, terribly;) – Raaja_is_at_topanswers.xyz Feb 14 '19 at 20:11
  • 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 '19 at 10:32
  • Hello. How to insert subfigures horizontally by wrapfigure? – Y. zeng Feb 11 '23 at 13:51