Debian Codegraph @

texmf-dist/source/latex/eqnalign/eqnalign.dtx

raw
1 % \iffalse meta-comment
2 %!TeX program = T_do_dtx
3 %
4 % This part is not going to appear anywhere so is good for 'header'
5 % information.
6 %
7 %
8 %<*driver>
9 \documentclass{ltxdoc}
10
11 \usepackage[vmargin=0.7in,hmargin={1.5in,0.6in},marginparsep=0.1in,marginparwidth=1in,a4paper]{geometry}
12 \usepackage{lmodern,anttor}
13 \usepackage[T1]{fontenc}
14 \usepackage{enumerate}
15 \usepackage{xcolor}
16 \usepackage{url, hyperref}
17 \usepackage{amsmath}
18
19 \DisableCrossrefs
20 \CodelineNumbered
21 \def\theCodelineNo{{\color{black}\normalfont\fontseries{sb}\selectfont\footnotesize\arabic{CodelineNo}}}
22 \def\MacroFont{\ttfamily\color{blue}}
23
24 % Whatever packages here
25 %
26 % Might include
27 % \usepackage{\jobname}
28 %
29 % Very handy is
30 % \usepackage[numbered]{hypdoc} % hyperref for l3doc
31 %
32 % You might want the following
33 %\EnableCrossrefs
34 %\CodelineIndex
35 %\RecordChanges
36 \begin{document}
37 \DocInput{\jobname.dtx}
38 \end{document}
39 %</driver>
40 % \fi
41 %
42 % ^^A Comments in the documentation part start ^^A
43 %
44 % ^^A If you use the extracted package itself you can do
45 % \GetFileInfo{\jobname.sty}
46 % ^^A and define \filedate, etc.
47 %
48 % ^^A Meta data for the package
49 % \title{\pkg{mypkg}\thanks{^^A
50 % This file describes version \fileversion, last revised \filedate.^^A
51 % }}
52 % \author{^^A
53 % Tom Hejda\thanks{E-mail: [email protected]}^^A
54 % }
55 % \date{Released \filedate}
56 %
57 % ^^A \section{User documentation}
58 %
59 % ^^A User documentation would appear here.
60 %
61 % ^^A This will appear at the end of the PDF, which might or might not include
62 % ^^A the typeset code
63 % \StopEventually{^^A
64 % \PrintChanges
65 % \PrintIndex
66 % }
67 %
68 % \vspace*{2cm}
69 % \begingroup
70 % \centering
71 % {\Huge\bfseries \LaTeX\@ package \texttt{eqnalign}\par}
72 % \bigskip
73 % {\Large\bfseries --- --- making |eqnarray|(|*|) look and work like |align|(|*|) --- ---\par}
74 % \bigskip
75 % {\Large by Tom Hejda, \texttt{[email protected]}\par}
76 % \endgroup
77 % \vspace*{2cm}
78 %
79 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % \section{The goal}
81 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %
83 % The goal of this package is to allow easy conversion from the insanely-looking |eqnarray| to
84 % the look and behaviour of |align| from |amsmath|.
85 % It is inspired by a \TeX.StackEchange question \url{http://tex.stackexchange.com/q/96210/11002} by a user called ``Werner'',
86 % and by an answer of mine to the question.
87 %
88 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 % \section{The behaviour}
90 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 %
92 % The package is activated by simply loading it, and it does not have any package options.
93 % It just redefines |eqnarray| and |eqnarray*|,
94 % and then it makes |amsthm| aware of this redefinition so that |\qedhere| works inside these environments.
95 % At the same time, we keep the original definition od |eqnarray| and |eqnarray*| as |EQNarray| and |EQNarray*|,
96 % respectively.
97 % Note that |hyperref| modifies |eqnarray|, so the package shall be loaded after |hyperref|;
98 % this also means that |hyperref| tweaks do not make it into |EQNarray|(|*|).
99 %
100 % The main feature is implemented in a simple way:
101 % We keep track of |&| count, and we disable the 2nd |&| of each line.
102 % (Note that we also disable the 5th, 8th etc.\@ ones.)
103 % We do it in a way that correctly interacts with cases such as when |array| or |matrix| is inside the environment.
104 %
105 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % \section{License and remarks}
107 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %
109 % \begin{enumerate}[(A)]
110 %
111 % \item
112 % The package is licensed under the \LaTeX\@ Project Public License version 1.3c (LPPL v1.3c) or higher.
113 % The latest version of this license is in \url{http://www.latex-project.org/lppl.txt}\,.
114 %
115 % \item
116 % Note that actually, the usage of this package is discouraged, in favour of converting the code into
117 % proper ``|amsmath| code'', using the true |align| and |align*| environments.
118 % It is intended for cases where a lot of already existing code needs to be converted and there
119 % is no capacity for doing it the right but time-consuming way.
120 %
121 % \item
122 % All bugs shall be reported to the GitHub page \url{http://github.com/tohecz/eqnalign}\,.
123 % Just note that unless the bug is crucial or easy to deal with, it may not be fixed since
124 % (per the previous remark) single problematic cases shall be dealt with by other means.
125 %
126 % \end{enumerate}
127 %
128 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 % \section{Known issues}
130 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 %
132 % \begin{enumerate}[(1)]
133 %
134 % \item
135 % Due to catcode changes, |eqnarray| will not work inside macro arguments and similar contexts.
136 %
137 % \item
138 % \fboxsep-5pt
139 % Note that as the package actually ignores the 2nd |&| of an |eqnarray| line,
140 % it in turn kills the alignment in cases such as the following:
141 % \\[-\baselineskip]
142 % \begin{tabular}[c]{@{}p{0.55\linewidth}@{}}
143 % \begin{verbatim}
144 %\begin{eqnarray*}
145 % A & = & 104 x^5 + 84 x^4 + 53 x^3 \\
146 % & & + 17 x^2 + 4 x - 1043
147 %\end{eqnarray*}\end{verbatim}
148 % \end{tabular}
149 % \fbox{\parbox[c]{0.35\linewidth}{\begin{eqnarray*}
150 % A & = & 104 x^5 + 84 x^4 + 53 x^3 \\
151 % & & + 17 x^2 + 4 x - 1043
152 % \end{eqnarray*}}}
153 % \vspace{-\baselineskip}
154 %
155 % This becomes, when |eqnalign| is loaded:
156 % \\[-\baselineskip]
157 % \begin{tabular}[c]{@{}p{0.55\linewidth}@{}}
158 % \begin{verbatim}
159 %\begin{align*}
160 % A & = 104 x^5 + 84 x^4 + 53 x^3 \\
161 % & + 17 x^2 + 4 x - 1043
162 %\end{align*}\end{verbatim}
163 % \end{tabular}
164 % \fbox{\parbox[c]{0.35\linewidth}{\begin{align*}
165 % A & = 104 x^5 + 84 x^4 + 53 x^3 \\
166 % & + 17 x^2 + 4 x - 1043
167 % \end{align*}}}
168 % \vspace{-\baselineskip}
169 %
170 % However, the prefered form would be, for instance:
171 % \\[-\baselineskip]
172 % \begin{tabular}[c]{@{}p{0.55\linewidth}@{}}
173 % \begin{verbatim}
174 %\begin{align*}
175 % A & = 104 x^5 + 84 x^4 + 53 x^3 \\
176 % & \phantom{={}} + 17 x^2 + 4 x - 1043
177 %\end{align*}\end{verbatim}
178 % \end{tabular}
179 % \fbox{\parbox[c]{0.35\linewidth}{\begin{align*}
180 % A & = 104 x^5 + 84 x^4 + 53 x^3 \\
181 % & \phantom{={}} + 17 x^2 + 4 x - 1043
182 % \end{align*}}}
183 % \vspace{-\baselineskip}
184 %
185 % This issue is actually somehow inherited from |align|, which in stardard usage shows the very same behaviour.
186 %
187 %
188 % \end{enumerate}
189 %
190 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % \section{Implementation}
192 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 %
194 % Note that we in general say |eqnarray| where we really mean either |eqnarray| or |eqnarray*|.
195 %
196 % \begin{macrocode}
197 %<*package>
198 % \end{macrocode}
199 % Package header.
200 % \begin{macrocode}
201 \ProvidesPackage{eqnalign}[2017/02/19 v1.0a Make eqnarray(*) behave like align(*).]
202 % \end{macrocode}
203 % The only necessary package is |amsmath| so that |align| and |align*| are defined.
204 % \begin{macrocode}
205 \RequirePackage{amsmath}
206 % \end{macrocode}
207 %
208 % The package does some catcode mysteries that shouldn't propagate out, so we make everything
209 % in a group and use |\gdef| everywhere.
210 % \begin{macrocode}
211 \begingroup
212 % \end{macrocode}
213 %
214 % \begin{macro}{\eqna@tab@amp}
215 % We store a catcode-4 (tab alignment char) |&| in a macro.
216 % We need a catcode-13 (active) |&| througout the rest of the package.
217 % \begin{macrocode}
218 \catcode`\&=4
219 \gdef\eqna@tab@amp{&}
220 \catcode`\&=13
221 % \end{macrocode}
222 % \end{macro}
223 %
224 % \begin{macro}{\eqna@new@amp}
225 % This will be the replacement of |&| inside |eqnarray|.
226 % We use |\eqna@amp@| if the innermost environment is |eqnarray| and |\eqna@tab@amp| otherwise;
227 % this is to allow things like arrays and matrices inside |eqnarray|.
228 % \begin{macrocode}
229 \gdef\eqna@new@amp{%
230 \ifx\@currenvir\eqna@currenvir
231 \expandafter\eqna@amp@
232 \else
233 \expandafter\eqna@tab@amp
234 \fi
235 }
236 % \end{macrocode}
237 % \end{macro}
238 %
239 % \begin{macro}{\eqna@amp@i}
240 % \begin{macro}{\eqna@amp@ii}
241 % \begin{macro}{\eqna@amp@iii}
242 % Three macros that are ``rotated'', after the first, the second shall be used, then the third.
243 % The third one ends a group since it ends a table cell,
244 % therefore after that the first one is again in action.
245 % The first |&| on a line is kept, the second is ignored, the third is kept.
246 % \begin{macrocode}
247 \gdef\eqna@amp@i{\eqna@tab@amp\let\eqna@amp@\eqna@amp@ii}
248 \gdef\eqna@amp@ii{\let\eqna@amp@\eqna@amp@iii}
249 \gdef\eqna@amp@iii{\eqna@tab@amp}
250 % \end{macrocode}
251 % \end{macro}
252 % \end{macro}
253 % \end{macro}
254 %
255 % \begin{macro}{\eqna@doamp}
256 % The default is |\eqna@amp@i|.
257 % \begin{macrocode}
258 \global\let\eqna@amp@\eqna@amp@i
259 % \end{macrocode}
260 % \end{macro}
261 %
262 % \begin{macro}{\eqna@hook}
263 % Hook exectuted at the beginning of |eqnarray|.
264 % We store the current environment, which is either |eqnarray| or |eqnarray*|;
265 % it is used in |\eqna@new@amp| for the test for nested environments.
266 % Then we activate |&| and make its meaning |\eqna@new@amp|.
267 % \begin{macrocode}
268 \gdef\eqna@hook{%
269 \let\eqna@currenvir\@currenvir
270 \catcode`\&=\active
271 \let&\eqna@new@amp
272 }
273 % \end{macrocode}
274 % \end{macro}
275 %
276 % Now we will be defining environments containing |*| in name, so we make it a letter.
277 % \begin{macrocode}
278 \catcode`\*=11
279 % \end{macrocode}
280 %
281 % \begin{macro}{\eqna@def@env}
282 % \begin{macro}{eqnarray}
283 % \begin{macro}{eqnarray*}
284 % We define a macro |\eqna@def@env| that contains the redefinitions of |eqnarray| (and |eqnarray*|).
285 % The environments themselves are like |align|, just hooked using |\eqna@hook|.
286 % We then call this macro immediately to define the environments.
287 % (All this fuss with |\eqna@def@env| is to correct things in case |hyperref| is loaded after |eqnalign|.
288 % \begin{macrocode}
289 \gdef\eqna@def@env{%
290 \gdef\eqnarray{\eqna@hook\align}%
291 \gdef\eqnarray*{\eqna@hook\align*}%
292 \global\let\endeqnarray\endalign
293 \global\let\endeqnarray*\endalign*
294 }
295 \eqna@def@env
296 % \end{macrocode}
297 % \end{macro}
298 % \end{macro}
299 % \end{macro}
300 %
301 % \begin{macro}{\eqnarray@qed}
302 % \begin{macro}{\eqnarray*@qed}
303 % To make |amsthm|'s |\qedhere| work in |eqnarray|, we need to ``hint'' |amsthm| that it exists
304 % \begin{macrocode}
305 \global\let\eqnarray@qed\align@qed
306 \global\let\eqnarray*@qed\align*@qed
307 % \end{macrocode}
308 % \end{macro}
309 % \end{macro}
310 %
311 % End the group we began at the very beginning.
312 % \begin{macrocode}
313 \endgroup
314 % \end{macrocode}
315 %
316 % \begin{macro}{EQNarray}
317 % Just of sentiment, we keep the original |eqnarray| as |EQNarray|;
318 % the code is a verbatim copy from |ltmath.dtx| (part of \LaTeXe\@ kernel).
319 % \begin{macrocode}
320 \def\EQNarray{%
321 \stepcounter{equation}%
322 \def\@currentlabel{\p@equation\theequation}%
323 \global\@eqnswtrue
324 \m@th
325 \global\@eqcnt\z@
326 \tabskip\@centering
327 \let\\\@eqncr
328 $$\everycr{}\halign to\displaywidth\bgroup
329 \hskip\@centering$\displaystyle\tabskip\z@skip{##}$\@eqnsel
330 &\global\@eqcnt\@ne\hskip \tw@\arraycolsep \hfil${##}$\hfil
331 &\global\@eqcnt\tw@ \hskip \tw@\arraycolsep
332 $\displaystyle{##}$\hfil\tabskip\@centering
333 &\global\@eqcnt\thr@@ \hb@xt@\z@\bgroup\hss##\egroup
334 \tabskip\z@skip
335 \cr
336 }
337 \def\endEQNarray{%
338 \@@eqncr
339 \egroup
340 \global\advance\c@equation\m@ne
341 $$\@ignoretrue
342 }
343 \@namedef{EQNarray*}{\def\@eqncr{\nonumber\@seqncr}\EQNarray}
344 \@namedef{endEQNarray*}{\nonumber\endEQNarray}
345 % \end{macrocode}
346 % \end{macro}
347 %
348 % Last but not least, if |hyperref| is loaded after |eqnalign| (and only in that case),
349 % we issue a warning since |hyperref| is doing bad things to |eqnarray|, and we redefine |eqnalign| once more.
350 % \begin{macrocode}
351 \@ifpackageloaded{hyperref}{}{
352 \AtBeginDocument{
353 \@ifpackageloaded{hyperref}{
354 \@latex@warning{Package `eqnalign' should be loaded after
355 `hyperref'.\MessageBreak Redefining `eqnarray' and `eqnarray*' at this
356 point \MessageBreak and crossing fingers...}
357 \eqna@def@env
358 }{}
359 }
360 }
361 % \end{macrocode}
362 %
363 % That's all.
364 % \begin{macrocode}
365 \endinput
366 % \end{macrocode}
367 % \begin{macrocode}
368 %</package>
369 % \end{macrocode}
370 %
371 % \Finale