Tx · LaTeX & TikZ

How to Use wrapfig with mdframed in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Understand the conflict

    wrapfig needs normal document flow, while mdframed collects its contents inside a box.

  2. 2. Avoid nesting

    Do not place a wrapfigure environment directly inside an mdframed environment.

  3. 3. Choose a layout

    Use side-by-side minipages when the image belongs inside the frame.

  4. 4. Use wrapping outside

    Place wrapfigure in ordinary document text when flowing text around the image matters most.

The wrapfig package makes text flow around a figure. The mdframed package draws a frame around content and can split that content across pages. These jobs use different TeX mechanisms, so placing wrapfigure directly inside mdframed is not a reliable arrangement.

The usual solution depends on the intended appearance: use a side-by-side layout for an image that must be inside the frame, or use wrapfigure in ordinary document text when flowing paragraphs around the image is the priority.

Why the environments conflict

A wrapfigure does more than insert an image. It changes the shape of the following paragraphs by adjusting the available line width for several lines. To do that, it must operate in the surrounding vertical and paragraph lists.

By contrast, mdframed collects its contents in a box so that it can draw borders, add margins, and—when requested—split the material into framed pieces. Material inside that box is no longer ordinary page-level paragraph flow. Consequently, a wrapfigure inside mdframed cannot reliably affect the paragraphs in the way wrapfig expects.

This is the problematic pattern:

\begin{mdframed}
  \begin{wrapfigure}{r}{0.35\linewidth}
    \includegraphics[width=\linewidth]{picture}
  \end{wrapfigure}
  Text that is supposed to wrap around the picture.
\end{mdframed}

Depending on the exact setup, this can produce an error, incorrect spacing, or text that does not wrap as intended. Changing the placement option from r to R does not fix the underlying issue. wrapfig has no [H] placement option: its optional bracket argument is a number of lines to narrow. In any case, changing placement controls where the wrapped figure is placed, not whether paragraph shaping works inside a box.

If the figure only needs to appear next to the text inside a frame, use two minipage environments. This is predictable and works well with mdframed.

The following is a complete example. It uses the mwe package only to provide the demonstration image example-image.

\documentclass{article}

\usepackage{graphicx}
\usepackage{mdframed}
\usepackage{mwe} % Provides example-image for this demonstration

\begin{document}

\begin{mdframed}[
  linewidth=0.8pt,
  linecolor=black,
  innertopmargin=8pt,
  innerbottommargin=8pt
]
\noindent
\begin{minipage}[t]{0.34\linewidth}
  \vspace{0pt}
  \centering
  \includegraphics[width=\linewidth]{example-image}

  \small Figure-like image
\end{minipage}
\hfill
\begin{minipage}[t]{0.60\linewidth}
  \vspace{0pt}
  This text is placed beside the image. It does not wrap around the
  image line by line, but the two columns remain inside the same
  mdframed box and have predictable dimensions.

  A side-by-side layout is usually preferable for short explanations,
  examples, definitions, and other content that should stay together.
\end{minipage}
\end{mdframed}

\end{document}

The two widths, 0.34\linewidth and 0.60\linewidth, sum to 0.94\linewidth, leaving 0.06\linewidth for \hfill. Within mdframed, \linewidth is normally the usable content width after the frame’s internal margins; adjust the fractions if the image or text needs more space. The [t] option aligns both minipages at their top edges, while \vspace{0pt} makes that alignment take effect consistently.

This approach creates columns rather than genuine paragraph wrapping. That distinction is useful: the text always occupies one rectangular column, so it will not move underneath the image.

If genuine wrapping is required

Use wrapfigure outside mdframed, in ordinary document flow:

\documentclass{article}

\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{mwe}

\begin{document}

\begin{wrapfigure}{r}{0.35\textwidth}
  \vspace{-10pt}
  \centering
  \includegraphics[width=\linewidth]{example-image}
  \caption{A wrapped figure}
\end{wrapfigure}

This paragraph is ordinary document text, so wrapfig can reduce its
line width and make the lines flow around the image. Continue writing
until the wrapping area has been used up. The figure is not enclosed in
an mdframed box in this arrangement.

\end{document}

Here, the figure and paragraph must be placed in the normal page stream. Do not put the wrapfigure inside a minipage, parbox, mdframed, or another boxed environment. Also avoid starting a section heading, display equation, list, or another major environment immediately after the wrapped figure; wrapfig works best when followed by ordinary paragraphs.

If the surrounding text must also have a border, one possible compromise is to frame separate blocks before and after the wrapped material. However, a single continuous mdframed border around genuinely wrapped text is not what these packages are designed to provide.

Common mistakes

Treating wrapfigure like an ordinary figure

wrapfigure is not a normal floating figure. Its placement is sensitive to the current paragraph and page position. It should be inserted near the paragraph that will wrap around it.

Using a full-width frame around a wrapped figure

Putting the frame around the figure and text changes the content into a box. The wrap mechanism then loses the page-level paragraph information it needs.

Forgetting internal frame margins

Inside mdframed, \linewidth normally denotes the content width after the frame’s inner margins have been applied. If two minipages add up to exactly \linewidth, however, any inter-column space or source whitespace can make them overrun. Leave some spare width, as in the example.

Expecting page breaks in a minipage

A minipage cannot normally break across pages. It is suitable for a compact image-and-text block, but not for a long multi-page layout. mdframed can break across pages in suitable configurations, but the individual minipages inside it still behave as boxed, unbreakable units.

For long framed content, keep the image and text in separate paragraphs or use a purpose-built multi-column layout rather than trying to combine wrapfig with the frame.

References

  1. The mdframed package documentation
  2. The wrapfig package documentation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.