2

I got a problem concerning the wrapfigure environment. I have worked with it before and managed to make it behave the way I want it. Currently im facing a problem which I boiled down to a minimum working example which illustrates the problem (see below). I found multiple questions on that topic (i.e. How to float box of text on the right side without leaving a gap throughout the rest of the document?) but could get a solution out of it. I used this general setup (Picture in the topright corner, with a heading and table next to it) before but the difference now is that i want the picture to be larger and the text beneath the table to float around it. Is there any way to achive this?

Sample output

%-*- coding: utf-8 -*-
\documentclass[twoside]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[draft]{graphicx}
\usepackage[paper=a4paper,inner=1.5cm,outer=2.5cm,top=2cm,bottom=2cm]{geometry}
\usepackage{wrapfig}

\begin{document}
\begin{wrapfigure}{r}{0.3\textwidth}
  \vspace{-10pt}
   \centering
   \includegraphics[width=0.29\textwidth, resolution=72]{test.png}
   % \vspace{-20pt}
\end{wrapfigure}
\section*{Maintitle\\{\small \emph{Subtitle}}}
\label{section_0526}
\begin{tabular}{l|l}
test: & Lorem ipsum dolor sit amet\\
\hline
test: & Lorem ipsum dolor sit amet\\
\hline
test: & Lorem ipsum dolor sit amet\\
\hline
test: & Lorem ipsum dolor sit amet\\
\hline
\end{tabular}\\

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.   

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.   

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.   

Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.   

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.   

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.   

Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.   

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.   

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.   

Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer
\end{document}
2

There are two things to tackle: too much space reserved for the figure and incorrect paragraph shapes long after the figure is finished.

You can manually specify how many lines of text are to be narrow by passing an optional argument to \wrapfigure. In this case an initial run shows two lines of text after the table, so you can write

\begin{wrapfigure}[2]{r}{0.3\textwidth}
....
\end{wrapfigure}

avoiding inserting manual vertical spacing around the figure.

Now the problem you also have is that the paragraph shape is being applied to subsequent paragraphs, long past the figure. The tabular is preventing wrapfigure from calling its closing code. You can call it manually by writing

\makeatletter
\WF@finale
\makeatother

after the first paragraph. For general convenience you could put

\makeatletter
\newcommand{\wrapfigcleanup}{\WF@finale}
\makeatother

in your preamble and then just call \wrapfigcleanup instead.

Sample output

\documentclass[twoside]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{graphicx}
\usepackage[paper=a4paper,inner=1.5cm,outer=2.5cm,top=2cm,bottom=2cm]{geometry}
\usepackage{wrapfig}

\makeatletter
\newcommand{\wrapfigcleanup}{\WF@finale}
\makeatother

\begin{document}

\begin{wrapfigure}[2]{r}{0.3\textwidth}
  \centering
  \includegraphics[width=0.29\textwidth]{example-image-a}
\end{wrapfigure}

\section*{Maintitle\\{\small \emph{Subtitle}}}
\label{section_0526}

\begin{tabular}{l|l}
  \hline
  test: & Lorem ipsum dolor sit amet\\
  \hline
  test: & Lorem ipsum dolor sit amet\\
  \hline
  test: & Lorem ipsum dolor sit amet\\
  \hline
  test: & Lorem ipsum dolor sit amet\\
  \hline
\end{tabular}

\smallskip

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam
et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet.

\wrapfigcleanup

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla facilisis at
vero eros et accumsan et iusto odio dignissim qui blandit praesent
luptatum zzril delenit augue duis dolore te feugait nulla
facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat.

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem
vel eum iriure dolor in hendrerit in vulputate velit esse molestie
consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et
accumsan et iusto odio dignissim qui blandit praesent luptatum zzril
delenit augue duis dolore te feugait nulla facilisi.

Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id quod mazim placerat facer

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam
et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla facilisis at
vero eros et accumsan et iusto odio dignissim qui blandit praesent
luptatum zzril delenit augue duis dolore te feugait nulla
facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat.

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem
vel eum iriure dolor in hendrerit in vulputate velit esse molestie
consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et
accumsan et iusto odio dignissim qui blandit praesent luptatum zzril
delenit augue duis dolore te feugait nulla facilisi.

Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id quod mazim placerat facer

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam
et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla facilisis at
vero eros et accumsan et iusto odio dignissim qui blandit praesent
luptatum zzril delenit augue duis dolore te feugait nulla
facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat.

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem
vel eum iriure dolor in hendrerit in vulputate velit esse molestie
consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et
accumsan et iusto odio dignissim qui blandit praesent luptatum zzril
delenit augue duis dolore te feugait nulla facilisi.

Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id quod mazim placerat facer

\end{document}

I have replaced the line break \\ after the table with \smallskip, you may want a bigger space and could use \medskip or \bigskip. Also I hope you are aware that vertical lines in table are not recommended, see the booktabs package for a general discussion.

Generally wrapfig does not work well in combinations with list environments. The tabular environment cases similar problems. In both cases, it is because the environments and wrapfigure both try to manipulate the paragraph building commands by redefining the standard commands. When a list or a tabular ends, the wrong values get restored to the macros. There does not seem to be an easy way to work around this automatically, but the possible need to invoke \WF@finale is mentioned in the wrapfig documentation, though not as a suggestion at user level.

  • Could you explain why "The tabular is preventing wrapfigure from calling its closing code. You can call it manually by writing" is true? Thanks for the answer and the link to the booktabs package documentation. – soriak Jul 5 '16 at 20:00
  • @soriak I have added some comments, but I do not understand all the details of exactly why it gives a problem. If someone did, then an automatic fix would have been made available. – Andrew Swann Jul 6 '16 at 9:09
0

Everything I came to know about this when facing the exact same problem:

The reason for this gap is, that the vertical space required for the picture is larger than the vertical space of the paragraph floating around it. As the next paragraph starts, wrapfigure reserves the full vertical space of the picture again, which results in the large gap. This problem "survives" pagebreaks.

This is a general problem of wrapfigure. The core problem, if one tried to tackle this problem, is the glue *TeX uses/may use between boxes. In this specific case it is foremost the glue between paragraphs, which is not a fixed length, but depends on the content of the page. A lot of content introduces glue to a page, e.g. headings, itemize like environments and (of course) floats, to name a few.

Therefore it is neigh impossible to calculate how much vertical space to reserve from the following paragraph to make the picture fit nicely. The full height of the picture + padding is of course an upper boundary.

To get what you want you would be telling LaTeX about your page layout, so that it would reserve the appropriate height and width in the upper right corner, and then place all its content around it.

I always envisioned a solution involving dynamically changing \hsize and other related sizes, triggered by reaching a certain distance to the top of the page (\pagetotal).

Somebody once told me that is/should be a rather simple thing to achieve, even in plain TeX, but I haven't been able to figure it out. I suppose one could talk oneself into changing the definition of \par to do some calculations when appropriate, but touching something like \par intuitively feels foolish and like an excellent approach to break typesetting.

Edit: I know this is not a straight up answer. But I think this is an important problem, hence I contribute what I can.

  • thanks for your reply, even I admit its not a straight up answer. Maybe Andrews answer helps you as well. But I guess you both do understand latex far better than I do. – soriak Jul 5 '16 at 20:05

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.