-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticrow.sty
71 lines (64 loc) · 2.65 KB
/
elasticrow.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
% Author: Herbert Kruitbosch, Michal Kaut; Copyright: be nice.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{elasticrow}[2022/02/13 Fit figures of different height to a row]
\RequirePackage{graphicx}
\RequirePackage{subcaption}
\RequirePackage{keyval}
\RequirePackage{etoolbox}
%% main control parameters, can be changed by user
% TODO: add package options
% width of the row
\newlength{\elasticrowwidth}\setlength{\elasticrowwidth}{\textwidth}
% separation between figures
\newlength{\elasticrowsep}\setlength{\elasticrowsep}{0pt}
%% internal definitions
\newlength{\elasticrow@height}
\newlength{\elasticrow@width}
\newlength{\elasticrow@sep}
\newsavebox{\elasticrow@box}
\newtoks{\elasticbox@argtoks}
\newif\ifelasticrow@fullwidth
%% command for adding figures to the row
% syntax: \elasticfigure[<caption>]{<figure_file>}
\newcommand{\elasticfigure}[2][\empty]{%
\elasticbox@argtoks=\expandafter{\the\elasticbox@argtoks\elasticfun[#1]{#2}}%
}
%% keys for the elasticrow environment
\define@key{elasticrow@key}{width}{\setlength{\elasticrow@width}{#1}}
\define@key{elasticrow@key}{sep}{\def\elasticrow@whitespace{#1}}
%% environment for the elastic row
% syntax: \begin{elasticrow}[<options>],
% where <options> use a key-value syntax with keys 'width' and 'sep'
\newenvironment{elasticrow}[1][]{%
\setlength{\elasticrow@width}{\elasticrowwidth}%
\def\elasticrow@whitespace{\elasticrowsep}%
\setkeys{elasticrow@key}{#1}%
\ifdimequal{\elasticrow@width}{\textwidth}{\elasticrow@fullwidthtrue}{\elasticrow@fullwidthfalse}%
\elasticbox@argtoks={}%
}{%
\addtolength{\elasticrow@width}{\elasticrow@whitespace}%
\let\elasticfun=\elasticcalculatewidth%
\the\elasticbox@argtoks%
\let\elasticfun=\elasticpreprocess%
\sbox\elasticrow@box{\resizebox{\elasticrow@width}{!}{\the\elasticbox@argtoks}}%
% using 99.9% of the width, to prevent line break with width=\textwidth and sep=0pt
% TODO: why is it needed?
\setlength{\elasticrow@height}{0.999\ht\elasticrow@box}%
\let\elasticfun=\elasticprocess%
\def\elasticrow@sep{0pt}%
\the\elasticbox@argtoks%
}
\newcommand{\elasticcalculatewidth}[2][\empty]{\addtolength{\elasticrow@width}{-\elasticrow@whitespace}}
\newcommand{\elasticpreprocess}[2][\empty]{\includegraphics[height=1cm]{#2}}
\newcommand{\elasticprocess}[2][\empty]{%
\hspace{\elasticrow@sep}%
\ifx#1\empty%
\includegraphics[height=\elasticrow@height]{#2}%
\else%
\subcaptionbox{#1}{\includegraphics[height=\elasticrow@height]{#2}}%
\fi%
% set \elasticrow@sep for the next figures
% - uses \fill for full-width rows, to prevent line break due to minor width overflow
\ifelasticrow@fullwidth\let\elasticrow@sep=\fill\else\let\elasticrow@sep=\elasticrow@whitespace\fi%
}
\endinput