summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/aligned-overset/aligned-overset.dtx
blob: 2a3793b69448295f2084fdaf2d39d0a803eff634 (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
% \iffalse meta-comment
%
%% Copyright (C) 2018 by Marcel Krueger
%%
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3c 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.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%
%<*batch>
%<*gobble>
\ifx\jobname\relax\let\documentclass\undefined\fi
\ifx\documentclass\undefined
\csname fi\endcsname
%</gobble>
\input l3docstrip.tex
\keepsilent
\preamble
\endpreamble
\generate{\file{aligned-overset.sty}{\from{aligned-overset.dtx}{package}}}
\endbatchfile
%</batch>
%<*gobble>
\fi
\expandafter\ifx\csname @currname\endcsname\empty
\csname fi\endcsname
%</gobble>
%<*driver>
\documentclass[full]{l3doc}
\usepackage{aligned-overset}
\begin{document}
\DocInput{aligned-overset.dtx}
\PrintIndex
\PrintChanges
\end{document}
%</driver>
%<*gobble>
\fi
%</gobble>
% \fi
%
% \GetFileInfo{aligned-overset.dtx}
% \title{The \pkg{aligned-overset} package\thanks{This document
%        corresponds to \pkg{aligned-overset}~0.1.0, dated~2018/04/04.}}
% \author{Marcel Kr\"uger \\ \href{mailto:tex@2krueger.de}{tex@2krueger.de}}
% 
% \maketitle
%
% \begin{documentation}
% This package allows the base character of \cmd\underset{} or \cmd\overset{} to be used as the alignment position for the amsmath aligned math environments.
%
% \section{Example}
% The naive way to align at an operator annotated by \cmd\overset{} would be
% \begin{verbatim}
% \begin{align*}
%   f(x)&\overset{\text{Def}}=x+x\\
%       &                    =2x
% \end{align*}
% \end{verbatim}
% This results in
% \begin{align*}
%   f(x)&\overset{\text{Def}}=x+x\\
%       &                    =2x
% \end{align*}
% But here, the two equal signes are not aligned.
%
% Using this package, this can be fixed by writing
% \begin{verbatim}
% \begin{align*}
%   f(x)\overset{\text{Def}}&=x+x\\
%                           &=2x
% \end{align*}
% \end{verbatim}
% which result in correct alignment:
% \begin{align*}
%   f(x)\overset{\text{Def}}&=x+x\\
%                           &=2x
% \end{align*}
% \section{Usage}
% \begin{function}{\overset,\underset}
% To add an alignment point at an \cmd\overset{} or \cmd\underset, add a |&| before the base symbol.
% The |&| should not be enclosed in braces, even if the symbols are enclosed in braces.
% For example you have to write |\overset{\approx}&{\ge}| instead of |\overset{\approx}{&\ge}|.
% \end{function}
% \end{documentation}
% \begin{implementation}
% \section{The implementation}
%    \begin{macrocode}
%<@@=alignedsets>
%    \end{macrocode}
% \iffalse
%<*package>
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplPackage
  {aligned-overset}
  {2018/04/04}
  {0.1.0}
  {aligned overset and underset}

\RequirePackage{xparse,amsmath,mathtools}
%</package>
% \fi
%    \begin{macrocode}
\box_new:N\l__alignedsets_clap_box
\box_new:N\l__alignedsets_full_box

%    \end{macrocode}
% \begin{macro}{\overset}
%    \begin{macrocode}
\let\__alignedsets_original_overset:nn\overset
\cs_set:Npn\overset{
  \group_align_safe_begin:
  \__alignedsets_modified_overset:nwn
}
\NewExpandableDocumentCommand\__alignedsets_modified_overset:nwn{mt{&}m}{
  \group_align_safe_end:
  \IfBooleanTF{#2}{
    \group_begin:
    \hbox_set:Nn\l__alignedsets_clap_box{
      $
      \displaystyle
      {}\__alignedsets_original_overset:nn{\mathclap{#1}}{#3}{}
      $
    }
    \hbox_set:Nn\l__alignedsets_full_box{
      $
      \displaystyle
      {}\__alignedsets_original_overset:nn{#1}{#3}{}
      $
    }
    \hspace{
      \dim_eval:n{(\box_wd:N\l__alignedsets_full_box-\box_wd:N\l__alignedsets_clap_box)/2}
    }
    \exp_args:NNNx
        \group_end:
        {&}
        \hspace{
          -\dim_eval:n{
            (\box_wd:N\l__alignedsets_full_box-\box_wd:N\l__alignedsets_clap_box)/2
          }
        }
    \__alignedsets_original_overset:nn{#1}{#3}
  }{
    \__alignedsets_original_overset:nn{#1}{#3}
  }
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\underset}
%    \begin{macrocode}
\let\__alignedsets_original_underset:nn\underset
\cs_set:Npn\underset{
  \group_align_safe_begin:
  \__alignedsets_modified_underset:nwn
}
\NewExpandableDocumentCommand\__alignedsets_modified_underset:nwn{mt{&}m}{
  \group_align_safe_end:
  \IfBooleanTF{#2}{
    \group_begin:
    \hbox_set:Nn\l__alignedsets_clap_box{
      $
      \displaystyle
      {}\__alignedsets_original_underset:nn{\mathclap{#1}}{#3}{}
      \m@th
      $
    }
    \hbox_set:Nn\l__alignedsets_full_box{
      $
      \displaystyle
      {}\__alignedsets_original_underset:nn{#1}{#3}{}
      \m@th
      $
    }
    \hspace{
      \dim_eval:n{(\box_wd:N\l__alignedsets_full_box-\box_wd:N\l__alignedsets_clap_box)/2}
    }
    \exp_args:NNNx
        \group_end:
        {&}
        \hspace{
          -\dim_eval:n{
            (\box_wd:N\l__alignedsets_full_box-\box_wd:N\l__alignedsets_clap_box)/2
          }
        }
    \__alignedsets_original_underset:nn{#1}{#3}
  }{
    \__alignedsets_original_underset:nn{#1}{#3}
  }
}
%    \end{macrocode}
% \end{macro}
% \end{implementation}