summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/conteq/conteq.dtx
blob: 07ce18b5a4f0442e4593f55720d0f4e7e754f110 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
% \iffalse meta-comment
% !TEX program  = pdfLaTeX
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
conteq --- continued equalities
E-mail: mail@joachim-breitner.de
Source repository: http://git.nomeata.de/?p=conteq.git
Source repository mirror: https://github.com/nomeata/conteq
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

This package allows you to write an continued equality such as

    \begin{conteq}
    e^{\pi\cdot i} \\
    = -1 & Euler's formula \\
    < 0 & this is an inequality \\ 
    < \sqrt 3 \\
    = \int e^{-x^2} dx & this is due to Gauss.
    \end{conteq}

in a declarative manner and easily switch between various layouts afterwards.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
conteq --- continued equalities
E-mail: mail@joachim-breitner.de
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

\endpreamble
\postamble

Copyright (C) 2013,2015 by Joachim Breitner <mail@joachim-breitner.de>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version.  The latest version of this license is in the file:

http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
Joachim Breitner.

This work consists of the file  conteq.dtx
and the derived files           conteq.ins,
                                conteq.pdf and
                                conteq.sty.

\endpostamble
\usedir{tex/latex/conteq}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}
                      \from{\jobname.dtx}{layouts}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\RequirePackage{expl3}[2012/07/02]
%<package>\ProvidesExplPackage
%<package>  {conteq}              % Package name
%<package>  {2015/07/17}          % Release date
%<package>  {0.1.1}                 % Release version
%<package>  {Various layouts for continued equalities} % Description
%
%<*driver>
\documentclass[full]{l3doc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{conteq}
%\usepackage[numbered]{hypdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{\jobname.sty}
%
% \CheckSum{175}
%
%
%\title{^^A
%  \textsf{conteq} --- continued equalities\thanks{^^A
%    This file describes version \fileversion, last revised \filedate.^^A
%  }^^A
%}
%\author{Joachim Breitner\thanks{E-mail: mail@joachim-breitner.de}}
%
%\date{Released \filedate}
%
%\maketitle
%
%\changes{0.1.1}{2015/07/17}{Stop using deprecated expl3 macros}
%\changes{0.1}{2013/05/23}{Converted to DTX file}
%
%
% \section{Introduction}
%
% There are various ways to layout an continued equality that spans multiple lines and has explanations along some steps. Often, the best layout is not clear beforehand, as it depends on the sizes of the various elements, or implementing it adds too much noise to the actual formulas. This package provides an environment for continued equalities (or inequalities) that allows you to easily define and switch layouts.
%
% \section{Usage}
%
% \DescribeEnv{conteq}
% Our running example is the following continued equality:
% 
% \begin{verbatim}
% \begin{conteq}
% e^{\pi\cdot i} \\
% = -1 & Euler's formula \\
% < 0 & this is an inequality \\ 
% < \sqrt 3 \\
% = \int e^{-x^2} dx & this is due to Gauss.
% \end{conteq}
% \end{verbatim}
%
% As you can see, the expressions of the continued equality are separated by |\\|, with equality signs (or other relations) at the beginning of all lines but the first. Some equalities also have explanations.
% 
% The result of the above code is
% \begin{conteq}
% e^{\pi\cdot i} \\
% = -1 & Euler's formula \\
% < 0 & this is an inequality \\ 
% < \sqrt 3 \\
% = \int e^{-x^2} dx & this is due to Gauss.
% \end{conteq}
%
% The expressions are typeset in math mode, while the explanation is assumed to be regular text. The curly braces around the explanations come from the default |\ConteqExplStyle|.
%
% There are other layouts available, which you select with an optional parameter to |\begin{conteq}|, e.g.
% \begin{verbatim}
% \begin{conteq}[explline]
% [..]
% \end{conteq}
% \end{verbatim}
%
% The layouts defined by |conteq| are:
%
% \paragraph{plain} The default layout, shown above.
%
% \paragraph{explline} A layout that puts the explanations below the right-hand side of the equalty it is explaining. To be used when the explanations and righ-hand sides are long.
% \begin{conteq}[explline]
% e^{\pi\cdot i} \\
% = -1 & Euler's formula \\
% < 0 & this is an inequality \\ 
% < \sqrt 3 \\
% = \int e^{-x^2} dx & this is due to Gauss.
% \end{conteq}

%
% \paragraph{headline} Like plain, but the first expression is also vertically aligned with the right-hand sides.
% \begin{conteq}[headline]
% e^{\pi\cdot i} \\
% = -1 & Euler's formula \\
% < 0 & this is an inequality \\ 
% < \sqrt 3 \\
% = \int e^{-x^2} dx & this is due to Gauss.
% \end{conteq}
%
% \paragraph{onecolumn} A combination of \textbf{explline} and \textbf{headline} that puts everything in one column, for maximum space efficiency.
% \begin{conteq}[onecolumn]
% e^{\pi\cdot i} \\
% = -1 & Euler's formula \\
% < 0 & this is an inequality \\ 
% < \sqrt 3 \\
% = \int e^{-x^2} dx & this is due to Gauss.
% \end{conteq}
%
% \paragraph{oneline} A layout, more for demonstrational purposes, that ignores the explanations and puts everything in one line.
% \begin{conteq}[oneline]
% e^{\pi\cdot i} \\
% = -1 & Euler's formula \\
% < 0 & this is an inequality \\ 
% < \sqrt 3 \\
% = \int e^{-x^2} dx & this is due to Gauss.
% \end{conteq}
%
% \DescribeMacro{\ConteqSetDefaultLayout} You can change the default layout using |\ConteqSetDefaultLayout|\marg{layout}
%
% \section{Defining layouts}
%
% \DescribeMacro{\ConteqDefineLayout}
% To define a new layout you use |\ConteqDefineLayout|, which takes seven arguments:
% \begin{enumerate}
% \item The name of the layout,
% \item What to put before the continued equalities,
% \item the left-hand side of the first equality,
% \item the right-hand side of equalities without explanation,
% \item the right-hand side of equalities with explanation,
% \item the line separator and
% \item what to put after the continued equalities.
% \end{enumerate}
%
% For example the existing layouts are defined using
% \iffalse
%<*layouts>
% \fi
%    \begin{macrocode}

\ConteqDefineLayout
    {plain}
    {\begin{align*}}
    { #1 }
    { & #1 }
    { & #1 && #2 }
    { \\ }
    {\end{align*}}
\ConteqSetDefaultLayout{plain}

\ConteqDefineLayout
    {explline}
    {\begin{align*}}
    { #1 }
    { & #1 }
    { & #1 \\ & \mathrel{\phantom{=}} #2 }
    { \\ }
    {\end{align*}}

\ConteqDefineLayout
    {headline}
    {\begin{align*}}
    { &\mathrel{\phantom{=}} #1 \\ }
    { & #1 }
    { & #1 && #2 }
    { \\ }
    {\end{align*}}
 
\ConteqDefineLayout
    {onecolumn}
    {\begin{align*}}
    { &\mathrel{\phantom{=}} #1 \\ }
    { & #1 }
    { & #1 \\ & \mathrel{\phantom{=}} #2 }
    { \\ }
    {\end{align*}}

\ConteqDefineLayout
    {oneline}
    {\[}
    { #1 }
    { #1 }
    { #1 }
    {}
    {\]}

%    \end{macrocode}
%
% \section{Changing the explanation style}
%
% \DescribeMacro{\ConteqExplStyle}
% The explanation is formatted using the macro |\ConteqExplStyle|\marg{explanation}, which is by default defined as
%    \begin{macrocode}
\newcommand{\ConteqExplStyle}[1]{\{~#1~\}}
%    \end{macrocode}
%
% To change the style, simply redefine this macro using, for example:
% \begin{verbatim}
% \renewcommand{\ConteqExplStyle}{\textit{-- #1}}
% \end{verbatim}
%
%
% \section{Future work}
%
% This package is, at the time of writing, very new, so please le me know about problems you are having or features you are missing.
%
% One feature that I am considering is an auto-selection of layouts, so when you specify |\begin{conteq}[plain,explline,onecolumn]...\end{conteq}|, it will analyze the table and select, from your list of layouts, the first one that is ``ok'', where ``ok'' would be some layout-specific heuristic taking the size of the expressions and explanations as well as the current |\linewidth| into account. If you think that this would be useful to you, please let me know.
%
% You can follow the package's development at \url{http://git.nomeata.de/?p=conteq.git} or the mirror at \url{https://github.com/nomeata/conteq}.
%
% \iffalse
%</layouts>
% \fi
%
%\StopEventually{^^A
%  \PrintChanges
%  \PrintIndex
%}
%
%
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
% \subsection{Package loading}
% 
%    \begin{macrocode}
\RequirePackage{amsmath}
\RequirePackage{environ}
%    \end{macrocode}
%
% \subsection{Defining Messages}
%
%    \begin{macrocode}
\msg_new:nnn
    { conteq }
    { empty }
    { Empty~conteq~environment~\msg_line_context: }

\msg_new:nnn
    { conteq }
    { ignoreddata }
    { Ignored~text~\msg_line_context: }

\msg_new:nnn
    { conteq }
    { undefined layout }
    { Undefined~layout~``#1''~\msg_line_context: }
%    \end{macrocode}

% \subsection{Declaring local variables}
%
%    \begin{macrocode}
\tl_new:N \l__conteq_default_layout_tl
\tl_new:N \l__conteq_layout_tl
\tl_new:N \l__conteq_body_tl
\tl_new:N \l__conteq_lines_seq
\tl_new:N \l__conteq_cells_seq
\tl_new:N \l__conteq_head_tl
\tl_new:N \l__conteq_lastline_tl
\tl_new:N \l__conteq_rhs_tl
\tl_new:N \l__conteq_expl_tl
\tl_new:N \l__conteq_result_tl
%    \end{macrocode}

% \subsection{Layouts}
%
% \begin{macro}{\ConteqSetDefaultLayout}
% The code to set the default layout.
%    \begin{macrocode}
\cs_new_protected:Nn \__conteq_set_default_layout:n
  {
    \tl_set:Nn \l__conteq_default_layout_tl {#1}
  }
\cs_new_eq:NN \ConteqSetDefaultLayout \__conteq_set_default_layout:n
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ConteqDefineLayout}
% The code to define new layouts.
%    \begin{macrocode}
\tl_const:Nn \c__conteq_prefix_tl { __conteq_layouts }

\cs_new_protected:Nn \__conteq_define_layout:nnnnnnn
  {
    \cs_new_protected:cn { \c__conteq_prefix_tl _ #1 _begin: } { #2 }
    \cs_new_protected:cn { \c__conteq_prefix_tl _ #1_lhs:n } { #3 }
    \cs_new_protected:cn { \c__conteq_prefix_tl _ #1_rhs_only:n } { #4 }
    \cs_new_protected:cn { \c__conteq_prefix_tl _ #1_rhs_expl:nn } { #5 }
    \cs_new_protected:cn { \c__conteq_prefix_tl _ #1_nl: } { #6 }
    \cs_new_protected:cn { \c__conteq_prefix_tl _ #1_end: } { #7 }
  }
\cs_new_eq:NN \ConteqDefineLayout \__conteq_define_layout:nnnnnnn
%    \end{macrocode}
% \end{macro}
%
% \subsection{Expansion utility function}
% 
% \begin{macro}{\__conteq_args_once:Nn, \__conteq_args_once:NV, \__conteq_args_once:Nnn, \__conteq_args_once:NVV}
% For ease of debugging(?) we construct a token list that contains exactly the tokens that a programmer would enter to create the layout manually. For that we need some fine-grained control over expansion.
%
%    \begin{macrocode}
\cs_new:Npn \__conteq_args_once:Nn #1#2
  {
   \exp_not:o { #1 {#2} }
  }
\cs_generate_variant:Nn \__conteq_args_once:Nn { NV }

\cs_new:Npn \__conteq_args_once:Nnn #1#2#3
  {
   \exp_not:o { #1 { #2 }{ #3 } }
  }
\cs_generate_variant:Nn \__conteq_args_once:Nnn { NVV }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Main code}
%
% \begin{macro}{\__conteq_print_line:nn}
% This macro splits and prints one line of the table. The second argument is either |\\| or, for the last line of the table, empty.
%    \begin{macrocode}
\cs_new_protected:Npn \__conteq_print_line:Nnn #1#2#3
  {
    \seq_set_split:Nnn \l__conteq_cells_seq { & } { #2 }
    \seq_pop_left:NN \l__conteq_cells_seq \l__conteq_rhs_tl
    \tl_clear:N \l__conteq_expl_tl
    \seq_if_empty:NF \l__conteq_cells_seq
      {
	\seq_pop_left:NN \l__conteq_cells_seq \l__conteq_expl_tl
      }
    \tl_if_blank:VTF \l__conteq_expl_tl
      {
	\tl_put_right:Nx #1
	  {
	    \__conteq_args_once:NV \__conteq_rhs_only:n \l__conteq_rhs_tl
	  }
      }
      {
	\tl_set:Nx \l_tmpa_tl { \exp_not:N \text { \exp_not:N \ConteqExplStyle { \exp_not:V \l__conteq_expl_tl } } }
	\tl_put_right:Nx #1
	  {
	    \__conteq_args_once:NVV \__conteq_rhs_expl:nn \l__conteq_rhs_tl \l_tmpa_tl
          }
      }
      \seq_if_empty:NF \l__conteq_cells_seq
	{
	  \msg_warning:nn{conteq}{ignoreddata}
	}
    \tl_put_right:Nn #1 {#3}
  }
\cs_generate_variant:Nn \__conteq_print_line:Nnn { Nno, NVn }
%    \end{macrocode}
% \end{macro}

%
% \begin{environment}{conteq}
%
% The main environment of the package.
%    \begin{macrocode}
\NewEnviron{ conteq }[1][ \l__conteq_default_layout_tl ]{
  \tl_set:NV \l__conteq_body_tl \BODY
  
  \tl_if_blank:oT \l__conteq_body_tl
    {
      \msg_warning:nn{conteq}{empty}
    }
%    \end{macrocode}

% Figure out the layout to use\ldots
%    \begin{macrocode}
  \tl_set:Nn \l__conteq_layout_tl { #1 }
%    \end{macrocode}
% and set the various functions accordingly, if the layout exists.
%    \begin{macrocode}
  \cs_if_exist:cTF { \c__conteq_prefix_tl _ \l__conteq_layout_tl _ begin: }
    {
      \clist_map_inline:nn{ begin:, lhs:n,rhs_only:n, rhs_expl:nn, nl:, end: }
        {
          \cs_set_eq:cc { __conteq_ ##1 }{ \c__conteq_prefix_tl _ \l__conteq_layout_tl _ ##1 }
        }
    }{
      \msg_critical:nnx{conteq}{undefined layout}{\l__conteq_layout_tl}
    }
%    \end{macrocode}

% Split the body into individual lines.
%    \begin{macrocode}
  \seq_set_split:NnV \l__conteq_lines_seq { \\ } \l__conteq_body_tl

  \tl_clear:N \l__conteq_result_tl
%    \end{macrocode}

% If there is only one line, simply print it.
%    \begin{macrocode}

  \int_case:nnF { \seq_count:N \l__conteq_lines_seq }
    {
      {1}
        {
          \tl_put_right:Nx \l__conteq_result_tl { \exp_not:o \__conteq_begin: }
          \tl_put_right:NV \l__conteq_result_tl \l__conteq_body_tl
          \tl_put_right:Nx \l__conteq_result_tl { \exp_not:o \__conteq_end: }
        }
    }
    {
%    \end{macrocode}

% Otherwise extract the head and the last line, and print each line using |\__conteq_print_line:nn|
%    \begin{macrocode}
      \seq_pop_left:NN \l__conteq_lines_seq \l__conteq_head_tl
      \seq_pop_right:NN \l__conteq_lines_seq \l__conteq_lastline_tl

      \tl_put_right:Nx \l__conteq_result_tl { \exp_not:o \__conteq_begin: }
      \tl_put_right:Nx \l__conteq_result_tl
        {
          \__conteq_args_once:NV \__conteq_lhs:n \l__conteq_head_tl
        }
      \seq_map_inline:Nn \l__conteq_lines_seq
        {
          \__conteq_print_line:Nno \l__conteq_result_tl { ##1 } { \__conteq_nl: }
        }
      \__conteq_print_line:NVn \l__conteq_result_tl \l__conteq_lastline_tl {} 
      \tl_put_right:Nx \l__conteq_result_tl { \exp_not:o \__conteq_end: }
    }
    % Use this for debugging
    %\tl_show:N \l__conteq_result_tl
    \tl_use:N \l__conteq_result_tl
  }
%    \end{macrocode}
%\end{environment}
%
%    \begin{macrocode}
\endinput
%    \end{macrocode}

%
% \iffalse
%</package>
% \fi
%
% \Finale
\endinput