summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/scalerel/scalerel.sty
blob: 5c25e8fa0f1091e14033399fb50bdd21225b0619 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
\ProvidesPackage{scalerel}
[2016/12/29 v1.8
 Routines for constrained scaling and stretching of objects,
 relative to a reference object or in absolute terms]
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Steven B. Segletes.

% V1.01-Correct typos in documentation
% V1.1 -Corrected usepackage dependencies; Significant doc rewrite.
% V1.2 -Added \scaleleftright and \stretchleftright; revised docs.
% V1.3 -Corrected missing % in stretch routines, added \hstretch and
%       \vstretch commands; fixed version number in .sty file
% V1.4 -Auto-detect mathmode and style; use the proper math display
%       style; Allow \ignoremathstyle and \discernmathstyle to
%       revert to former approach in rare cases and return to new
%       approach.
%      -Added \Isnextbyte as a supplemental routine
%      -Added \scaleobj (like \scalebox, but defaults to math mode
%       using current display style).
% V1.5 -Eliminated mathstyle package because of incompatibilites with
%       many other packages.  Implemented \mathchoice approach instead.
% V1.6 -Made available \LMex and \LMpt lengths (Local Math ex and
%       Local Math pt), usable inside \ThisStyle arguments (1ex/pt in 
%       \textstyle and \displaystyle, 0.7ex/pt in \scriptstyle, and
%       0.5ex/pt in \scriptscriptstyle).
%      -Revised/shortened/improved \Isnextbyte.
%      -Replaced ifthen package calls with etoolbox calls.
% V1.7 -Note that the V1.5 implementation with \mathchoice seemed to
%       have made the use of \ignoremathstyle and \discernmathstyle 
%       vestigial.  Thus, \ignoremathstyle was revised to be used
%       for the purpose of streamlining package efficiency when 
%       only textstyle (or displaystyle with \ignoremathstyle[D])
%       processing is required.
%      -Fixed use of dot {.} as null argument in \scaleleftright{}{}{} 
%       and \stretchleftright{}{}{}, which had been causing overflow.
%      -Made \hstretch and \vstretch routines more efficient, using
%       optional arguments of \scalebox, instead of \scalerel.
% V1.8 -Fixed divide by 0 bugs that would arise when object to be
%       scaled fell completely below the baseline (zero height).
%       Required revisions to \scalerelplain, \stretchrelplain,
%       \scaleto, and \stretchto.
%      -Fixed aspect-ratio bug, that calculated aspect for stretched object
%       based only upon portion above baseline

\usepackage{calc}
\usepackage{graphicx}
\usepackage{etoolbox}
\global\newlength\thesrwidth
\global\newlength\thesrheight
\global\newlength\srblobheight
\global\newlength\srblobdepth
\global\newlength\mnxsrwidth
\newlength\LMex
\newlength\LMpt
\def\scriptstyleScaleFactor{.7}
\def\scriptscriptstyleScaleFactor{.5}

\newcommand\scalerel{\@ifstar{\scalerelplain}{\scalerelplus}}

\newcommand\scalerelplain[3][99in]{\ThisStyle{%
  \setbox0\hbox{\@obj{#3}}%
  \setlength\srblobheight{\ht0+\dp0}%
  \setlength\srblobdepth{\dp0}%
  \setbox2\hbox{\@obj{#2}}%
  \setbox0\hbox{\raisebox{\dp2}{\@obj{#2}}}%
  \setlength\thesrwidth{\wd0*\ratio{\srblobheight}{\ht0}}%
  \setlength\thesrheight{\ht0*\ratio{\srblobheight}{\ht0}}%
  \setlength\mnxsrwidth{#1}%
  \ifdim\thesrwidth>\mnxsrwidth\setlength\thesrwidth{\mnxsrwidth}\fi%
  \raisebox{-\srblobdepth}{\resizebox{\thesrwidth}{\thesrheight}{\copy0}}%
  }%
}

\newcommand\scalerelplus[3][99in]{\scalerelplain[#1]{#2}{#3}#3}

\newcommand\stretchrel{\@ifstar{\stretchrelplain}{\stretchrelplus}}

\newcommand\stretchrelplain[3][10000]{\ThisStyle{%
  \setbox0\hbox{\@obj{#3}}%
  \setlength\srblobheight{\ht0+\dp0}%
  \setlength\srblobdepth{\dp0}%
  \setbox2\hbox{\@obj{#2}}%
  \setbox0\hbox{\raisebox{\dp2}{\@obj{#2}}}%
  \setlength\thesrwidth{\wd0}%
  \setlength\thesrheight{\ht0*\ratio{\srblobheight}{\ht0}}%
  \setlength\mnxsrwidth{\thesrheight*100/#1}%
  \ifdim\thesrwidth<\mnxsrwidth\setlength\thesrwidth{\mnxsrwidth}\fi%
  \raisebox{-\srblobdepth}{\resizebox{\thesrwidth}{\thesrheight}{\copy0}}%
  }%
}

\newcommand\stretchrelplus[3][10000]{\stretchrelplain[#1]{#2}{#3}#3}

\newcommand\scaleto[3][99in]{\ThisStyle{%
  \setlength\srblobheight{#3}%
  \setbox2\hbox{\@obj{#2}}%
  \setbox0\hbox{\raisebox{\dp2}{\@obj{#2}}}%
  \setlength\thesrwidth{\wd0*\ratio{\srblobheight}{\ht0}}%
  \setlength\thesrheight{\ht0*\ratio{\srblobheight}{\ht0}}%
  \setlength\mnxsrwidth{#1}%
  \ifdim\thesrwidth>\mnxsrwidth\setlength\thesrwidth{\mnxsrwidth}\fi%
  \setlength\srblobdepth{\dp2*\ratio{\srblobheight}{\ht0}}%
  \raisebox{-\srblobdepth}{\resizebox{\thesrwidth}{\thesrheight}{\copy0}}%
  }%
}

\newcommand\stretchto[3][10000]{\ThisStyle{%
  \setlength\srblobheight{#3}%
  \setbox2\hbox{\@obj{#2}}%
  \setbox0\hbox{\raisebox{\dp2}{\@obj{#2}}}%
  \setlength\thesrwidth{\wd0}%
  \setlength\thesrheight{\ht0*\ratio{\srblobheight}{\ht0}}%
  \setlength\mnxsrwidth{\thesrheight*100/#1}%
  \ifdim\thesrwidth<\mnxsrwidth\setlength\thesrwidth{\mnxsrwidth}\fi%
  \setlength\srblobdepth{\dp2*\ratio{\srblobheight}{\ht0}}%
  \raisebox{-\srblobdepth}{\resizebox{\thesrwidth}{\thesrheight}{\copy0}}%
  }%
}

\newcommand\scaleleftright[4][99in]{%
  \ifx.#2#3\else\scalerel[#1]{#2}{#3}\fi%
  \ifx.#4\else\scalerel*[#1]{#4}{#3}\fi%
}
\newcommand\stretchleftright[4][10000]{%
  \ifx.#2#3\else\stretchrel[#1]{#2}{#3}\fi%
  \ifx.#4\else\stretchrel*[#1]{#4}{#3}\fi%
}

\newcommand\hstretch[2]{%
  \ThisStyle{\scalebox{#1}[1]{\@obj{#2}}}}

\newcommand\vstretch[2]{%
  \ThisStyle{\scalebox{1}[#1]{\@obj{#2}}}}

\newcommand\scaleobj[2]{%
  \ThisStyle{\scalebox{#1}{\@obj{#2}}}}

% DETERMINE IF THE SCALEREL ROUTINE WAS CALLED IN MATH MODE.
% IF SO, USE THE PROPER MATH DISPLAY STYLE FOR THE CALL,
% *UNLESS* THE FIRST CHARACTER OF THE ARGUMENT IS A $ SIGN,
% SIGNIFYING A DESIRED ESCAPE FROM MATH MODE FOR THE ARGUMENT.
\newcommand\@obj[1]{%
  \if T\@mmode%
    \Isnextbyte[q]{$}{#1}%
    \if F\theresult%
      $\SavedStyle#1$%
    \else%
      $#1$%
    \fi%
  \else%
    $#1$%
  \fi%
}

\def\@mstyleD{\displaystyle}
\def\@mstyleT{\textstyle}
\def\@mstyleS{\scriptstyle}
\def\@mstyles{\scriptscriptstyle}
%
\def\SavedStyle{\csname @mstyle\m@switch\endcsname}
%
\newcommand\ThisStyle[1]{%
  \ifmmode%
    \def\@mmode{T}\mathchoice%
      {\edef\m@switch{D}\LMex=1ex\relax\LMpt=1pt\relax#1}%
      {\edef\m@switch{T}\LMex=1ex\relax\LMpt=1pt\relax#1}%
      {\edef\m@switch{S}\LMex=\scriptstyleScaleFactor ex\relax%
                        \LMpt=\scriptstyleScaleFactor pt\relax#1}%
      {\edef\m@switch{s}\LMex=\scriptscriptstyleScaleFactor ex\relax%
                        \LMpt=\scriptscriptstyleScaleFactor pt\relax#1}%
  \else%
    \def\@mmode{F}%
    \edef\m@switch{T}\LMex=1ex\relax\LMpt=1pt\relax#1%
  \fi%
}
%

\let\sv@ThisStyle\ThisStyle

\newcommand\discernmathstyle{\let\ThisStyle\sv@ThisStyle}

% \ignoremathstyle WOULD BE USED TO SPEED UP DEEPLY NESTED
% ROUTINES OF scalerel PACKAGE, WHEN LIMITED TO textstyle MATH
\newcommand\ignoremathstyle[1][T]{%
  \renewcommand\ThisStyle[1]{%
    \edef\m@switch{#1}\LMex=1ex\relax\LMpt=1pt\relax%
    \ifmmode\def\@mmode{T}\else\def\@mmode{F}\fi##1}%
}

% THIS CAN REPLACE \isnextbyte IN STRINGSTRINGS PACKAGE
% BECAUSE IT WORKS ON CHARS THAT CAN'T BE \edef'ED (\dag, ETC.)
\newcommand\Isnextbyte[3][v]{%
  \def\SR@Letter@A{#2}%
  \SR@nextbyte{\SR@Letter@B}#3\relax\relax\relax%
  \ifdefstrequal{\SR@Letter@A}{\SR@Letter@B}%
    {\edef\theresult{T}}{\edef\theresult{F}}%
  \if q#1\else\theresult\fi%
}
\def\SR@nextbyte#1#2#3\relax{\def#1{#2}}

\endinput