11

Is it possible to give figures a "range" for their size? For example from .3\textwidth to .5\textwidth or 5cm +- 10% or 5 cm +- 0.1\textwidth. You give a range for all images and TeX chooses the actual size within this range for best float placement and best "look" of the document. This could be helpful for the TeX algorithms to find a better or at least more flexible float placement and sofore a lower badness. It should be possible to "override" this auto-sizing for selected images as well, but I think this won't be any problem... Is my suggestion to abitious? I know there would be many variables though there it would be hard to find a solution... would take quite a time to run through all possible combinations. You could narrow it down to only apply this range for figures, whose floats are in some way "critical"... Also it could be a way of accelerating this process by increase the incrementation of the range (for example only two increments: +- 10 % resp. +- 0.5 cm for example). To be honest I don't think this would (easily) be possible to realize with TeX, but hope dies last! ;)

yo'
  • 51,322
ChrisP
  • 113

2 Answers2

5

You can set the width to be conditional on anything that TeX can test for; setting width to be the maximum of the natural size and \textwidth is quite common for example. However unless you want to re-write large parts of the LaTeX float placement algorithm, the decisions have to be made at the time the float is set and can not be made while LaTeX is deciding on the float placement as the floats are saved as boxes so the internal structure is not available to the float placement code to re-set the value.

On the other hand it is possible (but massively fragile and incompatible with any other code interfacing too figures) to hide a fixed number of variants into the float box and just use one depending on some information in the output routine (eg left or right page, or top or bottom float) see this answer

David Carlisle
  • 757,742
3

To extend a little the David's reference to conditional sizes, keep in mind that you can set both width and height as relative maximum sizes of text layout (or any other predetermined size) and maintain the aspect ratio of the image with the keepaspectratio option.

This add in fact a lot of flexibility, as the same rule can avoid scaling too much portrait images or too little panoramic pictures in vertical pages.

In the below example, the text lengths are hacked to simulate first an 1x1 text layout and later another four rectangular layouts.

In each case, an image of original 1x1 aspect ratio is repeated ten times with width and height settings between 10% and 90% in inverse order (with the help of a macro to simplify the coding).

In the unusual case of \textwidth = \textheight, obviously setting both sizes have no sense at all, and the maximum scaling here is done when both sizes are the same (0.5\textwidth = 0.5\textheight), but in rectangular text areas (the common situation) the maximum scaling is done setting the width at 10-20%, 30-40%, 60-70% or 90-100%, depending of the text portrait/landscape layout.

MWE

\documentclass{article}
\usepackage{mwe}

\newcommand\EXAMPLE[2]{\includegraphics[width=.#1\textwidth, height=.#2\textheight,keepaspectratio]{example-image-1x1}\hfill}

\begin{document}


Scheme:

\medskip

\footnotesize
Text
Layout: Image max. \verb|10% width| or \verb|90% height| 
\dotfill  max. \verb|90% width| or \verb|10% height| 

\large\bigskip
\setlength{\textwidth}{2.6cm}
\setlength{\textheight}{2.6cm}

$1\times1$: \hfill
\EXAMPLE{9}{1}
\EXAMPLE{9}{1}
\EXAMPLE{8}{2}
\EXAMPLE{7}{3}
\EXAMPLE{6}{4}
\EXAMPLE{5}{5}
\EXAMPLE{4}{6}
\EXAMPLE{3}{7}
\EXAMPLE{2}{8}
\EXAMPLE{1}{9} 

\medskip
\setlength{\textwidth}{2cm}
\setlength{\textheight}{4cm}

$2\times4$: \hfill 
\EXAMPLE{9}{1}
\EXAMPLE{8}{2}
\EXAMPLE{7}{3}
\EXAMPLE{6}{4}
\EXAMPLE{5}{5}
\EXAMPLE{4}{6}
\EXAMPLE{3}{7}
\EXAMPLE{2}{8}
\EXAMPLE{1}{9} 


\medskip
\setlength{\textwidth}{4cm}
\setlength{\textheight}{2cm}

$4\times2$: \hfill 
\EXAMPLE{9}{1}
\EXAMPLE{8}{2}
\EXAMPLE{7}{3}
\EXAMPLE{6}{4}
\EXAMPLE{5}{5}
\EXAMPLE{4}{6}
\EXAMPLE{3}{7}
\EXAMPLE{2}{8}
\EXAMPLE{1}{9}

\medskip
\setlength{\textwidth}{1cm}
\setlength{\textheight}{8cm}

$1\times8$: \hfill 
\EXAMPLE{9}{1}
\EXAMPLE{8}{2}
\EXAMPLE{7}{3}
\EXAMPLE{6}{4}
\EXAMPLE{5}{5}
\EXAMPLE{4}{6}
\EXAMPLE{3}{7}
\EXAMPLE{2}{8}
\EXAMPLE{1}{9} 

\medskip
\setlength{\textwidth}{8cm}
\setlength{\textheight}{1cm}
$8\times1$: \hfill 
\EXAMPLE{9}{1}
\EXAMPLE{8}{2}
\EXAMPLE{7}{3}
\EXAMPLE{6}{4}
\EXAMPLE{5}{5}
\EXAMPLE{4}{6}
\EXAMPLE{3}{7}
\EXAMPLE{2}{8}
\EXAMPLE{1}{9} 

\end{document}
Fran
  • 80,769