Graphics, Figures & TablesNumber of wrapping Lines for Figure

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
twiki
Posts: 3
Joined: Thu Feb 21, 2013 4:26 pm

Number of wrapping Lines for Figure

Post by twiki »

In the wrapfigure environment I don't know the number of lines in the paragraph(s) close to the image.

How do I resize automatically the height and width so that the image does not end up off the page?

In this case automatically means:

I would like the height and width of the image and/or the paragraph were adapted even if I change the image.

My MWE:

Code: Select all

\documentclass[a5paper]{article}
\usepackage[demo]{graphicx}
\usepackage{wrapfig}
\usepackage{blindtext}

\begin{document}
\large
\section*{First section}
\blindtext
%\begin{wrapfigure}{l}{5cm}
\begin{wrapfigure}[8]{l}{5cm}
\includegraphics[width=4.9cm]{test}
\caption{Test figure}
\end{wrapfigure}
\blindtext
\end{document}
I compile and find that there are 8 lines to be attached to the image, but after compilation!

If I use the commented wrapfigure environment, then on the next page I still wrapping! :?

Thank you for your attention!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Number of wrapping Lines for Figure

Post by cgnieder »

Hi twiki,

Welcome to the LaTeX community!

I don't think there is much you can do. The three indented lines in the next page you observe are the ones that would be needed for the caption of the figure if it wasn't at the bottom of the page. As it is now the caption is placed in the footer instead of the text body.

You're observing a principle problem when you have figures that are not allowed to float: you often have to change parameters or the position if the image manually to get it looking right.

So I'm afraid my answer is: the {wrapfigure} is already determining the needed lines automatically (and often does a decent job, I might add) but in cases like the one in your MWE the automatic mechanism has no chance and you have to intervene manually.

Regards
site moderator & package author
twiki
Posts: 3
Joined: Thu Feb 21, 2013 4:26 pm

Number of wrapping Lines for Figure

Post by twiki »

cgnieder wrote:Hi twiki,
Welcome to the LaTeX community!
Thank you!
I don't think there is much you can do. The three indented lines in the next page you observe are the ones that would be needed for the caption of the figure if it wasn't at the bottom of the page. As it is now the caption is placed in the footer instead of the text body.
My idea was to establish a fixed width for (all) wrap-figures
(e.g. 0.5\textwidth + overhang) and, if the required number of
adjacent lines of text exceeds the page, then AUTOMATICALLY reduce
the image until it is canceled the over-wrapping in next page.

In other words, the problem is to identify programmatically
if there is the over-wrapping in the next page; if there is,
LaTeX must reduce by a certain fixed amount the width (and/or height)
of the figure and repeat until they reach the size that
eliminate the over-wrapping.

Unfortunately I am not an expert TeX/LaTeX-programmer!
I do not know how to do it!
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: Number of wrapping Lines for Figure

Post by cgnieder »

The point is: it is not doable (if I know it right). When LaTeX is building the page and searching for a suitable page break it has already built paragraphs and forgotten about them.

Maybe this is somehow doable by recording some values to the aux-file so they are available on the next run but this (= paragraph shaping) is over my TeX skills. Maybe someone else can help here...

Regards
site moderator & package author
twiki
Posts: 3
Joined: Thu Feb 21, 2013 4:26 pm

Number of wrapping Lines for Figure

Post by twiki »

cgnieder wrote: Maybe this is somehow doable by recording some values to the aux-file so they are available on the next run but this (= paragraph shaping) is over my TeX skills. Maybe someone else can help here...
Established that the lower edge of the page is ymax; during compilation, so if the y position in which is placed an image of height h, plus a tolerance tol, just check if y + h + tol > ymax and if it is so then reduce h (eg h -> h/2)

It is not an operation logically complicated (from the point of view of a programming language)... you can not control because TeX can not infer the relative positions on the page of an object at "run-time"?

Grace for my ignorance of TeX!
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Number of wrapping Lines for Figure

Post by cgnieder »

Well, of course the math is easy, but as I tried to say before: the problem is the way input is processed and formed into paragraphs and pages. The long horizontal list of input tokens is first formed into paragraphs. Once they're formed TeX forgets about their details when building the pages from this vertical list of paragraphs. (This is probably wrong in detail(*) but the main idea is true.) So it can't try building a page and then decide to use another paragraph shape because that would be better suited. It's just impossible.

That said I still think what you want might be possible with the help of the aux file but needs someone who knows more about LaTeX's output routine and paragraph shaping than I do. I somehow doubt you'll get a better answer here, sorry.

Regards

(*) I need to re-read the relevant parts of the TeXbook...
site moderator & package author
Post Reply