TeX - LaTeX Asked on December 29, 2021
In an A5-compilation of letters, I want to insert a facsimile of a handwritten letter (or page of a letter) at the start of each chapter. These images are of different size, some letters have been written on A4 pages, others on smaller sheets of paper, so it may happen that they won’t fit on an A5 page unless they are scaled down.
I’ve made this function in my local style file:
% Insert an image in the middle of a page
% Arguments: #1 (Optional): Description; #2: Path to image
newlength{imagewidth}
newsavebox{tmpimage}
newcommand{addimage}[2][]{%
vspace*{fill}%
begin{minipage}[c]{linewidth}%
savebox{tmpimage}{includegraphics{#2}}%
ifthenelse{dimtest{heightof{usebox{tmpimage}}}{>}{textheight}}
% If image height is greater than text height, set imagewidth to width of vertically scaled image
{%
savebox{tmpimage{includegraphics[height=.9textheight]{#2}}}%
setlength{imagewidth}{widthof{usebox{tmpimage}}}%
}
% Otherwise set imagewidth to a fraction of linewidth
{setlength{imagewidth}{.9linewidth}}%
% If an image has been scaled vertically, its width may still be greater than linewidth
ifthenelse{dimtest{imagewidth}{>}{linewidth}}
% Scale image again to make sure imagewidth really is smaller than linewidth
{setlength{imagewidth}{.9linewidth}}%
% Everything is OK, do nothing!
{}%
includegraphics[width=imagewidth]{#2}%
ifthenelse{equal{#1}{}}
{}
{{centeringpar%
rule[.5ex]{imagewidth}{.6pt}par%
noindentsmallhspace*{fill}#1hfill~%
}}%
end{minipage}%
vfill%
clearpage%
}
This works, but it is highly inefficient and processing a 200+ pages document with footnotes and cross-references (thus requiring up to 3 passes) can really take some time if there are several image files with a size of 5MB or even more.
The reason is that my function will process each image 2 (worst case: 3) times on each pass.
Basically, I define a savebox with the unscaled image. I then check if it would fit on the page vertically — if so, I set imagewidth
(a locally defined length) to .9linewidth
. If it doesn’t fit, I overwrite the savebox with a vertically scaled image and set imagewidth
to the width of the savebox.
I then check if the image would fit horizontally by comparing imagewidth
and linewidth
. If the image still is too wide, I set imagewidth
to .9linewidth
. Finally, I use the value of imagewidth
to output a scaled version on the page.
Do you have an idea how to optimize this? I want this to work generally, and for different image formats.
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP