summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/stackengine/stackengine.sty
blob: 50103f0e9b84adb29bae56e8efeeb80b3c2be8f7 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
\def\stackengineversionnumber{v3.1}
\ProvidesPackage{stackengine}
[2013/09/11 \stackengineversionnumber
 Stacking text and objects in convenient ways]
% 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.0  -Initial release.
% V2.0  -Revised syntax, making \Sstackgap=len, \Lstackgap=len syntax
%        obsolete.  New syntax is \setstackgap{S}{len} and
%        \setstackgap{L}{len}
%       -Condensed several routines
%       -Revised documentation for clarity and to eliminate typos.
%       -Added \toplap and \bottomlap.
%       -Disallowed \quietstack for lapping.
%       -added \abovebaseline
% V3.0  -Added \stackMath and \stackText macros to treat arguments,
%        by default, in math mode or not.
% V3.1  -Added \hsmash, for zero-width, center-aligned argument (note:
%        other alignments can be achieved with \llap and \rlap)
%       -Eliminated need for \usepackage{readarray} through the 
%        implementation of \@readMANYrows and \@processROW
%       -Added [usestackEOL] package option to use \\ separator, rather
%        than space separator in \Shortstack, \Longstack,
%        \Shortunderstack, and \Longunderstack
%       -Implemented \setstackEOL to define new separator in \Shortstack,
%        \Longstack, \Shortunderstack, and \Longunderstack
%       -Renamed some package internal variables with @...
%       -Added \addstackgap to place vertical gap above and below an 
%        existing stack.  Negative gaps have no effect.
%       -Added \stackinset, deprecates \topinset & \bottominset.
%       -allow c \stackalignment on \stackinset, \topinset, and 
%        \bottominset to have meaning (rightward offset relative to
%        centerline, can be positive or negative).
%       -Forced \useanchorwidth{T} for insets, since otherwise the
%        stackwidth could vary for reasons not obvious to user.
%       -Added \strutlongstacks{} mode for auto-adding a strut to
%        each row of a long stack
%       -Recommend \strutshortanchors{} mode in preference to
%        \def\usestackstrut{} definition (now deprecated).
\usepackage{calc}
\usepackage{ifthen}
\newcounter{@stackindex}
\newlength\@boxshift
\newlength\stack@tmplength
\newlength\temp@stkl
\global\newlength\@stackedboxwidth
\newsavebox\@addedbox
\newsavebox\@anchorbox
\newsavebox\stackedbox
\newcounter{ROWcellindex@}

%% Following 3 lines thanks to Prof. Enrico Gregorio, from:
%% http://tex.stackexchange.com/questions/42318/
%% removing-a-backslash-from-a-character-sequence
\begingroup\lccode`\|=`\\
\lowercase{\endgroup\def\removebs#1{\if#1|\else#1\fi}}
\newcommand{\stack@macro@name}[1]{\expandafter\removebs\string#1}

\newcommand\setstackEOL[1]{%
  \ifthenelse{\equal{#1}{}}{\def\SEP@char{ }}{\def\SEP@char{#1}}%
  \expandafter\define@processROW\expandafter{\SEP@char}%
}

\newcommand\define@processROW[1]{%
    \def\@processROW##1#1##2||{%
      \protected@edef\@preSEP{##1}%
      \protected@edef\@postSEP{##2}%
    }%
}

% FOR PROCESSING A GROUP OF STACK ROWS SEPARATED BY \SEP@char
\newcommand\@readMANYrows[1]{%
  \def\@doneROWreads{F}%
  \def\@postSEP{#1\SEP@char}%
  \setcounter{ROWcellindex@}{0}%
  \whiledo{\equal{\@doneROWreads}{F}}{%
    \stepcounter{ROWcellindex@}%
    \expandafter\@processROW\@postSEP||%
    \ifthenelse{\equal{\@postSEP}{}}{%
      \def\@doneROWreads{T}%
    }{}%
    \ifthenelse{\equal{\SEP@char}{ }}%
      {\def\@preSEPtemp{\@preSEP}}%
      {\def\@preSEPtemp{\ignorespaces\@preSEP\unskip}}%
    \expandafter\protected@edef\csname arg\roman{ROWcellindex@}\endcsname{%
      \@preSEPtemp}%
  }%
% \narg GIVES HOW MANY ROWS WERE PROCESSED
  \xdef\narg{\arabic{ROWcellindex@}}%
}

% PROCESS PACKAGE OPTIONS
\newif\ifstackengine@oldsyntax
\newif\ifstackengine@usestackEOL
\DeclareOption{oldsyntax}{\stackengine@oldsyntaxtrue}
\DeclareOption{usestackEOL}{\stackengine@usestackEOLtrue}
\ProcessOptions\relax
\ifstackengine@oldsyntax%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% THIS VERSION 1.0 SYNTAX WAS PHASED OUT %%%%%%%%%%%%%%%%%%%
%%%%% (but can invoke with \usepackage[oldsyntax]{stackengine} )
\newlength\Sstackgap\newlength\Lstackgap
\newcommand\stackgap{\if S\stacktype\the\Sstackgap\else\the\Lstackgap\fi}
\global\setlength{\Sstackgap}{3pt}
\global\setlength{\Lstackgap}{\baselineskip}
\else%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% AND REPLACED WITH THE FOLLOWING %%%%%%%%%%%%%%%%%%%%%%%%%%
% Again, thanks to Prof. Gregorio for his lucid explanation that helped
% improve this package.  See:
% http://tex.stackexchange.com/questions/123443/
% defining-a-length-that-scales-with-fontsize-changes/123470#123470
\newcommand{\setstackgap}[2]{\@namedef{#1stackgap}{#2}}
\newcommand\stackgap{\@nameuse{\if S\stacktype S\else L\fi stackgap}\relax}
\setstackgap{S}{3pt}% SHORTSTACKING GAP BETWEEN ITEMS (SAME AS \shortstack)
\setstackgap{L}{\baselineskip}% LONGSTACKING GAP BETWEEN ITEMS (def. \baselineskip)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\fi
\ifstackengine@usestackEOL%
% USE \\ AS SEPERATOR IN \Shortstack, \Longstack,
% \Shortunderstack, and \Longunderstack
  \setstackEOL{\\}
\else
% USE SPACE TOKEN AS SEPERATOR IN \Shortstack, \Longstack,
% \Shortunderstack, and \Longunderstack
  \setstackEOL{ }
\fi

% PACKAGE DEFAULT DEFINITIONS
\def\useanchorwidth{F}% T= FORCE BOX WIDTH TO ANCHOR-WIDTH
\def\quietstack{F}% T = SUPPRESS OUTPUT OF ALL COMMANDS
%                   (USE \usebox{\stackedbox} TO RETRIEVE)
\def\stackalignment{c}% l, c, or r for left, center or right alignment
\def\stacktype{S}% S for constant gap shortstack;
                 % L for constant baselineskip longstack

% DEPRECATED USAGE \def\usestackstrut{}; INSTEAD USE \strutshortanchors{}
%\def\usestackstrut{T}

%MACROS

%%%%% MODES
\newcommand\stackMath{\global\def\stack@delim{$}}
\newcommand\stackText{\global\def\stack@delim{}}
\stackText

\newcommand\strutlongstacks[1]{\def\@strutlongstacks{#1}}
\strutlongstacks{F}

\newcommand\strutshortanchors[1]{\def\usestackstrut{#1}}
\strutshortanchors{T}

%%%%%

%\stackengine{\Sstackgap or \Lstackgap or \stackgap or stacklength}
%            {anchor}
%            {item}
%            {O or U}
%            {\stackalignment or l or c or r}
%            {\quietstack or T or F}
%            {\useanchorwidth or T or F}
%            {\stacktype or S or L}
\newcommand*\stackengine[8]{%
  \def\@STRT{}%
  \if T\@strutlongstacks\if L#8\def\@STRT{\strut}\fi\fi%
  \sbox{\stackedbox}{%
    \sbox{\@anchorbox}{\stack@delim\@STRT#2\stack@delim}%
    \sbox{\@addedbox}{\stack@delim\@STRT#3\stack@delim}%
    \settowidth{\@stackedboxwidth}{\stack@delim\@STRT#2\stack@delim}%
    \if F#7%
      \ifdim\wd\@addedbox>\@stackedboxwidth%
        \settowidth{\@stackedboxwidth}{\stack@delim\@STRT#3\stack@delim}%
      \fi%
    \fi%
    \setlength{\@boxshift}{#1}%
    \if L#8%
      \if U#4%
        \setlength{\@boxshift}{-\@boxshift}%
      \fi%
    \else%
      \if U#4%
        \setlength{\@boxshift}{-\dp\@anchorbox -\ht\@addedbox -\@boxshift}%
      \else%
        \setlength{\@boxshift}{\ht\@anchorbox +\dp\@addedbox +\@boxshift}%
      \fi%
    \fi%
    \if c#5%
      \hspace{.5\@stackedboxwidth}%
      \hspace{-.5\wd\@anchorbox}%
      \usebox{\@anchorbox}%
      \hspace{-.5\wd\@anchorbox}%
      \hspace{-.5\wd\@addedbox}%
      \raisebox{\@boxshift}{\stack@delim\@STRT#3\stack@delim}%
      \hspace{-.5\wd\@addedbox}%
      \hspace{.5\@stackedboxwidth}%
    \else%
      \if l#5%
        \usebox{\@anchorbox}%
        \hspace{-\wd\@anchorbox}%
        \raisebox{\@boxshift}{\stack@delim\@STRT#3\stack@delim}%
        \hspace{-\wd\@addedbox}%
        \hspace{\@stackedboxwidth}%
      \else%
        \if r#5%
          \hspace{\@stackedboxwidth}%
          \hspace{-\wd\@anchorbox}%
          \usebox{\@anchorbox}%
          \hspace{-\wd\@addedbox}%
          \raisebox{\@boxshift}{\stack@delim\@STRT#3\stack@delim}%
        \fi%
      \fi%
    \fi%
  }%
  \if F#6\usebox{\stackedbox}\fi%
}

\newcommand*\stackunder[3][\stackgap]{\stackengine%
  {#1}{#2}{#3}{U}{\stackalignment}{\quietstack}{\useanchorwidth}{\stacktype}}

\newcommand*\stackon[3][\stackgap]{\stackengine%
  {#1}{#2}{#3}{O}{\stackalignment}{\quietstack}{\useanchorwidth}{\stacktype}}

\newcommand*\Shortstack[2][\stackalignment]{\@stack{#1}{#2}{O}{S}}

\newcommand*\Longstack[2][\stackalignment]{\@stack{#1}{#2}{O}{L}}

\newcommand*\Shortunderstack[2][\stackalignment]{\@stack{#1}{#2}{U}{S}}

\newcommand*\Longunderstack[2][\stackalignment]{\@stack{#1}{#2}{U}{L}}

\newcommand*\@stack[4]{%
  \@readMANYrows{#2}%
  \if U#3%
    \setcounter{@stackindex}{\narg}%
    \def\@stacksign{-}%
    \def\@stackcondition{\the@stackindex > 1}%
  \else%
    \setcounter{@stackindex}{1}%
    \def\@stacksign{}%
    \def\@stackcondition{\the@stackindex < \narg}%
  \fi%
  \sbox{\stackedbox}{%
    \stack@delim\csname arg\roman{@stackindex}\endcsname\stack@delim}%
  \whiledo{\@stackcondition}{%
    \addtocounter{@stackindex}{\@stacksign 1}%
    \stackengine{\csname#4stackgap\endcsname}%
      {\csname arg\roman{@stackindex}\endcsname}%
      {\usebox{\stackedbox}}{#3}{#1}{T}{\useanchorwidth}{#4}%
  }%
  \if F\quietstack\usebox{\stackedbox}\fi%
}

% PERHAPS THESE SIX MACROS CAN BE REPLACED BY \toplap AND \bottomlap
\newcommand*\tllap[2][\Lstackgap]{\@stacklap{#1}{l}{#2}{O}}
\newcommand*\tclap[2][\Lstackgap]{\@stacklap{#1}{c}{#2}{O}}
\newcommand*\trlap[2][\Lstackgap]{\@stacklap{#1}{r}{#2}{O}}
\newcommand*\bllap[2][\Lstackgap]{\@stacklap{#1}{l}{#2}{U}}
\newcommand*\bclap[2][\Lstackgap]{\@stacklap{#1}{c}{#2}{U}}
\newcommand*\brlap[2][\Lstackgap]{\@stacklap{#1}{r}{#2}{U}}

\newcommand*\toplap[3][\Lstackgap]{\@stacklap{#1}{#2}{#3}{O}}
\newcommand*\bottomlap[3][\Lstackgap]{\@stacklap{#1}{#2}{#3}{U}}

\newcommand*\@stacklap[4]{%
  \if l#2\stackengine{#1}{}{#3}{#4}{r}{F}{T}{L}\else%
    \if r#2\stackengine{#1}{}{#3}{#4}{l}{F}{T}{L}\else%
      \stackengine{#1}{}{#3}{#4}{c}{F}{T}{L}%
    \fi%
  \fi
}

\newcommand*\hsmash[1]{\stackengine{0pt}{}{#1}{O}{c}{F}{T}{L}}

\newcommand*\stackanchor[3][\stackgap]{%
  \setlength{\stack@tmplength}{#1}%
  \setlength{\stack@tmplength}{.5\stack@tmplength}%
  \if T\usestackstrut%
    \if S\stacktype\addtolength{\stack@tmplength}%
       {.5\ht\strutbox-.5\dp\strutbox}\fi%
  \fi%
  \stackengine{\stack@tmplength}{}{#2}{O}{\stackalignment}{T}{F}{\stacktype}%
  \if T\usestackstrut%
    \if S\stacktype\addtolength{\stack@tmplength}%
       {-\ht\strutbox+\dp\strutbox}\fi%
  \fi%
  \stackengine{\stack@tmplength}{\usebox{\stackedbox}}{#3}{U}%
              {\stackalignment}{\quietstack}{F}{\stacktype}%
}

\newcommand*\abovebaseline[2][\stackgap]{%
  \stackengine{#1}{}{#2}{O}{\stackalignment}{\quietstack}{F}{\stacktype}}

\newcommand*\belowbaseline[2][\stackgap]{%
  \stackengine{#1}{}{#2}{U}{\stackalignment}{\quietstack}{F}{\stacktype}}

\newcommand*\savestack[2]{%
  \global\edef\sv@name{\stack@macro@name{#1}}%
  \@ifundefined{\sv@name content}{%
    \expandafter\newsavebox\expandafter{\csname\sv@name content\endcsname}%
   }{}%
  \expandafter\sbox\csname\sv@name content\endcsname{#2}%
  \protected@edef#1{\usebox{\csname\sv@name content\endcsname}}%
}

\newcommand*\addstackgap[2][\Sstackgap]{\stackengine{#1}{%
  \stackengine{#1}{#2}{}{O}{c}{\quietstack}{T}{S}}{}{U}{c}{\quietstack}{T}{S}}

\newcommand*\topinset[4]{\stackinset{\stackalignment}{#4}{t}{#3}{#1}{#2}}

\newcommand*\bottominset[4]{\stackinset{\stackalignment}{#4}{b}{#3}{#1}{#2}}

%\stackinset{l/c/r}{x}{b/c/t}{y}{inset}{anchor}
\newcommand*\stackinset[6]{%
  \def\conditioned@inset{\stack@delim#5\stack@delim}%
  \ifthenelse{\equal{#4}{}}%
    {\setlength{\stack@tmplength}{0pt}}%
    {\setlength{\stack@tmplength}{#4}}%
  \if c#3%
    \setlength{\temp@stkl}{%
      \heightof{#6}+\depthof{#6}%
      -\heightof{\conditioned@inset}-\depthof{\conditioned@inset}}%
    \addtolength{\stack@tmplength}{.5\temp@stkl}%
  \fi%
  \ifthenelse{\equal{#2}{}}{\def\stack@tmp{0pt}}{\def\stack@tmp{#2}}%
  \def\stack@lroffset{\rule{\stack@tmp}{0pt}}%
  \addtolength{\stack@tmplength}{%
    \heightof{\conditioned@inset}+\depthof{\conditioned@inset}}%
  \if c#1%
    \def\conditioned@inset{\stack@lroffset\stack@lroffset#5}%
  \else%
    \def\conditioned@inset{\stack@lroffset#5\stack@lroffset}%
  \fi%
  \stackengine{-\stack@tmplength}{#6}{\conditioned@inset}%
              {\inset@valign{#3}}{#1}{\quietstack}{T}{S}%
}

\def\inset@valign#1{\if t#1O\else U\fi}

\endinput