Debian Codegraph @

texmf-dist/source/latex/fitbox/fitbox.dtx

raw
1 % \iffalse
2 %
3 %% Copyright 2015-2019, Boris Veytsman <[email protected]
4 %% This work may be distributed and/or modified under the
5 %% conditions of the LaTeX Project Public License, either
6 %% version 1.3 of this license or (at your option) any
7 %% later version.
8 %% The latest version of the license is in
9 %% http://www.latex-project.org/lppl.txt
10 %% and version 1.3 or later is part of all distributions of
11 %% LaTeX version 2003/06/01 or later.
12 %%
13 %% This work has the LPPL maintenance status `maintained'.
14 %%
15 %% The Current Maintainer of this work is Boris Veytsman
16 %%
17 % \begin{macrocode}
18 %<style>\NeedsTeXFormat{LaTeX2e}
19 %<*gobble>
20 \ProvidesFile{fitbox.dtx}
21 %</gobble>
22 %<style>\ProvidesPackage{fitbox}
23 %<*style>
24 [2019/02/20 v1.02 Fitting boxes on a page]
25 % \end{macrocode}
26 %</style>
27 %<*gobble>
28 % \fi
29 %
30 %
31 %% \CharacterTable
32 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
33 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
34 %% Digits \0\1\2\3\4\5\6\7\8\9
35 %% Exclamation \! Double quote \" Hash (number) \#
36 %% Dollar \$ Percent \% Ampersand \&
37 %% Acute accent \' Left paren \( Right paren \)
38 %% Asterisk \* Plus \+ Comma \,
39 %% Minus \- Point \. Solidus \/
40 %% Colon \: Semicolon \; Less than \<
41 %% Equals \= Greater than \> Question mark \?
42 %% Commercial at \@ Left bracket \[ Backslash \\
43 %% Right bracket \] Circumflex \^ Underscore \_
44 %% Grave accent \` Left brace \{ Vertical bar \|
45 %% Right brace \} Tilde \~}
46 %
47 %\iffalse
48 % \begin{macrocode}
49 \documentclass{ltxdoc}
50 \usepackage{hypdoc}
51 \PageIndex
52 \CodelineIndex
53 \RecordChanges
54 \EnableCrossrefs
55 \begin{document}
56 \DocInput{fitbox.dtx}
57 \end{document}
58 % \end{macrocode}
59 %</gobble>
60 % \fi
61 % \MakeShortVerb{|}
62 % \GetFileInfo{fitbox.dtx}
63 %
64 %
65 % \title{Fit graphics on a page\thanks{\copyright 2015--2019 Boris Veytsman}
66 % \thanks{This package was commissined by Neadwerx, \url{http://www.neadwerx.com/}}}
67 % \author{Boris Veytsman \thanks{[email protected]}}
68 % \date{\filedate, \fileversion}
69 % \maketitle
70 %
71 % \begin{abstract}
72 % The \textsl{fitbox} package allows a box (usually an
73 % |\includegraphics| box) to fit on the page. It scales the box
74 % to the maximal allowed size within the user-set limits. If there
75 % there is not enough space on the page, the box is moved to the
76 % next one.
77 % \end{abstract}
78 %
79 % \tableofcontents
80 %
81 % \clearpage
82 %
83 %\section{Introduction}
84 %\label{sec:intro}
85 %
86 % How often one puts a picture on a page only to see that \LaTeX\
87 % decides to move it to the next one because there is not enough
88 % space---while shaving a millimeter off the height would make the
89 % difference? This package is intended to alleviate this difference.
90 % It uses several strategies to fit a picture on the page, and only if
91 % they fail, the picture is moved to the next one.
92 %
93 %\section{User Guide}
94 %\label{sec:ug}
95 %
96 %
97 %\subsection{Installation}
98 %\label{sec:ug_install}
99 %
100 % The installation of the class follows the usual
101 % practice~\cite{TeXFAQ} for \LaTeX{} packages:
102 % \begin{enumerate}
103 % \item Run \textsf{latex} on |fitbox.ins|. This will produce the file
104 % |fitbox.sty|.
105 % \item Put the file |fitbox.sty| to
106 % the place where \LaTeX{} can find it (see
107 % \cite{TeXFAQ} or the documentation for your \TeX{}
108 % system).\label{item:install}
109 % \item Update the database of file names. Again, see \cite{TeXFAQ}
110 % or the documentation for your \TeX{} system for the system-specific
111 % details.\label{item:update}
112 % \item The file |fitbox.pdf| provides the documentation for the
113 % package
114 % \end{enumerate}
115 % As an alternative to items~\ref{item:install} and~\ref{item:update}
116 % you can just put the files in the working directory where your
117 % |.tex| file is.
118 %
119 %
120 %
121 %\subsection{Usage}
122 %\label{sec:usage}
123 %
124 % To use the package, add to the preamble of your document
125 % \begin{verbatim}
126 % \usepackage{fitbox}
127 % \end{verbatim}
128 %
129 % \DescribeMacro{\fitbox}%
130 % The main command of the package is
131 % \cs{fitbox}\oarg{options}\marg{stuff}. The \marg{stuff} will be
132 % typeset in a box according to the \oarg{options}. In most cases
133 % \marg{stuff} is an \cs{includegraphics} command, but anything that
134 % fits into an LR-box can be typeset in this way.
135 %
136 % The \marg{stuff} is typeset in a box, and then the box is put on the
137 % page according to the following algorithm:
138 % \begin{enumerate}
139 % \item \TeX\ starts a new paragraph.
140 % \item The box is scaled up to the maximal dimensions specified by
141 % the user (while keeping the aspect ratio).
142 % \item If there is not enough space on the page to fit the box, the
143 % latter is scaled down as neccessary, but no smaller than the
144 % minimal dimensions specified by the user.
145 % \item If there is still not enough space, \TeX\ tries to enlarge the
146 % page up to the specified limit.
147 % \item If this also fails, \TeX\ starts a new page and fits the box
148 % there.
149 % \end{enumerate}
150 %
151 % \DescribeMacro{\fitbox*}%
152 % The starred version \cs{fitbox*} is intended to be used inside
153 % floats, where page length is not well defined. This command uses
154 % only the first two steps of the alogithm above, scaling the box up
155 % to the maximal dimensions provided by the user.
156 %
157 % \DescribeMacro{\fitboxset}%
158 % The options can be set individually for each \cs{fitbox} command, or
159 % globally using the command \cs{fitboxset}, for example,
160 % \begin{verbatim}
161 % \fitboxset{maxwidth=\textwidth, minwidth=\fitboxnatwidth}
162 % \end{verbatim}
163 %
164 %
165 % \DescribeMacro{\fitboxnatwidth}%
166 % \DescribeMacro{\fitboxnatheight}%
167 % The options of the package use key-value interface. Often
168 % the values are dimensions; in these cases the special dimensions
169 % \cs{fitboxnatwidth} and \cs{fitboxnatheight} can be used; they are
170 % equal to natural dimensions of the box. Note that |height| and
171 % \cs{fitboxnatheight} are actually \emph{total heights}, including
172 % both height and depth of the corresponding boxes. For example,
173 % \begin{verbatim}
174 % \fitboxset{minheight=0.5\fitboxnatheight}
175 % \end{verbatim}
176 % means that the box cannot be scaled down more than 50\%.
177 %
178 % The following options are recognized by the \cs{fitbox} command:
179 % \begin{description}
180 % \item[maxheight:] The maximal total height of the box. By default
181 % \cs{textheight}.
182 % \item[maxwidth:] The maximal width of the box. By default
183 % the size of the current box to be constructed.
184 % \item[minheight:] The minimal height of the box. By default
185 % \cs{fitboxnatheight}.
186 % \item[minwidth:] The minimal width of the box. By default
187 % \cs{fitboxnatwidth}.
188 % \item[belowboxspace:] The height of the space that must be left
189 % below the box (e.g. for a caption). By default zero.
190 % \item[maxenlargepage:] The maximal amount to add to the current
191 % page. By default zero.
192 % \end{description}
193 %
194 % \DescribeMacro{\SetFitboxLayout}%
195 % Since version 1.02 the package added the functionality of
196 % \textsl{FigSize} package~\cite{FigSize}. The latter has a handy
197 % command \cs{SetFigLayout}, which can be used to scale all figures on
198 % a page. This package uses the command \cs{SetFitboxLayout} that
199 % somewhat mimics \cs{SetFigLayout}. The macro has the arguments
200 % \cs{SetFitboxLayout}\oarg{keys}\marg{rows}\marg{columns}, and helps
201 % to scale the boxes into a grid. However, unlike \textsl{FigSize}
202 % package, we do not redefine \cs{includegraphics} command: you should
203 % use \cs{fitbox} for your graphics. The command \cs{SetFitboxLayout}
204 % is just \cs{fitboxset} with special values of the parameters. These
205 % commands can be mixed, with the latest command overriding the
206 % previous ones.
207 %
208 % The following keys are recognized by the \cs{SetFitboxLayout} command:
209 % \begin{description}
210 % \item[colsep:] the distance between the columns; by default the size
211 % of \cs{quad}.
212 % \item[colsepexpr:] if set, the width of the expression becomes
213 % \texttt{colsep}. For example, \path{colsepexpr=\quad} makes
214 % separation between the columns the size of \cs{quad}.
215 % \item[maincapheight:] The height of the main caption. By default,
216 % $\cs{abovecaptionskip}+\cs{belowcaptionskip}+
217 % n\cs{baselineskip})$
218 % where $n$ is the expected number of lines in the main caption (by
219 % default 1).
220 % \item[maincaplines:] The number of lines in the main caption. By
221 % default 1.
222 % \item[subcapheight:] The height of the subcaption. By default,
223 % $\cs{abovecaptionskip}+\cs{belowcaptionskip}+m\cs{baselineskip}$
224 % where $m$ is the expected number of lines in the subcaptions (by
225 % default 0).
226 % \item[subcaplines:] The number of lines in the subcaptions. By
227 % default 0.
228 % \end{description}
229 %
230 %
231 %\StopEventually{\clearpage
232 % \bibliography{fitbox}
233 % \bibliographystyle{unsrt}}
234 %
235 % \clearpage
236 %
237 % \section{Implementation}
238 % \label{sec:implementation}
239 %
240 % \begin{macrocode}
241 %<*style>
242 % \end{macrocode}
243 %
244 %
245 %\subsection{Setting up parameters}
246 %\label{sec:params}
247 %
248 %
249 %
250 %
251 % \begin{macro}{\fitboxnatheight}
252 % The total height of the box
253 % \begin{macrocode}
254 \newdimen\fitboxnatheight
255 % \end{macrocode}
256 %
257 % \end{macro}
258 %
259 % \begin{macro}{\fitboxnatwidth}
260 % The total width of the box
261 % \begin{macrocode}
262 \newdimen\fitboxnatwidth
263 % \end{macrocode}
264 %
265 % \end{macro}
266 %
267 %
268 % \changes{v1.01}{2019/02/20}{New keys for \cs{SetFitboxLayout}}
269 % We use |xkeyval| interface:
270 % \begin{macrocode}
271 \RequirePackage{xkeyval}
272 \define@cmdkeys{FTBX}{maxheight, minheight, maxwidth, minwidth,
273 belowboxspace, maxenlargepage, colsep, maincapheight,
274 subcapheight}
275 % \end{macrocode}
276 %
277 % Setting some keys changes other keys
278 % \begin{macrocode}
279 \define@key{FTBX}{colsepexpr}{%
280 \setbox\@tempboxa=\hbox{#1}%
281 \edef\cmdKV@FTBX@colsep{\wd\@tempboxa}}
282 \define@key{FTBX}{maincaplines}{%
283 \@tempdima=\z@
284 \advance\@tempdima by #1\baselineskip\relax
285 \ifnum#1>0\relax
286 \advance\@tempdima by \parskip
287 \fi
288 \advance\@tempdima by \abovecaptionskip
289 \advance\@tempdima by \belowcaptionskip
290 \edef\cmdKV@FTBX@maincapheight{\the\@tempdima}}
291 \define@key{FTBX}{subcaplines}{%
292 \@tempdima=\z@
293 \advance\@tempdima by #1\baselineskip\relax
294 \ifnum#1>0\relax
295 \advance\@tempdima by \parskip
296 \fi
297 \advance\@tempdima by \abovecaptionskip
298 \advance\@tempdima by \belowcaptionskip
299 \edef\cmdKV@FTBX@subcapheight{\the\@tempdima}}
300 % \end{macrocode}
301 %
302 %
303 % \begin{macro}{\fitboxset}
304 % Setting everything
305 % \begin{macrocode}
306 \def\fitboxset#1{\setkeys{FTBX}{#1}}
307 % \end{macrocode}
308 %
309 % \end{macro}
310 %
311 %\changes{v1.01}{2019/02/10}{Changed maximal width}
312 % The defaults
313 % \begin{macrocode}
314 \fitboxset{maxheight=\textheight, minheight=\fitboxnatheight,
315 maxwidth=\hsize, minwidth=\fitboxnatwidth,
316 belowboxspace=0pt, maxenlargepage=0pt, colsepexpr={aaa},
317 maincaplines=1, subcaplines=0}
318 % \end{macrocode}
319 %
320 %
321 %\subsection{Main command}
322 %\label{sec:main_command}
323 %
324 % \begin{macro}{\FTBX@box}
325 % The box which will held the stuff to be typeset
326 % \begin{macrocode}
327 \newbox\FTBX@box
328 % \end{macrocode}
329 %
330 % \end{macro}
331 %
332 % \begin{macro}{\FTBX@desired@maxheight}
333 % The desired maximal height
334 % \begin{macrocode}
335 \newdimen\FTBX@desired@maxheight
336 % \end{macrocode}
337 % \end{macro}
338 %
339 %
340 % \begin{macro}{\FTBX@desired@minheight}
341 % The desired minimal height
342 % \begin{macrocode}
343 \newdimen\FTBX@desired@minheight
344 % \end{macrocode}
345 % \end{macro}
346 %
347 % \begin{macro}{\FTBX@available@height}
348 % The desired available height
349 % \begin{macrocode}
350 \newdimen\FTBX@available@height
351 % \end{macrocode}
352 % \end{macro}
353 %
354 % \begin{macro}{\fitbox}
355 % \changes{v1.02}{2019/02/18}{Split into normal and starred version}
356 % The main command
357 % \begin{macrocode}
358 \def\fitbox{\@ifstar\@@fitbox\@fitbox}
359 % \end{macrocode}
360 %
361 % \end{macro}
362 %
363 % \begin{macro}{\@fitbox}
364 % \changes{v1.01}{2019/02/10}{Do not force width adjustment}
365 % \changes{v1.02}{2019/02/17}{\cs{noident} instead of \cs{leavevmode}
366 % (suggested by Frank Mittelbach)}
367 % \changes{v1.02}{2019/02/18}{Split into normal and starred version}
368 % The main command---``normal'' version
369 % \begin{macrocode}
370 \newcommand\@fitbox[2][]{\noindent
371 \fitboxset{#1}%
372 \setbox\FTBX@box=\hbox{#2}%
373 \fitboxnatwidth=\wd\FTBX@box\relax
374 \fitboxnatheight=\ht\FTBX@box\relax
375 \advance\fitboxnatheight by \dp\FTBX@box\relax
376 % Checking the sizes
377 \expandafter\ifdim\cmdKV@FTBX@minwidth>\columnwidth\relax
378 \PackageWarning{fitbox}{Minimal width is too large. Consider
379 changing it to \the\hsize}%
380 \fi
381 \expandafter\ifdim\cmdKV@FTBX@maxwidth>\hsize\relax
382 \PackageWarning{fitbox}{Desired width is too large. Consider
383 changing it to \the\hsize}%
384 \fi
385 \expandafter\ifdim\cmdKV@FTBX@minheight>\textheight\relax
386 \PackageWarning{fitbox}{Minimal height is too large.
387 Consider changing it to \the\textheight}%
388 \fi
389 \expandafter\ifdim\cmdKV@FTBX@maxheight>\textheight\relax
390 \PackageWarning{fitbox}{Desired height is too large.
391 Consider changing it to \the\textheight}%
392 \fi
393 % Calculating the minimal and maximal height
394 \Gscale@div{\@FTBX@tempa}{\cmdKV@FTBX@maxwidth}{\fitboxnatwidth}%
395 \FTBX@desired@maxheight=\@FTBX@tempa\fitboxnatheight\relax
396 \expandafter\ifdim\cmdKV@FTBX@maxheight<\FTBX@desired@maxheight\relax
397 \expandafter\FTBX@desired@maxheight=\cmdKV@FTBX@maxheight\relax
398 \fi
399 \Gscale@div{\@FTBX@tempa}{\cmdKV@FTBX@minwidth}{\fitboxnatwidth}%
400 \FTBX@desired@minheight=\@FTBX@tempa\fitboxnatheight\relax
401 \expandafter\ifdim\cmdKV@FTBX@minheight>\FTBX@desired@minheight\relax
402 \expandafter\FTBX@desired@minheight=\cmdKV@FTBX@minheight\relax
403 \fi
404 \ifdim\FTBX@desired@minheight>\FTBX@desired@maxheight\relax
405 \PackageWarning{fitbox}{Desired min scale exceeds desired max
406 scale.}%
407 \fi
408 \FTBX@available@height=\pagegoal\relax
409 \ifdim\FTBX@available@height>\vsize\relax
410 \FTBX@available@height=\vsize
411 \fi
412 \advance\FTBX@available@height by -\pagetotal\relax
413 \advance\FTBX@available@height by -\cmdKV@FTBX@belowboxspace\relax
414 \advance\FTBX@available@height by -\baselineskip\relax
415 \ifdim\FTBX@desired@maxheight>\FTBX@available@height\relax
416 \ifdim\FTBX@available@height<\FTBX@desired@minheight\relax
417 \@tempdima=\FTBX@desired@minheight\relax
418 \advance\@tempdima by
419 -\FTBX@available@height\relax
420 \expandafter\ifdim\cmdKV@FTBX@maxenlargepage<\@tempdima\relax
421 \newpage
422 \resizebox*{!}{\FTBX@desired@maxheight}{\box\FTBX@box}%
423 \else
424 \enlargethispage{\@tempdima}%
425 \resizebox*{!}{\FTBX@desired@minheight}{\box\FTBX@box}%
426 \fi
427 \else
428 \resizebox*{!}{\FTBX@available@height}{\box\FTBX@box}%
429 \fi
430 \else
431 \resizebox*{!}{\FTBX@desired@maxheight}{\box\FTBX@box}%
432 \fi
433 }
434 % \end{macrocode}
435 %
436 % \end{macro}
437 %
438 % \begin{macro}{\@@fitbox}
439 % \changes{v1.01}{2019/02/10}{Do not force width adjustment}
440 % \changes{v1.02}{2019/02/17}{\cs{noident} instead of \cs{leavevmode}
441 % (suggested by Frank Mittelbach)}
442 % \changes{v1.02}{2019/02/18}{Split into normal and starred version}
443 % The main command---``starred'' version. Simplified computations.
444 % \begin{macrocode}
445 \newcommand\@@fitbox[2][]{\noindent
446 \fitboxset{#1}%
447 \setbox\FTBX@box=\hbox{#2}%
448 \fitboxnatwidth=\wd\FTBX@box\relax
449 \fitboxnatheight=\ht\FTBX@box\relax
450 \advance\fitboxnatheight by \dp\FTBX@box\relax
451 % Checking the sizes
452 \expandafter\ifdim\cmdKV@FTBX@minwidth>\columnwidth\relax
453 \PackageWarning{fitbox}{Minimal width is too large. Consider
454 changing it to \the\hsize}%
455 \fi
456 \expandafter\ifdim\cmdKV@FTBX@maxwidth>\hsize\relax
457 \PackageWarning{fitbox}{Desired width is too large. Consider
458 changing it to \the\hsize}%
459 \fi
460 \expandafter\ifdim\cmdKV@FTBX@minheight>\textheight\relax
461 \PackageWarning{fitbox}{Minimal height is too large.
462 Consider changing it to \the\textheight}%
463 \fi
464 \expandafter\ifdim\cmdKV@FTBX@maxheight>\textheight\relax
465 \PackageWarning{fitbox}{Desired height is too large.
466 Consider changing it to \the\textheight}%
467 \fi
468 % Calculating the minimal and maximal height
469 \Gscale@div{\@FTBX@tempa}{\cmdKV@FTBX@maxwidth}{\fitboxnatwidth}%
470 \FTBX@desired@maxheight=\@FTBX@tempa\fitboxnatheight\relax
471 \expandafter\ifdim\cmdKV@FTBX@maxheight<\FTBX@desired@maxheight\relax
472 \expandafter\FTBX@desired@maxheight=\cmdKV@FTBX@maxheight\relax
473 \fi
474 \Gscale@div{\@FTBX@tempa}{\cmdKV@FTBX@minwidth}{\fitboxnatwidth}%
475 \FTBX@desired@minheight=\@FTBX@tempa\fitboxnatheight\relax
476 \expandafter\ifdim\cmdKV@FTBX@minheight>\FTBX@desired@minheight\relax
477 \expandafter\FTBX@desired@minheight=\cmdKV@FTBX@minheight\relax
478 \fi
479 \ifdim\FTBX@desired@minheight>\FTBX@desired@maxheight\relax
480 \PackageWarning{fitbox}{Desired min scale exceeds desired max
481 scale.}%
482 \fi
483 \resizebox*{!}{\FTBX@desired@maxheight}{\box\FTBX@box}%
484 }
485 % \end{macrocode}
486 %
487 % \end{macro}
488 %
489 %
490 %\subsection{Multi-figure layout}
491 %\label{sec:layout}
492 %
493 %
494 %
495 % \begin{macro}{\SetFitboxLayout}
496 % \changes{v1.02}{2019/02/17}{New macro}
497 % \changes{v1.02}{2019/02/20}{Key-value interface}
498 % A macro styles after \cs{SetFigsizeLayout} of \textsl{FigSize}
499 % package.
500 % \begin{macrocode}
501 \newcommand\SetFitboxLayout[3][]{%
502 \fitboxset{#1}%
503 \@tempdima=\textheight
504 \advance\@tempdima by -\cmdKV@FTBX@maincapheight\relax
505 \divide\@tempdima by #2\relax
506 \advance\@tempdima by -\cmdKV@FTBX@subcapheight\relax
507 \edef\cmdKV@FTBX@maxheight{\the\@tempdima}%
508 \@tempdima=\columnwidth
509 \@tempdimb=\cmdKV@FTBX@colsep\relax
510 \advance\@tempdima by -#3\@tempdimb\relax
511 \advance\@tempdima by \@tempdimb\relax
512 \divide\@tempdima by #3\relax
513 \edef\cmdKV@FTBX@maxwidth{\the\@tempdima}%
514 }
515 % \end{macrocode}
516 %
517 % \end{macro}
518 %
519 % \begin{macrocode}
520 %</style>
521 % \end{macrocode}
522 %\Finale
523 %\clearpage
524 %
525 %\PrintChanges
526 %\clearpage
527 %\PrintIndex
528 %
529 \endinput