2

An MWE showing the behaviour of wrapfig next to a list of short tables below.

\documentclass{article}
\usepackage[]{wrapfig}
\usepackage[]{booktabs}
\usepackage[]{blindtext}
\setlength{\parindent}{0pt} % Without it, all \entry below will be indented, which is not desireable for the MWE's purposes.

\newcommand{\tab}{%
    \begin{tabular}{r}
    \toprule 
    This \\ \midrule
    is \\
    a \\
    table \\
    \bottomrule
    \end{tabular}
    }

\newcommand{\entry}{%
    \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}lr@{}}
        \toprule
        a & b \\ \midrule
        c & d \\
        \bottomrule
    \end{tabular*}
    }

\begin{document}
\blindtext

\begin{wraptable}{l}{0pt}
\tab
\end{wraptable}
\entry

\entry

\entry

\entry

\blinddocument
\end{document}

The output (first page) is given below.

MWE output page 1

As can be seen, the wraptable is positioned vertically where expected. However, horizontally it is not: it doesn't reduce \linewidth next to it, as I would expect. It should result in the a number of the \entry wrapping around the wraptable, i.e. changing the \linewidth of those that fit next to the wraptable. 1. Can this be achieved and how?

Also, it does indent the text below the four \entry. This indentation repeats after every \par throughout the rest of the document. 2. Why does this repeated indentation happen?

The wraptable has some vertical space around it. Horizontally it has some smaller space, I'm guessing of length \columnsep. 3. Can the vertical space be reduced to be equal to the horizontal space between the wrapped text and wraptable/wrapfigure?

3

wrapfig works by using the height of the wrapped object to calculate the number of lines of text which it should shorten and then shortening those lines. Sometimes, this isn't right and you can override the number with the optional argument. In this case, that is never going to be enough because it is effectively unable to find any lines which it can shorten until it comes across some ordinary text.

Basically, wrapping text around figures or tables is hard in LaTeX and no solution can avoid considerable fragility. wrapfig does its best by explaining the limitations etc. in the documentation and offering workarounds for certain kinds of trouble.

In this case, the best thing to do would be to simply do the wrapping manually, relying on minipages, for example.

This needs tweaking, but something like this:

\documentclass{article}
\usepackage{booktabs,calc}
\usepackage{blindtext}
\setlength{\parindent}{0pt} % Without it, all \entry below will be indented, which is not desireable for the MWE's purposes.
\newcommand{\tab}{%
  \begin{tabular}{r}
    \toprule
    This \\ \midrule
    is \\
    a \\
    table \\
    \bottomrule
  \end{tabular}%
}
\newcommand{\entry}{%
  \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}lr@{}}
    \toprule
    a & b \\ \midrule
    c & d \\
    \bottomrule
  \end{tabular*}%
}
\begin{document}
\blindtext

\begin{minipage}[]{25mm}
  \tab
\end{minipage}%
\begin{minipage}{\linewidth-25mm}
  \entry

  \entry

  \entry

  \entry
\end{minipage}

\blinddocument
\end{document}

will yield

fake-wrapping for tabular

This isn't very convenient, but we can define a wrapper to help a bit. \wrapmytable[]{}{} takes one optional and two mandatory arguments. If specified, the optional argument specifies the width of the left hand part. Otherwise, this is the natural width of the content. The second and third arguments specify the left and right hand contents. \columnsep is allowed as separation between the two parts.

For example,

\wrapmytable{\tab}{%
  \entry

  \entry

  \entry

  \entry
}

produces

convenience macro's output

Complete code:

\documentclass{article}
\usepackage{booktabs,calc}
\usepackage{blindtext}
\setlength{\parindent}{0pt} % Without it, all \entry below will be indented, which is not desireable for the MWE's purposes.
\newcommand{\tab}{%
  \begin{tabular}{r}
    \toprule
    This \\ \midrule
    is \\
    a \\
    table \\
    \bottomrule
  \end{tabular}%
}
\newcommand{\entry}{%
  \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}lr@{}}
    \toprule
    a & b \\ \midrule
    c & d \\
    \bottomrule
  \end{tabular*}%
}
\newlength\mywrapwidth
\newcommand\wrapmytable[3][]{%
  \edef\tempa{}%
  \edef\tempb{#1}%
  \ifx\tempa\tempb\settowidth{\mywrapwidth}{#2}\else\setlength\mywrapwidth{#1}\fi
  \smallskip
  \begin{minipage}{\mywrapwidth}
    #2
  \end{minipage}\hskip \columnsep
  \begin{minipage}{\linewidth-\mywrapwidth-\columnsep}
    #3
  \end{minipage}%
  \smallskip
}
\begin{document}
\blindtext

\wrapmytable{\tab}{%
  \entry

  \entry

  \entry

  \entry
}

\blinddocument
\end{document}

EDIT

To respond to the query in comments, use of the t placement option in the optional arguments for minipage and tabular allows the left and right parts to be aligned at the top, as requested.

aligned to top

Code:

\documentclass{article}
\usepackage{booktabs,calc}
\usepackage{blindtext}
\setlength{\parindent}{0pt} % Without it, all \entry below will be indented, which is not desireable for the MWE's purposes.
\newcommand{\tab}{%
  \begin{tabular}[t]{r}
    \toprule
    This \\ \midrule
    is \\
    a \\
    table \\
    \bottomrule
  \end{tabular}%
}
\newcommand{\entry}{%
  \begin{tabular*}{\linewidth}[t]{@{\extracolsep{\fill}}lr@{}}
    \toprule
    a & b \\ \midrule
    c & d \\
    \bottomrule
  \end{tabular*}%
}
\newlength\mywrapwidth
\newcommand\wrapmytable[3][]{%
  \edef\tempa{}%
  \edef\tempb{#1}%
  \ifx\tempa\tempb\settowidth{\mywrapwidth}{#2}\else\setlength\mywrapwidth{#1}\fi
  \smallskip
  \begin{minipage}[t]{\mywrapwidth}
    #2
  \end{minipage}\hskip \columnsep
  \begin{minipage}[t]{\linewidth-\mywrapwidth-\columnsep}
    #3
  \end{minipage}%
  \smallskip
}
\begin{document}
\blindtext

\wrapmytable{\tab}{%
  \entry

  \entry

  \entry

  \entry
}

\blinddocument
\end{document}

Edit by OP below, original answer above left unedited.

This also works quite nicely to indent just a few of the \entry, but might require some tweaking for the particular implementation.

\entry

\wrapmytable[]{\tab}{%
  \entry

  \entry

  \entry
}

\entry

yields

<code>\entry</code> wrapping around the side table

I've edited the \entry to show \the\linewidth to illustrate the effect of the minipages. As you might notice, the vertical space between the \entry where the \linewidth changes is not as small as it can be. This can be solved by disabling the two \smallskips in the definition of \wrapmytable. This is probably fine if the third argument of \wrapmytable has greater height (plus some skip if desired) than the second input argument.

| improve this answer | |
  • Thank you for your elaborate answer. I've always found wrapfig difficult to use. Your use of minipages is quite nice and versatile. I took the freedom to edit your answer based on an edit in my question: I now explicitly ask for a fraction of the \entry to be indented. This was not clear in my original question. – Erik May 3 '16 at 6:28
  • Is it possible to align the left minipage at the top of the right minipage? When I try \begin{minipage}[t]... the alignment is not right. – Erik May 3 '16 at 9:50
  • @Erik Did you add the t to both minipages? – cfr May 3 '16 at 12:12
  • I did, but it doesn't align the boxes correctly. I'm looking into the adjustbox package to create two boxes of identical height, being exactly the height of the largest of the two (which will be the right minipage in my case). Then, I hope the contents of both boxes can be aligned to the same top edges. No luck so far! – Erik May 3 '16 at 12:16
  • 1
    Sorry - you are using tabular and tabular* so you need to make them top-aligned, too. Please see edit above. – cfr May 3 '16 at 12:24

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.