-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmemoize.sty
923 lines (852 loc) · 31.4 KB
/
memoize.sty
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
% memoize.sty
%
% Memoize is a LaTeX package for externalization of graphics and memoization of
% compilation results in general.
%
% Copyright (c) 2020 Saso Zivanovic <[email protected]>
% (Sa\v{s}o \v{Z}ivanovi\'{c})
%
% This work may be distributed and/or modified under the conditions of the
% LaTeX Project Public License, either version 1.3 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.
%
% This work is not yet published at CTAN.
%
% This work consists of the following files:
% - memoize.sty
% - memomanager.py
% - nomemoize.sty
% - memoize-tikz.tex
% - README.md
% and
% - xparse-arglist.sty
%
\ProvidesPackage{memoize}[2020/07/17 Memoization, including externalization]
\RequirePackage{pgfopts} % pgfkeys-based package options
% \RequirePackage{pgfkeys} % for UI, loaded by pgfopts
\RequirePackage{etoolbox} % generally useful
\RequirePackage{graphicx} % for including externalized graphics
\RequirePackage{environ} % for automemoization of environments:
\RequirePackage{pdftexcmds} % unified access to pdf primitives across engines,
% notably to md5sum in luatex
% The def is here, because we will redefine this for xetex.
\def\mmz@if@roughly@equal#1#2{%
\ifpdfabsdim\dimexpr#1-#2\relax<\mmz@tolerance\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
% Some engine-specific stuff we have to define despite pdftexcmds.
\ifdef\pdftexversion{%
\def\mmz@latex@binary{pdflatex}%
}{%
% Only pdftex seems to have these:
\def\pdfhorigin{1true in}%
\def\pdfvorigin{1true in}%
\ifdef\XeTeXversion{%
\def\mmz@latex@binary{xelatex}%
% pdftex and luatex profide \quitvmode (replacement for \leavevmode, see pdftex
% manual 8.21), but xetex doesn't.
\let\quitvmode\leavevmode
% xetex does not have \if(pdf)absdim
\def\mmz@if@roughly@equal#1#2{%
\begingroup
\@tempdima=\dimexpr#1-#2\relax
\ifdim\@tempdima<0pt \@tempdima=-\@tempdima\relax\fi
\expandafter\expandafter\expandafter\endgroup
\ifdim\@tempdima<\mmz@tolerance\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
}{%
\ifdef\luatexversion{%
% Is there a way to shipout a memoized page to a new pdf, thereby
% avoiding the separate splitting stage?
\def\mmz@latex@binary{lualatex}%
\let\ifpdfabsdim\ifabsdim
\let\pdfpagewidth\pagewidth
\let\pdfpageheight\pageheight
}{%
\PackageError{memoize}{Support for this TeX engine is not implemented.}{}%
}%
}%
}
% Registers and such
\newif\ifmemoize
\newif\ifmemoizing
\newif\ifmmz@readonly
\newif\ifmmz@temp
\newwrite\mmz@out
\newbox\mmz@box
\newcount\mmz@realpage
\newtoks\mmz@temptoks
\newif\ifmmz@loadonly
\newif\ifmmz@light
\newif\ifmmz@manual
\newif\ifmmz@shipout@only@memoized
\newif\ifmmz@cancel
\pgfqkeys{/memoize/package options}{
loadonly/.is if=mmz@loadonly, % disabled, no automemoization
light/.is if=mmz@light, % without xparse
manual/.is if=mmz@manual,
}
\ProcessPgfPackageOptions{/memoize/package options}
% Some useful stuff.
\def\mmz@valueof#1{\pgfkeysvalueof{/memoize/#1}}
\def\apptotoks#1#2{\expandafter#1\expandafter{\the#1#2}}
% Copied from pgfutil-common.tex (and renamed), for self-containment
\newif\ifmmzutil@in@
\def\mmzutil@in@#1#2{%
\def\mmzutil@in@@##1#1##2##3\mmzutil@in@@{%
\ifx\mmzutil@in@##2\mmzutil@in@false\else\mmzutil@in@true\fi}%
\mmzutil@in@@#2#1\mmzutil@in@\mmzutil@in@@}
\def\mmz@split@filename#1#2#3{%
% input: #1 = filename (absolute, relative, with or without path component)
% output: #2 = path, #3 = name
\edef\pgfkeyscurrentkey{#1}%
\expandafter\mmzutil@in@\expandafter/\expandafter{\pgfkeyscurrentkey}%
\ifmmzutil@in@
\pgfkeys@split@path
\edef#2{\pgfkeyscurrentpath/}%
\let#3\pgfkeyscurrentname
\else
\def#2{}%
% \edef here gets rid of \@empty used in \mmz@updatefilenames.
\edef#3{#1}%
\fi
}
%%%%%%%%%%%%%%%%%%%%% UI
\def\memoizeset#1{\pgfqkeys{/memoize}{#1}}
% todo: preamble vs. all-the-time options --- redefine \memoizeset to another path!
\memoizeset{%
% When the TeX job starts, \jobname contains the basename of the PDF that
% will be created. If we set \jobname (using the below key) to some other
% filename (without ".tex" suffix), we will fool LaTeX into using and
% writing the auxiliary files of the "real job". By default, the real job
% equals \jobname, i.e. we memoize "inline".
%
% Include the path, but don't add ".tex" here.
jobname/.estore in=\jobname,
%
% After each compilation, "\primitive\jobname.mmz" will contain the list of memoized
% stuff. On the next run, tex will split the memo PDFs off the big PDF we
% have generated in the first compilation. (The second step can also be
% achieved by the external tool "memoize-split.py")
%
% The keys "memo filename prefix/suffix" tell the package where to store the
% separate externalized pictures (memos). The prefix can contain the path,
% which must be relative to real job folder.
%
% For example, say "memo filename prefix={\jobname.memo.dir/}" --- DON'T
% FORGET THE SLASH AND THE BRACES --- to store memos in the
% "\jobname.memo.dir" folder --- where \jobname is the name of the real job.
% You need to create the folder by hand.
%
% Default: We store the externalized files in the same folder (filename of
% form \jobname.<md5sum>.memo), because we don't know if we can write
% elsewhere.
memo filename prefix/.initial={\mmz@jobbasename.},
memo filename suffix/.initial={.memo},
% A shortcut for a .memo.dir: without an argument, defaults to the job name,
% but any base name can be given.
memo dir/.style={memo filename prefix={#1.memo.dir/}},
memo dir/.default=\mmz@jobbasename,
%
% System call for embedded LaTeX compilations. The engine (pdftex, xetex
% etc.) is chosen automatically, but can be changed by "latex binary" key.
latex binary/.store in=\mmz@latex@binary,
latex system call/.initial={\mmz@latex@binary\space
-halt-on-error
-interaction=batchmode
\mmz@valueof{latex system call options}\space
-jobname "\detokenize\expandafter{\mmz@syscall@jobname}"
"\detokenize\expandafter{\mmz@syscall@commands}"},
% Additional options for the LaTeX system call
latex system call options/.initial={},
%
% enable and disable are defined later
%
% When we say "readonly" (or "readonly=true"), we will use whatever is
% already memoized, but not memoize any new stuff.
readonly/.is if=mmz@readonly,
% padding (for dealing with tikz overlays, \rlap and \llap etc.)
padding left/.store in=\mmz@padding@left,
padding right/.store in=\mmz@padding@right,
padding top/.store in=\mmz@padding@top,
padding bottom/.store in=\mmz@padding@bottom,
padding x/.style={padding left=#1, padding right=#1},
padding y/.style={padding top=#1, padding bottom=#1},
padding/.style={padding x=#1, padding y=#1},
default padding/.style={padding x=\pdfhorigin, padding y=\pdfvorigin},
default padding,
% todo: page padding --- for "remember picture"
%
if readonly/.code 2 args={
\ifmmz@readonly\pgfkeysalso{#1}\else\pgfkeysalso{#2}\fi},
if memoize/.code 2 args={
\ifmemoize\pgfkeysalso{#1}\else\pgfkeysalso{#2}\fi},
if memoizing/.code 2 args={
\ifmemoizing\pgfkeysalso{#1}\else\pgfkeysalso{#2}\fi},
if manual/.code 2 args={
\ifmmz@manual\pgfkeysalso{#1}\else\pgfkeysalso{#2}\fi},
% This might need to go, as the regular output might define some stuff needed
% in the memoized output, see https://github.com/sasozivanovic/memoize/issues/1
shipout only memoized/.is if=mmz@shipout@only@memoized,
}
\def\mmz@updatefilenames{%
\mmz@split@filename
\jobname
\mmz@jobpath
\mmz@jobbasename
\mmz@split@filename
% Split the "memo filename prefix" (relative to the job path) into path and
% basename. The "\noexpand\@empty" below makes sure that \pgfkeys@split@path
% will work when "memo filename prefix" specifies a directory, i.e.\ when it
% ends with "/" --- this is the case when we use "memo dir".
{\mmz@jobpath\mmz@valueof{memo filename prefix}\noexpand\@empty}%
\mmz@memo@path
\mmz@memo@basenameprefix
}
% Manual memoization: \memoize[memoization key]{the code to be memoized}. In a
% manual invocation, the optional argument should usually be unnecessary. So,
% #1 is the memoization key (what md5sum is computed from), and #2 is the code
% to be executed for memoization.
%
% we might need to redefine \memoize from "nomemoize.sty":
\let\memoize\relax
\newcommand\memoize[2][]{%
\quitvmode
\ifmemoize
\ifstrempty{#1}{%
\edef\mmz@id{\expandafter\pdf@mdfivesum\expandafter{\detokenize{#2}}}%
}{%
\edef\mmz@id{\expandafter\pdf@mdfivesum\expandafter{\detokenize{#1}}}%
}%
\mmz@ifmemoized{%
\ifmmz@@disabled
\let\mmz@next\memoize@compile
\else
\let\mmz@next\mmz@use@memoized
\fi
}{%
\ifmmz@readonly
\let\mmz@next\memoize@compile
\else
\let\mmz@next\mmz@memoize
\fi
}%
\else
\let\mmz@next\memoize@compile
\fi
\mmz@next{#2}%
% In all cases, we are left with \mmz@box.
\box\mmz@box
}% todo: flexibility, optimization
\let\nomemoize\relax
\newcommand\nomemoize[1]{{\memoizedisable#1}}
%%%%%%%%%%%%%%%%%%%% THE CORE
\long\def\memoize@compile#1{%
% We compile in a \hbox, so that the situation is the same as when
% memoization is active --- both in terms of the typeset output and the
% content of registers etc.
\setbox\mmz@box=\hbox{\memoizefalse#1}%
}
% \mmz@ifmemoized{true code}{false code}
% Is there memoized data for \mmz@id? If yes, leave it in various macros (see
% \memoized) and execute {true code}. If no, \mmz@@filename will be undefined
% and we run {false code}.
\def\mmz@ifmemoized{%
% If inputing <md5sum>.memo sets the filename, we found it!
\let\memoized\memoized@search
\undef\mmz@@filename
\@input{\mmz@memo@filename\mmz@id}%
\let\mmz@next\@secondoftwo % not found
\ifmmz@@disabled
% disabled counts as found, so that we don't try to memoize again
\let\mmz@next\@firstoftwo
\else
\ifdef\mmz@@filename{%
% Well ... if that file exists. First, we prefix the correct folder:
\epreto\mmz@@filename\mmz@memo@path
\IfFileExists{\mmz@@filename}%
{\let\mmz@next\@firstoftwo}% yes!
{}%
}{}%
\fi
\mmz@next
}
% We use the "graphics" package, so we don't have to deal with various TeX
% engines. And then, we actually use "graphicsx", because I have no idea how
% trim the padding without it.
\def\mmz@tolerance{0.01pt}
\long\def\mmz@use@memoized#1{%
\PackageInfo{memoize}{Using \mmz@@id\space}%
\setbox\mmz@box=\hbox{\lower\mmz@@dp\hbox{\includegraphics[trim=%
{\mmz@@padding@left} {\mmz@@padding@bottom}
{\mmz@@padding@right} {\mmz@@padding@top}]{\mmz@@filename}}}%
% I have no idea why this doesn't work.
% \setbox\mmz@box=\hbox{\lower\mmz@@dp\hbox{\includegraphics
% [\mmz@@padding@left,\mmz@@padding@bottom]%
% [\dimexpr\mmz@@padding@left+\mmz@@wd\relax,%
% \dimexpr\mmz@@padding@bottom+\mmz@@dp+\mmz@@ht\relax]%
% {\mmz@@filename}}}%
% Safety net: Warn if we include a page of unexpected dimensions.
\mmz@tempfalse
\mmz@if@roughly@equal{\mmz@@wd}{\wd\mmz@box}{%
\mmz@if@roughly@equal{\mmz@@ht}{\ht\mmz@box}{%
% no need to check \dp, we have just set it
\mmz@temptrue
}{}}{}%
\ifmmz@temp\else\mmz@use@memoized@warning\fi
\immediate\write\mmz@out{\noexpand\usedmemoized{\mmz@@id}%
{filename={\mmz@@filename}, page=\mmz@@page,
wd=\mmz@@wd, ht=\mmz@@ht, dp=\mmz@@dp,
padding left=\the\dimexpr\mmz@@padding@left,
padding bottom=\the\dimexpr\mmz@@padding@bottom,
padding right=\the\dimexpr\mmz@@padding@right,
padding top=\the\dimexpr\mmz@@padding@top%
}%
\@percentchar
}%
}
\def\mmz@use@memoized@warning{%
\PackageWarning{memoize}{Wrong dimensions of externalized graphics \mmz@@id.
Did something go wrong?}}
% Memoize and leave the box in \mmz@box.
\long\def\mmz@memoize#1{%
\PackageInfo{memoize}{Memoizing \mmz@id\space}%
% Typeset the code.
\setbox\mmz@box=\hbox{%
\memoizefalse
\memoizingtrue
% Detect whether memoization of this particular picture (id) should be disabled,
% e.g. if \pdfsavepos was used
\global\mmz@cancelfalse
\mmz@cancel@setup
#1%
}% todo: smuggle \mmz@toks and \mmz@global@toks, once we have them!
\ifmmz@cancel\mmz@memoize@no\else{\mmz@memoize@yes}\fi
}
\def\mmz@memoize@yes{%
% So that the user can define padding in terms of the size of the
% externalized graphics:
\edef\width{\the\wd\mmz@box}%
\edef\height{\the\ht\mmz@box}%
\edef\depth{\the\dp\mmz@box}%
% Set the dimensions of the page: \mmz@box + padding
\hoffset\dimexpr\mmz@padding@left - \pdfhorigin\relax
\voffset\dimexpr\mmz@padding@top - \pdfvorigin\relax
\pdfpagewidth\dimexpr\mmz@padding@left + \wd\mmz@box + \mmz@padding@right\relax
\pdfpageheight\dimexpr\mmz@padding@top + \ht\mmz@box +
\dp\mmz@box+ \mmz@padding@bottom\relax
%
% If we are compiling by xelatex, using \documentclass{minimal} and if the
% first \shipout is our externalization shipout, we get a weird error:
% all the pages of the document have the same size as our first
% shipout. Our own shipouts have the size set correctly. Pdflatex does
% not exhibit this behaviour.
%
% Debug PDF: \pdfcompresslevel=0 \pdfobjcompresslevel=0
%
% Use the original \shipout here, otherwise we can get in trouble with other
% packages like pdfpages. Let's just hope that (i) some other package
% doesn't count shipouts as well (then, they are in trouble), or (ii) doesn't
% use the primitive \shipout (then, we are in trouble).
\global\advance\mmz@realpage1
\pdf@primitive\shipout\copy\mmz@box
% Store memoized data and the link to the .pdf in to .mmz file.
\immediate\write\mmz@out{\noexpand\memoized{\mmz@id}%
{filename={\[email protected]}, page=\the\mmz@realpage,
wd=\the\wd\mmz@box, ht=\the\ht\mmz@box, dp=\the\dp\mmz@box,
% order as in graphicx:
padding left=\the\dimexpr\mmz@padding@left,
padding bottom=\the\dimexpr\mmz@padding@bottom,
padding right=\the\dimexpr\mmz@padding@right,
padding top=\the\dimexpr\mmz@padding@top}%
\@percentchar
}%
}
\def\mmz@memoize@no{%
\immediate\write\mmz@out{\noexpand\memoized{\mmz@id}%
{disable}%
\@percentchar
}%
}
%%%%%% Creating and reading .mmz and .memo files
% A simple .memo file consists of a call to \memoized containing the
% information memoized at a call to \memoize. Crucially, this information
% includes the "link" to the externalized picture (todo: picture*s*). Like
% this:
%
% \memoized{md5sum}{pdf filename}{pdf page number}{width}{height}{depth}
%
% \item todo: context
% \item todo: code, input file, input line (if verbose) --- maybe check
% currfile package?
%
% The pdf filename is a path to the pdf relative to the location of the .memo
% file.
%
% A complex .memo file will contain a series of calls to \memoized. A .mmz
% file is really a complex .memo file. The different suffix indicates the
% different function. .mmz files are transitory, the idea is to split them
% into several .memo files, one for each \memoize(d). ".memo" files are
% permanent, their filenames contain md5sum.
%
% Memoization always produces a complex .memo file (.mmz), which is then split
% into several "<md5sum>.memo" files, each with a corresponding
% "<md5sum>.memo.pdf". However, the idea is to also support inclusion of
% externalized graphics from a single .memo -- .memo.pdf pair (probably slower,
% but easier to share). This is why \memoized@search below skips entries with a
% wrong md5sum, i.e. \memoized@search will only store the information when the
% md5sum argument (#1) matches \mmz@id. (The idea is to \undef something, like
% \mmz@@filename, and test it with \ifdef after inputting the .memo file.)
%
% The following macros store the info from .memo file into \mmz@@... macros.
\def\memoized@search#1{%
\def\mmz@@id{#1}%
\ifdefstrequal\mmz@@id\mmz@id{%
\let\memoized@next\endinput
\memoized@found
}{%
\let\memoized@next\skip
\memoized@skip
}%
}
\newif\ifmmz@@disabled
\pgfqkeys{/memoized}{
filename/.store in=\mmz@@filename,
page/.store in=\mmz@@page,
wd/.store in=\mmz@@wd,
ht/.store in=\mmz@@ht,
dp/.store in=\mmz@@dp,
padding left/.store in=\mmz@@padding@left,
padding bottom/.store in=\mmz@@padding@bottom,
padding right/.store in=\mmz@@padding@right,
padding top/.store in=\mmz@@padding@top,
disable/.is if=mmz@@disabled,
}
\def\memoized@found#1{%
\def\mmz@@filename{}%
\def\mmz@@page{}%
\def\mmz@@wd{}%
\def\mmz@@ht{}%
\def\mmz@@dp{}%
\def\mmz@@padding@left{}%
\def\mmz@@padding@bottom{}%
\def\mmz@@padding@right{}%
\def\mmz@@padding@top{}%
\mmz@@disabledfalse
\pgfqkeys{/memoized}{#1}%
\memoized@next
}
\def\memoized@skip#1{}
\def\usedmemoized#1{\memoized@skip}
% \jobname needs to be split into \mmz@jobpath and \mmz@jobbasename before
% calling this:
\def\mmz@memo@filename#1{%
\mmz@jobpath
\mmz@valueof{memo filename prefix}%
#1%
\mmz@valueof{memo filename suffix}%
}
\memoizeset{
% This is the stuff we need to do before we can start memoizing.
initialize/.style={
if readonly={}{initialize shipout},
open mmz,
if manual={}{enable environments},
define memoizeenable,
},
% Hijack \shipout. If "shipout only memoized" is in effect, we prevent the
% shipout of (regular) pages. Otherwise, we count how many pages were
% shipped out, so that we know which pages are ours --- can this be done
% otherwise, maybe by "total_pages" in LuaTeX?
initialize shipout/.code={%
\let\mmz@orig@shipout\shipout
\ifmmz@shipout@only@memoized
\def\shipout{%
\deadcycles=0
\setbox0=%
}
\else
\def\shipout{%
\global\advance\mmz@realpage1
\mmz@orig@shipout
}%
\fi
},
% Open the .mmz control file. This needs to be done after splitting but
% before the first externalization. By default, we do it in
% \AtBeginDocument,
open mmz/.code={%
% The .mmz has to sit next to the generated PDF, so \primitive\jobname!
\edef\pdf@primitivejobname{\pdf@primitive\jobname}%
\mmz@split@filename{\pdf@primitive\jobname}\mmz@temp\pdf@primitivejobbasename
\immediate\openout\mmz@out=\pdf@primitive\jobname.mmz
% Prefix and suffix info are used when splitting, either by an embedded
% call of from the external tool.
\immediate\write\mmz@out{%
\noexpand\memoizeset{%
memo filename prefix={\mmz@valueof{memo filename prefix}},
memo filename suffix={\mmz@valueof{memo filename suffix}},
jobname={\jobname}%
}\@percentchar
}%
%
% Close the .mmz file at the end.
\AtEndDocument{\immediate\closeout\mmz@out}%
},
% Enable enabling memoization :D
define memoizeenable/.code={%
\def\memoizeenable{\AfterPreamble{\memoizetrue}}%
},
% Split the .pdf generated in the previous compilation into ".memo.pdf"s
split/.code={%
\let\memoized\memoized@split
{% .mmz sets "jobname" and "memo filename prefix/suffix". We need them
% there because "split" is called before the user had the chance to ever
% call \memoizeset. But we don't want them to override anything, so we
% do the splitting in a group.
\def\memoizeset##1{\pgfqkeys{/memoize}{##1}\mmz@updatefilenames}%
\@input{\pdf@primitive\jobname.mmz}%
}%
% Disable afterwards, we really don't want to split twice!
\memoizeset{split/.code={}}%
},
}
\def\memoized@split#1{%
\def\mmz@@id{#1}%
\let\memoized@next\memoized@split@a
\memoized@found
}
\def\memoized@split@a{%
\ifmmz@@disabled\memoized@split@disabled\else\memoized@split@b\fi
}
\def\memoized@split@b{%
% Split the relevant page off to the memo .pdf.
\edef\mmz@syscall@jobname{\mmz@memo@filename\mmz@@id}%
\edef\mmz@marshal{%
\noexpand\justmemoized@prepare@syscall@commands{\mmz@jobpath\mmz@@filename}{\mmz@@page}%
}\mmz@marshal
\pdf@system{\mmz@valueof{latex system call}}%
% Write the .memo file.
\IfFileExists{\mmz@[email protected]}{%
\immediate\openout\mmz@out=\mmz@syscall@jobname
\immediate\write\mmz@out{%
\noexpand\memoized{\mmz@@id}%
{filename={\mmz@memo@basenameprefix\mmz@@id
\mmz@valueof{memo filename suffix}.pdf}, page=1,
wd=\mmz@@wd, ht=\mmz@@ht, dp=\mmz@@dp,
padding left=\mmz@@padding@left, padding bottom=\mmz@@padding@bottom,
padding right=\mmz@@padding@right, padding top=\mmz@@padding@top}%
\@percentchar
}%
\immediate\closeout\mmz@out
}{\justmemoized@error}%
}
\def\justmemoized@prepare@syscall@commands#1#2{%
% By using pdfpages, we don't care about the engine we use. And we're bound
% to LaTeX anyway ...
\def\mmz@syscall@commands{%
\documentclass{minimal}
\nofiles
% Debug PDF: \pdfcompresslevel=0 \pdfobjcompresslevel=0
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=#2,fitpaper]{#1}
\end{document}
}%
}%
\def\justmemoized@error{%
\PackageError{memoize}{Failed to extract page \mmz@@page\space of
"\mmz@jobpath\mmz@@filename" to "\mmz@[email protected]".}{}}
\def\memoized@split@disabled{%
\edef\mmz@syscall@jobname{\mmz@memo@filename\mmz@@id}%
\immediate\openout\mmz@out=\mmz@syscall@jobname
\immediate\write\mmz@out{%
\noexpand\memoized{\mmz@@id}%
{disable}%
\@percentchar
}%
\immediate\closeout\mmz@out
}
% AUTO-MEMOIZATION
% UI:
\memoizeset{
% * without an argument: general \memoizeenable / \memoizedisable
% (prevent is not allowed)
% * the argument is a control sequence: enable/disable/prevent
% memoizing a command
% * otherwise: enable/disable/prevent memoizing an environment
enable/.code={\def\mmz@handle@how{enable}\mmz@handle#1\mmz@end},
disable/.code={\def\mmz@handle@how{disable}\mmz@handle#1\mmz@end},
prevent/.code={\def\mmz@handle@how{prevent}\mmz@handle#1\mmz@end},
enable/.default={},
disable/.default={},
prevent/.default={},
% Non-xparse-defined commands must be registered. Todo in docs: if you get
% "LaTeX3 Error: Command '\foo' not defined using xparse.", you forgot to
% register the command.
register/.code 2 args={%
% #1 = the handled command
% #2 = the xparse argument specification of the handled command
\def\ArgumentSpecification{#2}%
\mmz@declare@handler#1%
},
% Let #2 handle #1 (which can be either a command or an environment). The
% user is responsible for correct argument structure in the case of #1 being
% a command. If #1 is an environment, the handler should take two arguments,
% the environment name and the environment body (long). The handler is used
% for both "enable" and "prevent" mode: when it is called, \MemoizeWrapper
% will be \let to either \memoize or \nomemoize. Todo: "extract" mode?
handler/.code 2 args={\cslet{\mmz@repl@{#1}}#2},
% Enable/disable auto-memoization of *all* environments. This patches \begin.
% By default, this is done in "initialize", unless option package option
% "manual" was given.
enable environments/.code={%
\ifdefequal\mmz@orig@begin\relax{}{%
\let\mmz@orig@begin\begin
\let\begin\mmz@begin
}{}%
},
disable environments/.code={%
\ifdef\mmz@orig@begin{%
\let\begin\mmz@orig@begin
\let\mmz@orig@begin\relax
}{}%
},
cancel/.code={%
\ifstrempty{#1}{%
\def\mmz@cancel@setup{}%
}{%
\eappto\mmz@cancel@setup{%
\let\mmz@canc#1\noexpand#1%
\def\noexpand#1{\global\noexpand\mmz@canceltrue\mmz@canc#1}%
}%
}%
},
}
\def\mmz@cancel@setup{}%
\def\mmz@canc#1{\expandonce{\csname mmz@canc@\string#1\endcsname}}
% Control sequences where we will store the handlers:
\def\mmz@orig#1{mmz@orig@\string#1} % the original command
\def\mmz@repl#1{mmz@repl@\string#1} % the initial part of the handler
\def\mmz@repl@#1{mmz@repl@\string#1@} % the handler replacing the original commmand
% End-of-arguments marker:
\def\mmz@end{\mmz@end}
% general, command or environment?
\def\mmz@handle{\futurelet\mmz@temp\mmz@handle@a}
\def\mmz@handle@a{%
\ifx\mmz@temp\mmz@end
\expandafter\mmz@handle@noarg % general
\else
\ifcat\noexpand\mmz@temp\relax
\expandafter\expandafter\expandafter\mmz@handle@cmd
\else
\expandafter\expandafter\expandafter\mmz@handle@env
\fi
\fi
}
% General:
\def\mmz@handle@noarg\mmz@end{\csuse{memoize\mmz@handle@how}}
\def\memoizeenable{\PackageError{memoize}{You need to call
\string\memoizeset{initialize} before \string\memoizeenable}{}}
\def\memoizedisable{\AfterPreamble{\memoizefalse}}
\def\memoizeprevent{\PackageError{memoize}{Key "prevent" requires an argument
(environment or command name)}{}}
% Command:
\def\mmz@handle@cmd{\csname mmz@handle@cmd@\mmz@handle@how\endcsname}
% Enable automemoizing the command:
\def\mmz@handle@cmd@enable{\mmz@handle@cmd@ep\memoize}
% Prevent memoization within the command:
\def\mmz@handle@cmd@prevent{\mmz@handle@cmd@ep\nomemoize}
% Enable automemoization / prevent memoization within the command
% #1 = handled (original) \cs
\def\mmz@handle@cmd@ep#1#2\mmz@end{%
% #1 = the wrapper: \memoize / \nomemoize
% #2 = the handled command
\ifcsdef{\mmz@repl@{#2}}{% We already have a handler for #2, use it.
}{% #2 may only be an xparse-defined command, extract the argspec.
\mmz@handle@cmd@ep@heavy#2%
}%
\mmz@handler@init#2#1%
}
\ifmmz@light %%%%%%%%%%%%%%%% begin light
\def\mmz@handle@cmd@ep@heavy#1{\PackageError{memoize}{Handler for \string#1 is
not defined.}{If \string#1 was defined using package "xparse", you need to
remove package option "light" to handle it automatically.}{}}
\def\mmz@declare@handler#1{\PackageError{memoize}{Fancy command handling with
package "xparse" is not available when package option "light" is given.}{}}
\else %%%%%%%%%%%%%%%% begin heavy
\RequirePackage{xparse-arglist}
\def\mmz@handle@cmd@ep@heavy#1{% #1 = the handled command
\GetDocumentCommandArgSpec{#1}%
\mmz@declare@handler#1%
}
\def\mmz@declare@handler#1{%
\edef\mmz@marshal{%
\noexpand\DeclareDocumentCommand
\expandonce{\csname \mmz@repl#1@\endcsname}%
{\expandonce{\ArgumentSpecification}}%
{%
% todo: add a hook for users setup; prevent user from changing \MemoizeWrapper?
\edef\noexpand\mmz@marshal{%
\noexpand\noexpand\noexpand\MemoizeWrapper{%
\noexpand\noexpand\expandonce{\csname\mmz@orig#1\endcsname}%
\ArgumentList
}%
}\noexpand\mmz@marshal
}%
}\mmz@marshal
}
\fi %%%%%%%%%%%%%%%% end light/heavy
% Define the initial part of the handler for #1, and let it replace the
% original command
\def\mmz@handler@init#1#2{%
% Define the initial part: in that part, we check \ifmemoize without
% expanding any arguments, and \let \MemoizeWrapper
% #1 = handled command \cs
% #2 = \memoize / \nomemoize
\edef\mmz@marshal{%
\noexpand\DeclareRobustCommand
\expandonce{\csname\mmz@repl#1\endcsname}%
{%
\noexpand\ifmemoize
\let\noexpand\MemoizeWrapper\noexpand#2%
\noexpand\expandafter\expandonce{\csname \mmz@repl@#1\endcsname}%
\noexpand\else
\noexpand\expandafter\expandonce{\csname\mmz@orig#1\endcsname}%
\noexpand\fi
}%
}\mmz@marshal
% Save the original (handled) command, if we don't have it yet.
\ifcsmacro{\mmz@orig{#1}}{}{%
\cslet{\mmz@orig{#1}}#1%
}%
% Replace the original command with the handler.
\letcs#1{\mmz@repl{#1}}%
}
% Restore the original command:
\def\mmz@handle@cmd@disable#1#2\mmz@end{%
\ifstrempty{#2}{}{\mmz@handle@cmd@disable@error#1}%
\ifcsdef{\mmz@orig{#1}}{%
\letcs#1{\mmz@orig{#1}}%
% \undef, so that subsequent enable/prevent can pick up a new original
\csundef{\mmz@orig{#1}}%
}{}%
}
\def\mmz@handle@cmd@disable@error#1{%
\PackageWarning{memoize}{Ignoring argument specification while disabling
\string#1}}
% Environments: We only need to remember how environment #1 should be handled:
% #1 = enable / disable / prevent, cs "\mmz@repl{#1}" is let to
% \mmz@begin@enable/disable/prevent. These are defined later.
\def\mmz@handle@env#1\mmz@end{%
\csletcs{\mmz@repl{#1}}{mmz@begin@\mmz@handle@how}}
% The real work is done by our redefinitions of \begin.
% \item todo: environment hooks (etoolbox)
\DeclareRobustCommand\mmz@begin{%
\ifmemoize
\expandafter\mmz@begin@a
\else
\expandafter\mmz@orig@begin
\fi
}
% \mmz@repl@<envname> is \let to \mmz@begin@enable/disable/prevent
\def\mmz@begin@a#1{%
\expandafter\ifx\csname\mmz@repl{#1}\endcsname\relax
\expandafter\mmz@orig@begin
\else
\expandafter\mmz@begin@handled
\fi
{#1}%
}
% Automemoize an environment.
\def\mmz@begin@handled#1{%
\edef\mmz@marshal{\noexpand\mmz@begin@handled@a{\@currenvir}{#1}}%
% We need to temporarily redefine \@currenvir for \Collect@Body to work.
\def\@currenvir{#1}%
\Collect@Body\mmz@marshal
}
\long\def\mmz@begin@handled@a#1#2#3\end#4{%
% #1 = the previous environment
% #2 = the handled environment name
% #3 = the handled environment body
% #4 = #2
\csuse{\mmz@repl{#2}}{#2}{#3}%
\def\@currenvir{#1}%
}
% Environment handlers:
% #1 = environment name
% #2 = environment code
\long\def\mmz@begin@enable#1#2{%
\memoize{\begin{#1}#2\end{#1}}}
\long\def\mmz@begin@prevent#1#2{%
\nomemoize{\begin{#1}#2\end{#1}}}
\let\mmz@begin@disable\relax
\AtEndPreamble{%
\mmz@updatefilenames % even if loadonly!
}
\unless\ifmmz@loadonly %%%%%%%%%%%%%%%% begin not loadonly
\AtEndPreamble{%
\unless\ifmmz@manual %%%%%%%%%%%%%%%% begin automemoization
% Enable auto-memoization for tikz, if it is loaded.
\@ifpackageloaded{tikz}{%
\input memoize-tikz % Here we define and install \tikz handler.
\memoizeset{
enable=tikzpicture,
enable=\tikz,
}%
}{}%
% Enable auto-memoization for forest, if it is loaded.
% todo: Move this into forest itself.
\@ifpackageloaded{forest}{%
\memoizeset{
enable=forest,
% \Forest is defined by xparse, so no need to register it.
enable=\Forest,
}%
}{}%
\fi %%%%%%%%%%%%%%%% end automemoization
%
}
\memoizeset{cancel=\pdfsavepos}
% We do the patching as late as possible.
\AtBeginDocument{%
\memoizeset{
initialize,
% We can't split while the new PDF is being produced.
split/.code={\PackageError{memoize}{Splitting is only allowed in the preamble}{}},
}%
\memoizeenable
}
% We split while loading the package. Why? Some packages, when loaded, already
% start producing the PDF, but we need the PDF from the previous compilation
% intact to split! So splitting now makes it possible for the user to avoid
% this problem by ordering memoize before any offending packages.
%
% Obviously, this only affects splitting via an embedded system call.
%
% This problem is also why we load memo filename prefix and suffix from ".mmz".
% We can't include it among the package options, because of the spaces in the
% key name (and we don't want a cryptic package key option). Plus, for the user
% it makes sense that these affixes can be specified in the same location as
% everything else. Plus, loading it from .mmz is consistent with memomanager.
%
% The list of stuff that should be loaded after "memoize" for splitting to
% work: tikz library "shadows".
\memoizeset{split}
\fi %%%%%%%%%%%%%%%% end not loadonly
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "test1"
%%% End: