summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/abraces/abraces.sty
blob: 768afbb3cdba82ef59ca1354283888ce9489a1e0 (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
\ProvidesPackage{abraces}% http://ctan.org/pkg/abraces
  [2012/08/24 v1.0 Arbitrary and asymmetric braces]
\newif\if@overload
\DeclareOption{overload}{\@overloadtrue}
\ProcessOptions
\newsavebox{\bracebox}% stores content contained under/over brace

\newcommand{\newbracespec}[2]{% Add a new brace specification based on existing ones
  \@defbracecharcode{#1}{\@genbrace#2\@nnil\@genbrace}
}
% Brace components
\newcommand{\bracescript}[1]{{% Add a script above/below an over-/underbrace
  \let\@@bfil\@@bunfil% Make line leaders into void/empty leaders
  \let\@use@rl@se\phantom% Phantomize some brace components
  \makebox[\wd\bracebox]{$\genbrace{#1}$}% Place the text in a box of width \bracebox
}}
\def\@@bunfil{\leaders\hbox{\ }\hfill}% Empty filler
\newcommand{\bracefil}[1]{%
  \csname brace@char@#1'\endcsname%
}
\def\@@bfil{\leaders \vrule \@height \ht\z@ \@depth \z@ \hfill}% default brace filler
%\def\bfil{\@@bfil}%
%\def\@bLfil{\@@bfil}% left leader filler
%\def\@bRfil{\@@bfil}% right leader filler
\def\aupbracefill#1{$\m@th\setbox\z@\hbox{$\braceld$}\genbrace{#1}$}
\def\adownbracefill#1{$\m@th\setbox\z@\hbox{$\braceld$}\genbrace{#1}$}

% Repetition structure. Reference:
%   Repeat command n times?
%   http://tex.stackexchange.com/q/16189/5764
% Alternatively
%   \RequirePackage{expl3}% http://ctan.org/pkg/expl3
%   \ExplSyntaxOn
%   \cs_new_eq:NN \Repeat \prg_replicate:nn
%   \ExplSyntaxOff

\newcommand{\Repeat}[1]{%
  \expandafter\@Repeat\expandafter{\the\numexpr #1\relax}%
}
\def\@Repeat#1{%
    \ifnum#1>0
        \expandafter\@@Repeat\expandafter{\the\numexpr #1-1\expandafter\relax\expandafter}%
    \else
        \expandafter\@gobble
    \fi
}
\def\@@Repeat#1#2{%
    \@Repeat{#1}{#2}#2%
}
% Taken from mathtools package (http://ctan.org/pkg/mathtools)
\newcommand{\aunderbrace}[2][l1D1r]{%
  \begin{lrbox}{\bracebox}$\displaystyle{#2}$\end{lrbox}%
  \mathop{\vtop{\m@th\ialign{##\crcr
    $\hfil\displaystyle{#2}\hfil$\crcr
    \noalign{\kern.7\fontdimen5\textfont2\nointerlineskip}%
    \aupbracefill{#1}\crcr\noalign{\kern.5\fontdimen5\textfont2}}}}\limits}
\newcommand{\aoverbrace}[2][L1U1R]{%
  \begin{lrbox}{\bracebox}$\displaystyle{#2}$\end{lrbox}%
  \mathop{\vbox{\m@th\ialign{##\crcr
    \noalign{\kern.5\fontdimen5\textfont2}%
    \adownbracefill{#1}\crcr
    \noalign{\kern.7\fontdimen5\textfont2\nointerlineskip}%
  $\hfil\displaystyle{#2}\hfil$\crcr}}}\limits}

% Parsing structure. Reference:
%   Parsing a macro argument character-by-character for conditional execution
%   http://tex.stackexchange.com/q/33197/5764

\let\@use@rl@se\relax
\def\@defbracecharcode#1{% Macro to define brace character "codes"
  \@namedef{brace@char@#1}%
}
\@defbracecharcode{L}{\@use@rl@se\braceld\@genbrace}% Left down
\@defbracecharcode{R}{\@use@rl@se\bracerd\@genbrace}% Right down
\@defbracecharcode{l}{\@use@rl@se\bracelu\@genbrace}% Left up
\@defbracecharcode{r}{\@use@rl@se\braceru\@genbrace}% Right up
\@defbracecharcode{U}{\@use@rl@se\braceru\@use@rl@se\bracelu\@genbrace}% Tip up
\@defbracecharcode{D}{\@use@rl@se\bracerd\@use@rl@se\braceld\@genbrace}% Tip down
\@defbracecharcode{0}{\@@bunfil\@genbrace}% Do nothing
\@defbracecharcode{1}{\@@bfil\@genbrace}% 1 filler
\@defbracecharcode{2}{\@@bfil\@@bfil\@genbrace}% 2 fillers
\@defbracecharcode{3}{\@@bfil\@@bfil\@@bfil\@genbrace}% 3 fillers
\@defbracecharcode{4}{\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 4 fillers
\@defbracecharcode{5}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 5 fillers
\@defbracecharcode{6}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 6 fillers
\@defbracecharcode{7}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 7 fillers
\@defbracecharcode{8}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 8 fillers
\@defbracecharcode{9}{\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@@bfil\@genbrace}% 9 fillers
\@defbracecharcode{@}{\@firstofone@go}% Insert arbitrary stuff
\@defbracecharcode{!}{\@firstofone@len}% Insert a leader of specific length
\@defbracecharcode{*}{\@repeat@num@times}% Repeat stuff a number of times

\newcommand{\@firstofone@go}[1]{#1\@genbrace}%
\newcommand{\@firstofone@len}[1]{\hb@xt@#1{\@@bfil}\@genbrace}%
\newcommand{\@repeat@num@times}[2]{\Repeat{#1}{\@genbrace#2\@nnil}\@genbrace}%

\newcommand{\@genbrace}[1]{%
  \ifx\@nnil#1\relax\else
    \@nameuse{brace@char@#1\expandafter}%
  \fi%
}
\newcommand{\genbrace}[1]{%
  \@genbrace#1\@nnil%
}

% If package was loaded with \usepackage[overload]{abraces}
\if@overload
  \let\overbrace\aoverbrace
  \let\underbrace\aunderbrace
\fi
\endinput