Discussion:
Graphics sized by area?
(too old to reply)
V***@googlemail.com
2007-02-16 00:04:57 UTC
Permalink
My respect to the TeX experts!

I'm writing a story with some illustrations (photos) in LaTeX. The
photos are of different aspect ratio and if I set their size in the
document by width or height, they look in the document as if some are
smaller than others. It would be nice to specify the area of the
images instead. Currently I adjust the size manually for every image
so that take approximately the same area in the document. Would be
great if this could be automated.

I'm using floatflt and code looks as such:

\begin{floatingfigure}[l]{75mm}
\noindent{\fbox{\includegraphics[width=74mm]{photo.jpg}}}
\end{floatingfigure}
Lars Madsen
2007-02-16 00:43:43 UTC
Permalink
Post by V***@googlemail.com
My respect to the TeX experts!
I'm writing a story with some illustrations (photos) in LaTeX. The
photos are of different aspect ratio and if I set their size in the
document by width or height, they look in the document as if some are
smaller than others. It would be nice to specify the area of the
images instead. Currently I adjust the size manually for every image
so that take approximately the same area in the document. Would be
great if this could be automated.
\begin{floatingfigure}[l]{75mm}
\noindent{\fbox{\includegraphics[width=74mm]{photo.jpg}}}
\end{floatingfigure}
try using \linewidth inside the smaller area

But remember that the \fbox also takes up some space.

/daleif
V***@googlemail.com
2007-02-16 01:02:27 UTC
Permalink
Post by Lars Madsen
try using \linewidth inside the smaller area
All of the figures are much smaller than column width, this is not a
problem. I mean that I want TeX to adjust the sizes automatically to
the same area, so that width*height is the same for all images.
Lars Madsen
2007-02-16 09:24:27 UTC
Permalink
Post by V***@googlemail.com
Post by Lars Madsen
try using \linewidth inside the smaller area
All of the figures are much smaller than column width, this is not a
problem. I mean that I want TeX to adjust the sizes automatically to
the same area, so that width*height is the same for all images.
that might not be a good idea, since you might introduce unwanted stretching in
some direction. Consider a square picture of a circle, being stretched to fit a
2 by 4 in area, then it's not a picture of a circle any more.
--
/daleif (remove RTFSIGNATURE from email address)

LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=minxampl
V***@googlemail.com
2007-02-16 11:21:22 UTC
Permalink
Post by Lars Madsen
that might not be a good idea, since you might introduce unwanted stretching in
some direction. Consider a square picture of a circle, being stretched to fit a
2 by 4 in area, then it's not a picture of a circle any more.
I'm not going to change the aspect ratio.
Scott Pakin
2007-02-16 04:12:38 UTC
Permalink
Post by V***@googlemail.com
I'm writing a story with some illustrations (photos) in LaTeX. The
photos are of different aspect ratio and if I set their size in the
document by width or height, they look in the document as if some are
smaller than others. It would be nice to specify the area of the
images instead. Currently I adjust the size manually for every image
so that take approximately the same area in the document. Would be
great if this could be automated.
\begin{floatingfigure}[l]{75mm}
\noindent{\fbox{\includegraphics[width=74mm]{photo.jpg}}}
\end{floatingfigure}
I believe it can be automated but it may be a bit tricky. Here's how I'd
probably go about it:

1) Use LaTeX's \savebox command to store the \includegraphics in a box.
2) Calculate the width and height+depth of the box using \wd, \ht, and \dp.
3) Use \FPqsolve from the fp package to solve width*height*x^2 + 0*x + -area = 0.
4) Specify the \includegraphics with width and height each scaled by x.

-- Scott
V***@googlemail.com
2007-02-16 13:56:29 UTC
Permalink
Post by Scott Pakin
3) Use \FPqsolve from the fp package to solve width*height*x^2 + 0*x + -area = 0.
I tried to modify the example by Bernd Strieder to include the square
root calculation using the FP package. But I got into trouble feeding
my length arguments (defined by \newlength) as arguments of \FPupn.
Does anybody know if it's possible at all?
Robin Fairbairns
2007-02-16 14:18:50 UTC
Permalink
Post by V***@googlemail.com
Post by Scott Pakin
3) Use \FPqsolve from the fp package to solve width*height*x^2 + 0*x + -area = 0.
I tried to modify the example by Bernd Strieder to include the square
root calculation using the FP package. But I got into trouble feeding
my length arguments (defined by \newlength) as arguments of \FPupn.
Does anybody know if it's possible at all?
show us your code: "got into trouble" isn't an awfully good diagnostic
code.

(best of all, of course, provide a minimal working example...)
--
Robin Fairbairns, Cambridge
V***@googlemail.com
2007-02-16 14:43:14 UTC
Permalink
show us your code: "got into trouble" isn't an awfully good diagnostic code.
OK. Here is my example.

\documentclass[a4paper]{minimal}
\usepackage{graphicx,calc,fp-eval}
\begin{document}
\newlength\oldwidth \setlength\oldwidth{100pt}
\newlength\oldheight \settoheight{\oldheight}{\includegraphics[width=
\oldwidth]{photo.jpg}}

%Then I need to calculate the square root of oldwidth/oldheight,
something like:

\FPeval{scaling}{root(oldwidth/oldheight,2)} %This line doesn't work
(although it works with numbers)

%So that I can later do this:

\setlength\newwidth{\oldwidth*\scaling}
%This also doesn't work, because I need to convert somehow the result
of FPeval to a normal number

\includegraphics[width=\newwidth]{photo.jpg}
\end{document}
Robin Fairbairns
2007-02-16 16:18:10 UTC
Permalink
Post by V***@googlemail.com
show us your code: "got into trouble" isn't an awfully good diagnostic code.
OK. Here is my example.
\documentclass[a4paper]{minimal}
\usepackage{graphicx,calc,fp-eval}
\begin{document}
\newlength\oldwidth \setlength\oldwidth{100pt}
\newlength\oldheight \settoheight{\oldheight}{\includegraphics[width=
\oldwidth]{photo.jpg}}
%Then I need to calculate the square root of oldwidth/oldheight,
it would have been better if you had kept yourself to comments within
the line length allowed by your news client ;-)
Post by V***@googlemail.com
\FPeval{scaling}{root(oldwidth/oldheight,2)} %This line doesn't work
(although it works with numbers)
\FPeval{scaling}{root(\oldwidth/\oldheight,2)}

seems to do the business.
note, you hadn't created \newwidth
Post by V***@googlemail.com
\setlength\newwidth{\oldwidth*\scaling}
no: you're using a length here, and multiplying a length by a scalar
isn't one of calc's things (i suppose) since tex will do it direct:

\setlength\newwidth{\scaling\oldwidth}
Post by V***@googlemail.com
%This also doesn't work, because I need to convert somehow the result
of FPeval to a normal number
\includegraphics[width=\newwidth]{photo.jpg}
of course, putting stuff into \newwidth, so that \includegraphics can
copy it into one of its own internal registers, is a bit extreme. i
would do

\includegraphics[width=\scaling\oldwidth]{photo.jpg}

hth.
--
Robin Fairbairns, Cambridge
V***@googlemail.com
2007-02-16 16:34:12 UTC
Permalink
Post by Robin Fairbairns
\FPeval{scaling}{root(\oldwidth/\oldheight,2)}
Did you try it? It is all the same: Illegal character \oldwidth found
in float number.
Post by Robin Fairbairns
no: you're using a length here, and multiplying a length by a scalar
I think in calc it should work. But how to make a scalar out of
\scaling?
Post by Robin Fairbairns
\includegraphics[width=\scaling\oldwidth]{photo.jpg}
I'll be able to try it after I have the problem with FPeval solved.
Robin Fairbairns
2007-02-16 16:53:50 UTC
Permalink
Post by Robin Fairbairns
\FPeval{scaling}{root(\oldwidth/\oldheight,2)}
seems to do the business.
oops.

%***********
\makeatletter
\edef\oldwidthm{\expandafter\***@pt\oldwidth}
\edef\oldheightm{\expandafter\***@pt\oldheight}
%***********
\FPeval{scaling}{root(\oldwidthm/\oldheightm,2)}

it may work as i wrote it above, but my actual test used the code here.
--
Robin Fairbairns, Cambridge
V***@googlemail.com
2007-02-16 17:33:21 UTC
Permalink
Yes, this version works. Thanks a lot!
V***@googlemail.com
2007-02-16 22:36:37 UTC
Permalink
Robin, one more question to you, hopefully the last one in this
branch.

I want to group all this code in some \def or \newcommand environment
to place it in the preambule so that the code near each figure is as
short as possible.

If I do it like this, then there is some problem with nesting of \edef
inside of \def. May be you will help me to resolve it. Here is the
code:

\documentclass[a4paper]{minimal}
\usepackage{graphicx,calc,fp-eval}

\newlength\oldheight \newlength\newwidth

\def\doscaling#1
{ \includegraphics[width=100pt]{#1}
\settoheight{\oldheight}{\includegraphics[width=100pt]{#1}}
\makeatletter
\edef\oldwidthm{\expandafter\***@pt\oldwidth}
\edef\oldheightm{\expandafter\***@pt\oldheight}
\FPeval{scaling}{root(\oldwidthm/\oldheightm,2)}
\makeatother}

\begin{document}

\doscaling{photo.jpg} \includegraphics[width=\scaling\oldwidth]
{photo.jpg}

\end{document}
Robin Fairbairns
2007-02-17 11:16:14 UTC
Permalink
Post by V***@googlemail.com
Robin, one more question to you, hopefully the last one in this
branch.
I want to group all this code in some \def or \newcommand environment
to place it in the preambule so that the code near each figure is as
short as possible.
If I do it like this, then there is some problem with nesting of \edef
inside of \def. May be you will help me to resolve it. Here is the
\documentclass[a4paper]{minimal}
\usepackage{graphicx,calc,fp-eval}
\newlength\oldheight \newlength\newwidth
\def\doscaling#1
{ \includegraphics[width=100pt]{#1}
\settoheight{\oldheight}{\includegraphics[width=100pt]{#1}}
\makeatletter
\FPeval{scaling}{root(\oldwidthm/\oldheightm,2)}
\makeatother}
no, that's not the problem.

the meaning of "@" in the macro expansion is assigned as the macro is
defined; no amount of \makeatletter _inside_ the macro will help.
replace your definition with

\makeatletter
\def\doscaling#1{%
\includegraphics[width=100pt]{#1}%
\settoheight{\oldheight}{\includegraphics[width=100pt]{#1}}%
\edef\oldwidthm{\expandafter\***@pt\oldwidth}%
\edef\oldheightm{\expandafter\***@pt\oldheight}%
\FPeval{scaling}{root(\oldwidthm/\oldheightm,2)}%
}
\makeatother

(note, i've also protected all ends-of-line so as not to introduce
unwanted spacing. probably not an issue in this case, but it's a good
habit to get in to.)
--
Robin Fairbairns, Cambridge
V***@googlemail.com
2007-02-17 12:54:36 UTC
Permalink
Post by Robin Fairbairns
defined; no amount of \makeatletter _inside_ the macro will help.
replace your definition with...
I already tried this, but even in such form the compiler gives the
same error that \doscaling is an undefined command (at the line
\doscaling{photo.jpg}).
Robin Fairbairns
2007-02-17 15:18:25 UTC
Permalink
Post by V***@googlemail.com
Post by Robin Fairbairns
defined; no amount of \makeatletter _inside_ the macro will help.
replace your definition with...
I already tried this, but even in such form the compiler gives the
same error that \doscaling is an undefined command (at the line
\doscaling{photo.jpg}).
you've got a definition of \newwidth (which isn't used) but not of
\oldwidth (which is).
--
Robin Fairbairns, Cambridge
V***@googlemail.com
2007-02-18 01:17:15 UTC
Permalink
I've corrected a few more bugs. Works perfectly. Here is the final
version in case someone needs it:

\documentclass[a4paper]{minimal}
\usepackage{graphicx,calc,fp,floatflt}

% ------------- Macro \areafloat[r/l]{<area>}{<filename>}{<caption>}
-------------

\newlength\oldheight \newlength\oldwidth \newlength\newwidth
\makeatletter
\newcommand\areafloat[4][r]{%
\settowidth{\oldwidth}{\includegraphics[width=#2]{#3}}%
\settoheight{\oldheight}{\includegraphics[width=#2]{#3}}%
\edef\oldwidthm{\expandafter\***@pt\oldwidth}%
\edef\oldheightm{\expandafter\***@pt\oldheight}%
\FPeval{scaling}{root(2,\oldwidthm/\oldheightm)}%
\setlength\newwidth{\scaling\oldwidth}%
\parbox{\textwidth}{}\vspace{-\baselineskip}\vspace{-0.7em}%
\begin{floatingfigure}[#1]{\newwidth}%
\vspace{-0.85em}\noindent%
\fbox{\includegraphics[width=\newwidth-2.0\fboxsep-2.0\fboxrule]
{#3}}%
\medskip\\ \footnotesize{#4\smallskip}%
\end{floatingfigure}%
\parbox{\textwidth}{}\vspace{-\baselineskip}\vspace{-0.7em}%
}\makeatother

% -------------- Area of the float (it is actually the sqrt of the
area) --------------

\newlength\floatarea \setlength\floatarea{75mm}

% ------------------------------------ Example of usage
-------------------------------------

\begin{document}
\areafloat[r]{\floatarea}{photo.jpg}{Figure caption.}
Some text here in which the figure will float.
\end{document}

Bernd Strieder
2007-02-16 10:58:45 UTC
Permalink
Hello,
Post by V***@googlemail.com
I'm writing a story with some illustrations (photos) in LaTeX. The
photos are of different aspect ratio and if I set their size in the
document by width or height, they look in the document as if some are
smaller than others. It would be nice to specify the area of the
images instead.
I've been curious about this, so I tried. There is my prototype using
\resizebox from the graphics/graphicx package to scale text, graphics
should work analogously.

\documentclass[a4paper]{minimal}
\usepackage{graphicx}
\usepackage{calc}
\begin{document}
\newlength\gaga
Blah
\settowidth{\gaga}{Blah}
width:\the\gaga
\settoheight{\gaga}{Blah}
height:\the\gaga
\resizebox*{1pt*\ratio{140pt}{\height}}{1pt*\ratio{140pt}{\width}}{Blah}
\resizebox*{1pt*\ratio{600pt}{\height}}{1pt*\ratio{600pt}{\width}}{Blah}
\end{document}

First I determine width and height of "Blah" as 20.4pt and 7pt, which is
140 square pt. The calc package defines \ratio and makes infix
multiplication possible in the width and height parameter to
\resizebox*, also the natural \width and \height are available. So
dividing the 140 square point inserted as 140pt in the calculation by
the \height should produce about \width, analogously in the height
parameter, indeed the second Blah is about the same as the first one.
The third Blah is produced with 600 square pt.

I don't know, but \width and \height might be available to
\includegraphics as well, so the same trick might be available there.
Otherwise try \resizebox*{...}{...}{\includegraphics[...]{...}}.
Post by V***@googlemail.com
Currently I adjust the size manually for every image
so that take approximately the same area in the document. Would be
great if this could be automated.
Independent from TeX, there is quite a quality problem with scaling
bitmap graphics in an uncontrolled or automatic manner like this or
that above and actual printing on paper. But also for E-Books, because
there you usually want small files, i.e. no waste because of too high
resolution of the bitmaps. You can reach optimal quality for either
purpose only, if you include bitmap graphics without scaling. I.e.
prepare them accordingly before including them. I'm feeling a little
bit like you saying you need a round to shoot your right big toe, and
me grinding you a very rough one by hand.

Bernd Strieder
V***@googlemail.com
2007-02-16 11:22:46 UTC
Permalink
I've been curious about this, so I tried. There is my prototype...
Thanks a lot!
t***@googlemail.com
2007-02-16 15:01:17 UTC
Permalink
Post by V***@googlemail.com
smaller than others. It would be nice to specify the area of the
images instead. Currently I adjust the size manually for every image
so that take approximately the same area in the document. Would be
great if this could be automated.
FWIW, XeTeX has support for this built in. When you include an image
you can specify the desired dimensions.

Tim
Continue reading on narkive:
Loading...