Debian Codegraph @

texmf-dist/doc/latex/protex/README

raw
1 \ifx\documentstyle\UnDef\else\documentstyle{report}\begin{document}\fi
2 \input ProTex.sty \def\CodeDel{{<<<-}{->>>}}
3 \AlProTex{tex,<<<>>>,|,title,ClearCode} \<\><<<-
4
5 ProTex is Eitan Gurari's literate programming environment for TeX.
6 His monumental tex4ht package (http://ctan.org/pkg/tex4ht) uses it.
7
8 FILES
9 =====
10 ProTex.sty -- Basic environment for literate programming
11 AlProTex.sty -- Adjustments to ProTeX
12 README -- Introduction and examples for main features
13 of (Al)ProTeX (this file)
14
15 MANUAL
16 ======
17 Eitan Gurari, TeX and LaTeX: Drawing and Literate Programming.
18 McGraw-Hill, 1994. ISBN 0-07-025208-4 / 0-07-911616-7
19 The dratex package described there is at http://ctan.org/pkg/dratex.
20 Various ProTex examples are in that package's file Examples.tex.
21
22 Table of Contents
23 =================
24 A. A Basic Literate Programming Environment
25 B. Other Basic Literate Programming Environments
26 C. Environments for Multi-Module Programs
27 D. Code and Documentation Produced from Same Specification
28 E. Examples B, C, D
29 F. License
30
31 The code for the examples can be extracted from this file
32 by renaming this file to LitProg.tex and compiling the
33 file with TeX or LaTeX (the Makefiles for Example C, however,
34 must be extracted mannually). The compilation puts the code
35 in files progA.tex, progB.tex, progC.tex, main.tex,
36 aux.tex, macros.tex, progC.sty, and progD.tex.
37
38 A. A Basic Literate Programming Environment
39 ===========================================
40
41 \input ProTex.sty
42 \AlProTex{EXT,<<<>>>,title,list,[],`}
43
44 This environment adds to (La)TeX the following commands for handling code.
45
46 a. Definitions of code fragments \<code title\><<<
47 code fragment>>>
48
49 b. References to code segments `<code title`>
50
51 c. Output files of code \OutputCode[EXTENSION]\<code title\>
52 `[EXTENSION]' is optional
53 when EXTENSION = EXT
54
55 d. References to the character `
56 within code fragments ``
57
58 Warning: Do not use 2 or more consecutive blank characters
59 in `<code title`>
60
61 ------- Example A: Single-Module C Program ----------------------------------
62 ------- Put in file progA.tex and compile with LaTeX ------%->>>\<progA\><<<-
63
64 \documentstyle{book}
65
66 \input ProTex.sty
67 \AlProTex{c,<<<>>>,title,list,[],`}
68
69 \begin{document}
70
71 \<body\><<<
72 printf("\n ``hello everybody``");
73 >>>
74
75 \<prog A\><<<
76 `<include`>
77 main()
78 {
79 `<body`>
80 }
81 >>>
82
83 \<include\><<<
84 #include <stdio.h>
85 >>>
86
87 \<body\><<<
88 printf("\n");
89 >>>
90
91 \OutputCode\<prog A\> \end{document}
92
93 ------------ End Example A -----------------%->>>\OutputCode\<progA\>\<\><<<-
94
95
96 B. Other Basic Literate Programming Environments
97 ================================================
98
99 a. Framing of code fragments can be requested with the option [[]]
100 in \AlProTex, e.g.,
101
102 \AlProTex{EXT,<<<>>>,title,list,[],`,[[]]}
103
104 b. Framing of code titles can be avoided by removing the option []
105 from \AlProTex, e.g,
106
107 \AlProTex{EXT,<<<>>>,title,list,`}
108
109 c. The characters @, |, and ? can also act as escape characters within
110 code fragments (e.g., replace all appearances of ` with @ in Example A).
111
112 d. Line breaks within listing of code can be obtained with the command `NL%
113
114 e. Comments within listing of code can be obtained with commands of
115 the form `%...`%
116
117 f. The choice of code-delimiters <<< and >>> can be changed by placing
118 a command of the form \def\CodeDel{{open delimiter}{close delimiter}}
119 before \AlProTex.
120
121 g. The following command requests that future \OutputCode commands identify
122 the titles of the different code fragments. (Other information, like
123 page numbers in documents and line numbers in source files, can also
124 be incorporated.)
125
126 \Comment{open-comment delimiter}{close-comment delimiter}
127
128 PLEASE EXAMINE AND RUN EXAMPLE B
129
130
131 C. Environments for Multi-Module Programs
132 =========================================
133
134 Multi-module programs can be divided into independent (La)TeX files
135 that can be compiled either separately or together. For instance,
136 in Example C
137
138 1. Three modules (main, aux, and macros) are defined independently
139 (in main.tex, aux.tex, macros.tex) and are connected through a
140 joint root file (progC.tex). The different files import a
141 common style file (progC.sty).
142
143 progC.tex Root file
144 main.tex LaTeX file for first module
145 aux.tex LaTeX file for second Module
146 macros.tex LaTeX file for third module
147 progC.sty Style file (imported by each of the above files)
148 make files Unix management programs
149
150 2. The files can be compiled separately (latex main.tex, latex aux.main,
151 latex macros.tex) or jointly (latex progC.tex).
152
153 3. To allow quick generation, compilation, and linkage of C-code files
154 (see, e.g., the make files), joint compilations do not revise code
155 files.
156
157 4. Separate compilations use chapter/section/subsection/subsubsection/page
158 numbers that are collected in joint compilations.
159
160 5. In joint compilations, each module ends with an index to its code titles.
161
162 6. To allow nonrelated identical titles in different modules, and to
163 save on memory, previous definitions of code fragments are deleted at
164 the start of each module.
165
166 PLEASE RUN AND EXAMINE EXAMPLE C
167
168 D. Code and Documentation Produced from Same Specification
169 ==========================================================
170
171 Example D demonstrates how math formulas and their code can be generated
172 from joint specifications.
173
174 PLEASE EXAMINE AND RUN EXAMPLE D
175
176
177
178 E. Examples B, C, D
179 ===================
180
181 ------- Example B: Variant of Example A -------------------------------------
182 ------- Put in file progB.tex and compile with LaTeX ------%->>>\<progB\><<<-
183
184 \documentstyle{book}
185
186 \input ProTex.sty
187 \def\CodeDel{{}{////}}
188 \Comment{
189 /*}{*/
190 }
191 \AlProTex{c,<<<>>>,title,list,[],`}
192
193 \begin{document}
194
195 \<body\>
196 printf("\n ``hello everybody`` within a long`NL% line of text");
197 ////
198
199 \<prog B\>
200 `<include`>
201 main()
202 {
203 `<body`> `%a comment`%
204 }
205 ////
206
207 \<include\>
208 #include <stdio.h>
209 ////
210
211 \<body\>
212 printf("\n");
213 ////
214
215 \OutputCode\<prog B\> \end{document}
216 ------------ End Example B -----------------%->>>\OutputCode\<progB\>\<\><<<-
217
218
219
220 ------------ Example C: Multi-Module C Program ------------------------------
221 ............ File progC.tex ...............................%->>>\<progC\><<<-
222
223 \def\Root{}
224 \input progC.sty
225
226 \INPUT{main} % import first module
227
228 \INPUT{aux} % import second module
229
230 \INPUT{macros} % import third module
231
232 \end{document}
233 ............ End file progC.tex .............%->>>\OutputCode\<progC\>\<\><<<-
234
235 ............ File main.tex ..................................%->>>\<main\><<<-
236 \input progC.sty
237
238 \<include files\><<<
239 #include "aux.h"
240 #include "macros.h"
241 #include <stdio.h>
242 >>>
243
244 \chapter{Outline}
245
246 All the code in this module is included in file main.c.
247
248 \section{The Main Program}
249
250 This program does not handle the case in which the input consists just
251 of one 0.
252
253 \<main.c\><<<
254 int main()
255 {
256 `<main's vars`>
257 `<read input values, count them, and record their sum`>
258 `<compute and output the average of the input values`>
259 exit(0);
260 }
261 >>>
262
263 \chapter{The Actual Actions}
264
265 \section{Process the Input Values}
266
267 The input must end with 0.
268
269 \<read input values, count them, and record their sum\><<<
270 initialize_sum_count();
271 WHILE TRUE DO
272 printf("\n Value (0 to stop)? ");
273 scanf("%d", &value);
274 IF value == 0 THEN break; FI
275 add_value(value);
276 END
277 >>>
278
279 \<main's vars\><<<
280 int value;
281 >>>
282
283 \section{Find the Result and Print It Out}
284
285 \<compute and output the average of the input values\><<<
286 result = get_average();
287 printf("\n Average = %f\n", result);
288 >>>
289
290 \<main's vars\><<<
291 double result;
292 >>>
293
294 \BYE{ \OutputCodE\<main.c\> }
295 ............ End file main.tex ..............%->>>\OutputCode\<main\>\<\><<<-
296
297 ............ File aux.tex ...................................%->>>\<aux\><<<-
298 \input progC.sty
299
300 \chapter{Reusable Functions}
301
302 \section{Shared Variable}
303
304 The following code is included in reusable.c.
305
306 \<vars\><<<
307 int count;
308 double sum;
309 >>>
310
311 \section{Setting Initial Conditions}
312
313 The following code is included in reusable.h.
314
315 \<export functions\><<<
316 void initialize_sum_count();
317 >>>
318
319 The following code is included in reusable.c.
320
321 \<functions\><<<
322 void initialize_sum_count()
323 {
324 count = 0;
325 sum = 0.0;
326 }
327 >>>
328
329 \section{Processing New Values}
330
331 \<export functions\><<<
332 void add_value();
333 >>>
334
335
336 \<functions\><<<
337 void add_value(value)
338 {
339 count++;
340 sum += value;
341 }
342 >>>
343
344
345 \section{Computing the Average}
346
347 \<export functions\><<<
348 double get_average();
349 >>>
350
351 \<functions\><<<
352 double get_average()
353 {
354 return sum / count;
355 }
356 >>>
357
358 Old files reusable.h and reusable.c are replaced when this
359 file (i.e., reusable.tex) is compiled, but not when progC.tex is compiled.
360
361 \BYE{ \OutputCodE\<aux.h\>
362 \OutputCodE\<aux.c\> }
363 ............ End file aux.tex ................%->>>\OutputCode\<aux\>\<\><<<-
364
365 ............ File macros.tex .............................%->>>\<macros\><<<-
366 \input progC.sty
367
368 \chapter{C Macros}
369
370 Algol-oriented flavor.
371
372 \<macros.h\><<<
373 #define TRUE 1
374 #define IF if (
375 #define THEN ) {
376 #define ELSE }else{
377 #define ELSEIF }else if(
378 #define FI }
379 #define WHILE while(
380 #define FOR for(
381 #define WITH ;
382 #define STEP ;
383 #define DO ){
384 #define BEGIN {
385 #define END }
386 #define CASE switch(
387 #define OF ){
388 >>>
389
390 \BYE{ \OutputCodE\<macros.h\> }
391 ............ End file macros.tex ..........%->>>\OutputCode\<macros\>\<\><<<-
392
393 ............ File progC.sty .....................%->>>\ClearCode\<progC\><<<-
394 \ifx\Code\undefined
395
396 \hbadness=10000 \vbadness=10000 \hfuzz=99in \vfuzz=99in
397 \ifx \documentstyle\UnDef
398 %load private style
399 \else
400 \documentstyle{book}
401 \let\cleardoublepage=\clearpage
402 \begin{document}
403 \fi
404 \input ProTex.sty
405
406 \ifx \Root\UnDef
407 \AlProTex{c,<<<>>>,[],list,title,`,_^,ClearCode}
408 \else
409 \AlProTex{c,<<<>>>,[],list,title,`,_^,ClearCode,ShowIndex}
410 \let\SvShowIndex=\ShowIndex
411 \def\ShowIndex{\let\ShowIndex=\SvShowIndex}
412 \fi
413 \def\CodeId#1#2{#1.#2-pg:\the\pageno-ln:\the\inputlineno}
414
415 \let\FileName=\jobname
416 \def\INPUT#1{
417 \def\FileName{#1}
418 \immediate\openout15=#1.pg
419 \immediate\write15{%
420 \string \pageno=\the\pageno\string \relax
421 \string \csname\space c@page\string \endcsname =
422 \expandafter\the\csname c@page\endcsname\string \relax
423 \string \csname\space c@chapter\string \endcsname =
424 \expandafter\the\csname c@chapter\endcsname\string \relax
425 \string \csname\space c@section\string \endcsname =
426 \expandafter\the\csname c@section\endcsname\string \relax
427 \string \csname\space c@subsection\string \endcsname =
428 \expandafter\the\csname c@subsection\endcsname\noexpand\relax
429 \string \csname\space c@subsubsection\string \endcsname =
430 \expandafter\the\csname c@subsubsection\endcsname\noexpand\relax
431 }
432 \immediate\closeout15
433 \input #1
434 }
435
436 \ifx \documentstyle\UnDef \errmessage{--- This is a LaTeX file ---}\fi
437 \title{Averaging Numbers}
438 \date{2 October 1994}
439 \maketitle
440 \tableofcontents
441
442 \openin15=\jobname.pg
443 \ifeof15 \else \input \jobname.pg \fi
444 \closein15
445 \def\OutputCodE\<#1.#2\>{%
446 \csname :DoName\endcsname\def{#1}{\<#1.#2\>}%
447 \OutputCode[#2]\<#1\>}
448 \let\xxxOutputCode=\OutputCodE
449 \def\xxOutputCode\<#1.#2\>{%
450 \openin15=#1.#2
451 \ifeof15 \xxxOutputCode\<#1.#2\>\fi
452 \closein15 }
453 \fi
454
455 \ifx \BYE\undefined \def\BYE#1{#1\end{document}}
456 \else \def\BYE#1{{\let\OutputCodE=\xxOutputCode #1}%
457 \ShowIndex\ClearCode}\fi
458
459 \ifx\Root\NotDef
460 \vfill\break\noindent\hfil\underbar{\bf INPUT FILE: \FileName .tex}
461
462 \<\FileName.h\><<<
463 `<export define`>
464 `<export types`>
465 `<export vars`>
466 `<export functions`>
467 >>>
468
469 \<\FileName.c\><<<
470 `<include files`>
471 `<define`>
472 `<types`>
473 `<header functions`>
474 `<vars`>
475 `<functions`>
476 >>>
477
478 \else \let\Root=\NotDef \fi
479
480 \Comment{
481 /*}{*/
482 }
483
484 ............ End file progC.sty ........%->>>\OutputCode[sty]\<progC\>\<\><<<-
485
486 ............ Make file 1 .....................................................
487 # make -fS make.1
488 # call LaTeX and C for revised files
489
490 all : progC
491 progC
492
493 main.c: main.tex
494 rm -f main.llog
495 latex main > main.llog
496
497 aux.c: aux.tex
498 rm -f aux.llog
499 latex aux > aux.llog
500
501 aux.h: aux.tex
502 rm -f aux.llog
503 latex aux > aux.llog
504
505 macros.h: macros.tex
506 rm -f macros.llog
507 latex macros > macros.llog
508
509 aux.o: aux.c aux.h macros.h
510 cc -c aux.c
511
512 main.o: main.c aux.h macros.h
513 cc -c main.c
514
515 progC : main.o aux.o
516 cc -o progC main.o aux.o
517
518 ............ End make file 1 ................................................
519 ............ Make file 2 ....................................................
520 # make -fS make.2
521 # call LaTeX in background for revised files
522
523 all : main.c aux.c macros.h
524
525 main.c: main.tex
526 rm -f main.llog
527 latex main > main.llog &
528
529 aux.c: aux.tex
530 rm -f aux.llog
531 latex aux > aux.llog &
532
533 macros.h: macros.tex
534 rm -f macros.llog
535 latex macros > macros.llog &
536
537 ............ End make file 2 .................................................
538 ------------ End Example C -------------------------------------------------
539
540
541
542 ------------ Example D: Joint Specification --------------%->>>\<progD\><<<-
543 \documentstyle{report}
544 \input ProTex.sty
545 \AlProTex{code,<<<>>>,[],title,list}
546 %......................................................................
547 \def\Formula#1#2{\ifx#1\UnDefined\else\immediate\write16
548 {--- warning --- Redefine equation for \string#1 ---}\fi
549 \gdef#1{%
550 \expandafter\ifx \csname :Tb\endcsname\relax
551 \ifmmode \InMath #2
552 \else \vskip\smallskipamount\noindent\hfil
553 $ \let\InText=\relax \InMath #2 $%
554 \vskip\smallskipamount
555 \fi
556 \else #2\fi}%
557 }
558 \let\PrevModifyShowCode=\ModifyShowCode
559 \def\ModifyShowCode{\InCode \PrevModifyShowCode }
560 \let\PrevModifyOutputCode=\ModifyOutputCode
561 \def\ModifyOutputCode{\InCode \PrevModifyOutputCode }
562 \def\InCode{%
563 \def\divide##1##2{((##1) / (##2))}%
564 \def\sqrt##1{SQRT(##1)}%
565 \def\cdot{ * }%
566 \def\sin##1{(sin(##1))}%
567 \def\power##1##2{(##1)**(##2)}%
568 \def\NewLn{\csname :Ln\endcsname}%
569 }
570 \def\InMath{%
571 \def\divide##1##2{\ifx\LaTeX\undefined{##1\over##2}\else
572 {\frac{##1}{##2}}\fi}%
573 \def\power##1##2{##1^{##2}}%
574 \def\NewLn{\ifinner
575 \ifx\InText\realx \else \hfil\break\null\hfil \fi
576 \else $$$$\InMath \fi}%
577 }
578 %......................................................................
579 \begin{document}
580
581 Metalanguage: {\tt\string\divide}, {\tt\string\sqrt}, {\tt\string\cdot},
582 {\tt\string\sin}, {\tt\string\power}, {\tt\string\NewLn}.
583
584 \Formula\f{\divide{x}{\sqrt{1+\power{x}{2}}}\cdot
585 \NewLn \sin{\divide{y}{x}}}
586
587 Formula \f{} and its code
588
589 \<formula\><<<
590 w = \f
591 >>>
592
593 Same formula in math modes $\f$ and $$\f$$.
594
595 \OutputCode\<formula\> \end{document}
596
597 ------------ End Example D -----------%->>>\OutputCode\<progD\>\end{document}
598
599 F. License
600 ==========
601 % Copyright 2016 TeX Users Group
602 % Copyright 1990-1999 Eitan M. Gurari
603 %
604 % This program can redistributed and/or modified under
605 % the terms of the LaTeX Project Public License
606 % Distributed from CTAN archives in directory
607 % macros/latex/base/lppl.txt; either version 1 of the
608 % License, or (at your option) any later version.
609 %
610 % This work has the LPPL maintenance status "maintained".
611 %
612 % The Current Maintainer of this work
613 % is the TeX4ht Project <http://tug.org/tex4ht>.