]> git.lyx.org Git - lyx.git/blob - development/mathmacros/architecture.lyx
Customization: correct some color names.
[lyx.git] / development / mathmacros / architecture.lyx
1 #LyX 1.5.2 created this file. For more info see http://www.lyx.org/
2 \lyxformat 276
3 \begin_document
4 \begin_header
5 \textclass article
6 \language english
7 \inputencoding auto
8 \font_roman default
9 \font_sans default
10 \font_typewriter default
11 \font_default_family default
12 \font_sc false
13 \font_osf false
14 \font_sf_scale 100
15 \font_tt_scale 100
16 \graphics default
17 \paperfontsize default
18 \papersize default
19 \use_geometry false
20 \use_amsmath 1
21 \use_esint 1
22 \cite_engine basic
23 \use_bibtopic false
24 \paperorientation portrait
25 \secnumdepth 3
26 \tocdepth 3
27 \paragraph_separation indent
28 \defskip medskip
29 \quotes_language english
30 \papercolumns 1
31 \papersides 1
32 \paperpagestyle default
33 \tracking_changes false
34 \output_changes false
35 \author "" 
36 \author "" 
37 \end_header
38
39 \begin_body
40
41 \begin_layout Title
42 Dynamic Macros for LyX
43 \end_layout
44
45 \begin_layout Author
46 Stefan Schimanski 
47 \newline
48
49 \begin_inset LatexCommand url
50 target "sts@1stein.org"
51
52 \end_inset
53
54
55 \end_layout
56
57 \begin_layout Date
58 21.10.2007
59 \end_layout
60
61 \begin_layout Section
62 The old system
63 \end_layout
64
65 \begin_layout Standard
66 LyX has the concept of math macros for quite some time.
67  In LyX 1.4 or 1.5 you can create one in your document by calling the 
68 \family typewriter
69 math-macro
70 \family default
71  command in the mini buffer.
72  Visually this results in something equivalent to a TeX macro: 
73 \begin_inset listings
74 inline false
75 status open
76
77 \begin_layout Standard
78
79
80 \backslash
81 newcommand{
82 \backslash
83 foo}{
84 \backslash
85 frac{1}{2}}
86 \end_layout
87
88 \end_inset
89
90
91 \end_layout
92
93 \begin_layout Standard
94 After LyX processed this, the command is available in math environments
95  in the same documents.
96  But internally there is nothing more than 
97 \series bold
98 one
99 \series default
100  global macro table.
101  The position inside the document does not matter.
102  The command is available even in front of the definition.
103  But even worse, this global macro table is global for all opened documents.
104  If two buffers use the same macro name with different definitions, you
105  are in trouble.
106  The behaviour is undefined.
107  If you are lucky LyX will not crash.
108  Nothing must be said about redefining a macro later in the document: the
109  behaviour of LyX will not be what you expect.
110 \end_layout
111
112 \begin_layout Standard
113 LyX 1.4 and 1.5 do not show the support for this kind of macro very prominently.
114  In fact it is described in the 
115 \begin_inset Quotes eld
116 \end_inset
117
118 Extended Features
119 \begin_inset Quotes erd
120 \end_inset
121
122  manual.
123  But there is no menu item to create macros.
124  Next to the mentioned method with the mini buffer you can use the 
125 \begin_inset Quotes eld
126 \end_inset
127
128 Ctrl-L
129 \begin_inset Quotes erd
130 \end_inset
131
132  short cut to convert a raw 
133 \backslash
134 newcommand into a LyX math macro.
135  Hence the role of macro is more of a power user tool for users who know
136  what they are doing.
137 \end_layout
138
139 \begin_layout Section
140 A wish list for a new macro implementation
141 \end_layout
142
143 \begin_layout Standard
144 In the following usecases are shown which can be wished to be supported
145  if macros are reimplemented.
146  Most of them are not possible in the old implemention, or at least very
147  hard to do.
148  
149 \end_layout
150
151 \begin_layout Enumerate
152 Define a 
153 \series bold
154 new macro
155 \series default
156  with a known arity ("arity" = number of arguments).
157  Use instances later on in the document.
158 \end_layout
159
160 \begin_layout Enumerate
161
162 \series bold
163 Redefine
164 \series default
165  a macro to use the same macro name with different definitions in different
166  areas of the document.
167 \end_layout
168
169 \begin_layout Enumerate
170 Have 
171 \family typewriter
172
173 \backslash
174 newcommand
175 \family default
176  in the preamble (i.e.
177  by importing tex code) or 
178 \series bold
179 not accessible
180 \series default
181  as a LyX macro in another way, and then define the command as a native
182  LyX math macro later.
183  All the uses of the old command should then turn into instances of the
184  LyX math macro.
185  
186 \end_layout
187
188 \begin_layout Enumerate
189
190 \series bold
191 Rename
192 \series default
193  a macro and also adapt all the instance of the macro in the document.
194 \end_layout
195
196 \begin_layout Enumerate
197
198 \series bold
199 Change the arity
200 \series default
201  of a macro (normaly probably increase it), maybe with a default value used
202  in instances of the macro (possibly empty).
203 \end_layout
204
205 \begin_layout Enumerate
206
207 \series bold
208 Removal
209 \series default
210  of a macro.
211 \end_layout
212
213 \begin_layout Enumerate
214 Insertion of a macro via the 
215 \series bold
216 menu
217 \series default
218  like "Insert->MathMacro".
219 \end_layout
220
221 \begin_layout Enumerate
222
223 \series bold
224 Moving
225 \series default
226  of a macro.
227 \end_layout
228
229 \begin_layout Enumerate
230
231 \series bold
232 \begin_inset LatexCommand label
233 name "sub:listedit"
234
235 \end_inset
236
237
238 \series default
239 Editing of a macro instance as a 
240 \series bold
241 list
242 \series default
243  of #1: __, #2: __, i.e.
244  it 
245 \series bold
246 unfold
247 \series default
248 s when the cursor goes inside.
249 \end_layout
250
251 \begin_layout Enumerate
252 Editing of macros 
253 \series bold
254 inline
255 \series default
256  (the macro definition will be read-only, only the arguments as holes are
257  editable).
258 \end_layout
259
260 \begin_layout Enumerate
261 Changing of the 
262 \series bold
263 editing behaviour
264 \series default
265  of 
266 \series bold
267
268 \begin_inset LatexCommand ref
269 reference "sub:listedit"
270
271 \end_inset
272
273
274 \series default
275  for certain macros, not only globally.
276 \end_layout
277
278 \begin_layout Enumerate
279 Defining dynamic 
280 \series bold
281 macros inside of macros
282 \series default
283 .
284 \end_layout
285
286 \begin_layout Enumerate
287 Using macros with the 
288 \series bold
289 same name
290 \series default
291 , but different definitions in different open documents or parts of the
292  same document.
293 \end_layout
294
295 \begin_layout Enumerate
296 Using macros from the 
297 \series bold
298 master document
299 \series default
300 .
301 \end_layout
302
303 \begin_layout Enumerate
304 Using the same argument, e.g.
305  #1, (
306 \series bold
307 non-linearly
308 \series default
309 ) more than once in the definition, like 
310 \family typewriter
311
312 \backslash
313 newcommand{
314 \backslash
315 ntothen}[1]{#1^#1}
316 \end_layout
317
318 \begin_layout Enumerate
319
320 \series bold
321 Patterns
322 \series default
323  like 
324 \family typewriter
325
326 \backslash
327 def
328 \backslash
329 foo #1/#2{
330 \backslash
331 frac{#1}{#2}}
332 \end_layout
333
334 \begin_layout Enumerate
335
336 \series bold
337 Higher order
338 \series default
339  substitution (or call-by-name text substition) like 
340 \family typewriter
341
342 \backslash
343 def
344 \backslash
345 foo #1{#1 12}
346 \family default
347 , which is applicable like 
348 \family typewriter
349
350 \backslash
351 foo
352 \backslash
353 frac
354 \family default
355  to give 
356 \family typewriter
357 1/2
358 \end_layout
359
360 \begin_layout Enumerate
361
362 \series bold
363 Optional
364 \series default
365  parameters like 
366 \family typewriter
367
368 \backslash
369 newcommand{
370 \backslash
371 foo}[2][x]{#2_#1}
372 \end_layout
373
374 \begin_layout Standard
375 The old implementation supports the following: 
376 \series bold
377 new macro
378 \series default
379
380 \series bold
381 moving
382 \series default
383
384 \series bold
385 list
386 \series default
387
388 \series bold
389 master document
390 \series default
391
392 \series bold
393 non-linear.
394  
395 \series default
396 Though support of master documents is more or less an coincidence by the
397  global table.
398 \end_layout
399
400 \begin_layout Standard
401 The implementation described later (short: the new implementation) supports
402  the following: 
403 \series bold
404 new macro
405 \series default
406
407 \series bold
408 redefine
409 \series default
410
411 \series bold
412 not accesible
413 \series default
414
415 \series bold
416 change the arity
417 \series default
418
419 \series bold
420 removal
421 \series default
422
423 \series bold
424 menu
425 \series default
426
427 \series bold
428 moving
429 \series default
430
431 \series bold
432 unfolds
433 \series default
434
435 \series bold
436 inline
437 \series default
438
439 \series bold
440 same name
441 \series default
442
443 \series bold
444 master documents
445 \series default
446
447 \series bold
448 non-linearly
449 \series default
450
451 \series bold
452 optional
453 \series default
454 .
455 \end_layout
456
457 \begin_layout Standard
458 Not supported are: 
459 \series bold
460 patterns
461 \series default
462
463 \series bold
464 higher order
465 \series default
466
467 \series bold
468 macros inside of macros
469 \series default
470 ,
471 \series bold
472  editing behaviour
473 \series default
474 ,
475 \series bold
476  list
477 \series default
478 ,
479 \series bold
480  rename
481 \series default
482 .
483  The last 3 should be doable without much work, maybe also 
484 \series bold
485 patterns
486 \series default
487 .
488 \end_layout
489
490 \begin_layout Subsection
491 Main deficiencies of the old system
492 \end_layout
493
494 \begin_layout Standard
495 The main problem of the old implementation is that it is not dynamic at
496  all.
497  A macro is resolved (i.e.
498  the lookup in the global table takes place) when the internal object is
499  created, e.g.
500  while parsing of the 
501 \family typewriter
502 .lyx
503 \family default
504  document or when typing 
505 \family typewriter
506
507 \backslash
508 foo
509 \family default
510 .
511  If no macro definition of the right name exists at this time, an ERT is
512  created.
513  
514 \end_layout
515
516 \begin_layout Standard
517 Moreover there is no position awareness of the definition, only during loading
518  of a document there is something like that because macro definitions are
519  put into the global table at the point in the document where it appears.
520  
521 \end_layout
522
523 \begin_layout Section
524 A new approach
525 \end_layout
526
527 \begin_layout Standard
528 The goal of a new macro approach must be to support as many use cases subsection
529  1.2 as possible, or at least make an implementation possible of the remaining
530  ones.
531  Moreover a proper implementation better brings most of TeX's power of math
532  command into LyX.
533 \end_layout
534
535 \begin_layout Standard
536 In a few words the new approach could be described as follow: 
537 \end_layout
538
539 \begin_layout Itemize
540 A macro is a dynamic inset which can 
541 \begin_inset Quotes eld
542 \end_inset
543
544 eat up
545 \begin_inset Quotes erd
546 \end_inset
547
548  or 
549 \begin_inset Quotes eld
550 \end_inset
551
552 spit out
553 \begin_inset Quotes erd
554 \end_inset
555
556  insets which follow the macro, depending on the success to resolve it and
557  the arity of the macro definition at the position in the document.
558 \end_layout
559
560 \begin_layout Itemize
561 Macros are resolved again everytime it is redrawn on screen if the macro
562  definition changed which is valid at the position.
563 \end_layout
564
565 \begin_layout Standard
566 This 
567 \begin_inset Quotes eld
568 \end_inset
569
570 eat up
571 \begin_inset Quotes erd
572 \end_inset
573
574  and 
575 \begin_inset Quotes eld
576 \end_inset
577
578 spit out
579 \begin_inset Quotes erd
580 \end_inset
581
582  process is the key idea.
583  Imagine a macro definition 
584 \family typewriter
585
586 \backslash
587 newcommand{
588 \backslash
589 foo}[2]{a#1b#2c}
590 \family default
591  which is valid when a macro
592 \family typewriter
593  
594 \backslash
595 foo 
596 \family default
597 appears in the context 
598 \family typewriter
599
600 \backslash
601 foo ABCD
602 \family default
603 .
604  When the macro is drawn the definition is checked and the arity is compared
605  to the number of 
606 \begin_inset Quotes eld
607 \end_inset
608
609 eaten
610 \begin_inset Quotes erd
611 \end_inset
612
613  insets.
614  At the beginning the latter will be zero.
615  Because the arity of 
616 \family typewriter
617
618 \backslash
619 foo
620 \family default
621  is 2, the macro inset will eat up 2 insets (the 
622 \family typewriter
623 A
624 \family default
625  and the 
626 \family typewriter
627 B
628 \family default
629 ), hence internally the macro 
630 \family typewriter
631
632 \backslash
633 foo
634 \family default
635  is changed to arity 2 and 
636 \family typewriter
637 A
638 \family default
639  and 
640 \family typewriter
641 B
642 \family default
643  are moved into it.
644  The visual representation of macro is that of the definition with the arguments
645  replaced by the eaten insets.
646  So eventually you will see 
647 \family typewriter
648 aAbBcCD
649 \family default
650  on screen.
651 \end_layout
652
653 \begin_layout Standard
654 When you change the macro definition at the position into a unary macro,
655  e.g.
656  
657 \family typewriter
658
659 \backslash
660 newcommand{
661 \backslash
662 foo}[1]{a#1b}
663 \family default
664 , the macro inset will spit out the second eaten inset, here the 
665 \family typewriter
666 B
667 \family default
668 .
669  Hence you will eventually see 
670 \family typewriter
671 aAbBCD
672 \family default
673 .
674 \end_layout
675
676 \begin_layout Standard
677 This process is done automatically, transparent to the user and in a fast
678  way everytime the macro is rendered and the definition has changed.
679 \end_layout
680
681 \begin_layout Standard
682 If you look at the produced TeX code of the 
683 \family typewriter
684
685 \backslash
686 foo ABCD
687 \family default
688  you will notice that it didn't change during all this eating and spitting.
689  This is what you expect from a macro in TeX.
690  There the whole sense of command (i.e.
691  macros) is that you keep the same TeX code, independently from the macro
692  definition.
693  This approach carries this over to the LyX world.
694 \end_layout
695
696 \begin_layout Subsection
697 The implementation
698 \end_layout
699
700 \begin_layout Subsubsection
701 MathData
702 \end_layout
703
704 \begin_layout Standard
705 When a MathData object is drawn, more precisely when the metrics are computed,
706  all math macros in the 
707 \family typewriter
708 MathData
709 \family default
710  are processed in the way described in the previous section.
711  If the arity of definition of a macro is changed the spitting/eating process
712  takes place.
713  This is implemented in 
714 \family typewriter
715 MathData::updateMacros
716 \family default
717 , in a quite straight forward way.
718  
719 \end_layout
720
721 \begin_layout Standard
722 Some complexity comes into the game by the necessary updating of the current
723  cursor.
724  If the user unfolds a macro the arity practically changes to zero, hence
725  the arguments are spit out.
726  If the cursor was inside an argument before, it should be in the same argument
727  after the unfolding.
728  The same should be the case for folding.
729 \end_layout
730
731 \begin_layout Standard
732 The metrics calculation is, by its typing, a const method, i.e.
733  it shouldn't change the 
734 \family typewriter
735 MathData
736 \family default
737  object.
738  The macro updating though does changes of course.
739  Technically this is true, semantically (taking the produced TeX code as
740  semantics) it is not because nothing changes by eating/spitting or folding/unfo
741 lding with the later output.
742  To still allow these changes in 
743 \family typewriter
744 MathData::metrics
745 \family default
746  a 
747 \family typewriter
748 const_cast
749 \family default
750  is used.
751  This is somewhat ugly and a cleaner solution should be found.
752  Maybe one day the drawing and metrics will merge, then it would make sense
753  to think about the 
754 \family typewriter
755 const_cast
756 \family default
757  as well again.
758 \end_layout
759
760 \begin_layout Subsubsection
761 Macro Table
762 \end_layout
763
764 \begin_layout Standard
765 How does the 
766 \family typewriter
767 MathData
768 \family default
769  know which macro definitions are known at its position in the buffer? During
770  the metrics call a 
771 \family typewriter
772 MacroContext
773 \family default
774  is passed around as an element of the 
775 \family typewriter
776 MetricsInfo
777 \family default
778  class.
779  This context can be asked to resolve a macro name.
780 \end_layout
781
782 \begin_layout Standard
783 To make this possible it has to know about a position in the buffer.
784  In fact it knows about the paragraph in the buffer, and in addition it
785  has a 
786 \begin_inset Quotes eld
787 \end_inset
788
789 local
790 \begin_inset Quotes erd
791 \end_inset
792
793  
794 \family typewriter
795 MacroTable
796 \family default
797 .
798  The latter is used to also resolve macros correctly which are defined in
799  the paragraph where the macro appears.
800  The inset loop in the 
801 \family typewriter
802 TextMetrics::redoParagraph
803 \family default
804  creates and updates the 
805 \family typewriter
806 MacroContext
807 \family default
808  and the local macros in the expected way.
809  
810 \end_layout
811
812 \begin_layout Standard
813 All the other macros are resolved by the 
814 \family typewriter
815 MacroContext
816 \family default
817  by asking the buffer directly.
818  For this the 
819 \family typewriter
820 MacroContext
821 \family default
822 , as written above, knows the paragraph it belongs to.
823  It passes this information to the buffer (via 
824 \family typewriter
825 Buffer::hasMacro(docstring name, Paragraph par)
826 \family default
827 ) and the buffer then uses the 
828 \family typewriter
829 par.macrocontextPosition()
830 \family default
831  information to lookup the defined macros at the position in the map 
832 \family typewriter
833 Buffer::pimpl->macros
834 \family default
835 .
836  This maps macro names and positions to the macro definitions.
837  which are defined at the position or before.
838 \end_layout
839
840 \begin_layout Standard
841 The missing bit is how the buffer creates this map.
842  This is done in the same way as in the old macro implementation, namely
843  by the Buffer::updateMacros method which iterates over the top-level inset
844  of the buffer.
845  It is called from 
846 \family typewriter
847 BufferView::processUpdateFlags
848 \family default
849  very often.
850  This sounds slow, but it turned out that it is not noticable in fact.
851  In LyX 1.5 the same is done as well already.
852  Maybe some optimisation could help though, but was not investigated.
853 \end_layout
854
855 \begin_layout Standard
856 To support master documents there will a last lookup (if the previous lookup
857  were not successfull) by asking the master buffer.
858 \end_layout
859
860 \begin_layout Subsection
861 File Format
862 \end_layout
863
864 \begin_layout Standard
865 The file format concerning macros in the old macro implementation is not
866  well defined.
867  As described above there is a big difference between the visual semantics
868  (what the user sees inside LyX 1.5) and the latex semantics (what LaTeX
869  will make out of the document) are not the same.
870 \end_layout
871
872 \begin_layout Standard
873 The new approach changes this for most documents (if the user does not do
874  any dirty tricks at least) to be the same.
875  So from the file format point of view there probably should not be any
876  conversion needed to a new file format.
877 \end_layout
878
879 \begin_layout Standard
880 One exception of this comes from the support for optional arguments in the
881  new implementation.
882  Those were not available in the old format.
883 \end_layout
884
885 \begin_layout Subsubsection
886 Simple Macros
887 \end_layout
888
889 \begin_layout Standard
890 Macro definitions are stored in the following way:
891 \begin_inset listings
892 inline false
893 status open
894
895 \begin_layout Standard
896
897
898 \backslash
899 begin_inset FormulaMacro
900 \end_layout
901
902 \begin_layout Standard
903
904
905 \backslash
906 newcommand{
907 \backslash
908 abc}[1]{
909 \backslash
910 sin
911 \backslash
912 left(
913 \backslash
914 frac{-3}{#1}
915 \backslash
916 right)} 
917 \end_layout
918
919 \begin_layout Standard
920
921
922 \backslash
923 end_inset
924 \end_layout
925
926 \end_inset
927
928
929 \end_layout
930
931 \begin_layout Standard
932 The resulting LaTeX code is as expected:
933 \begin_inset listings
934 inline false
935 status open
936
937 \begin_layout Standard
938
939  
940 \backslash
941 newcommand{
942 \backslash
943 abc}[1]{
944 \backslash
945 sin
946 \backslash
947 left(
948 \backslash
949 frac{-3}{#1}
950 \backslash
951 right)}
952 \end_layout
953
954 \end_inset
955
956
957 \end_layout
958
959 \begin_layout Subsubsection
960 One Optional Argument
961 \end_layout
962
963 \begin_layout Standard
964 With one optional argument the LyX code looks like this:
965 \begin_inset listings
966 inline false
967 status open
968
969 \begin_layout Standard
970
971
972 \backslash
973 begin_inset FormulaMacro
974 \end_layout
975
976 \begin_layout Standard
977
978
979 \backslash
980 newcommand{
981 \backslash
982 abc}[1][42]{
983 \backslash
984 sin
985 \backslash
986 left(
987 \backslash
988 frac{-3}{#1}
989 \backslash
990 right)}
991 \end_layout
992
993 \begin_layout Standard
994
995
996 \backslash
997 end_inset
998 \end_layout
999
1000 \end_inset
1001
1002  and the LaTeX code again is the same:
1003 \begin_inset listings
1004 inline false
1005 status open
1006
1007 \begin_layout Standard
1008
1009
1010 \backslash
1011 newcommand{
1012 \backslash
1013 abc}[1][42]{
1014 \backslash
1015 sin
1016 \backslash
1017 left(
1018 \backslash
1019 frac{-3}{#1}
1020 \backslash
1021 right)}
1022 \end_layout
1023
1024 \end_inset
1025
1026
1027 \end_layout
1028
1029 \begin_layout Subsubsection
1030 Multi Optional Argument Macro 
1031 \end_layout
1032
1033 \begin_layout Standard
1034 More than one optional argument is not supported by LaTeX.
1035  There are several solutions to allow them by defining some custom 
1036 \family typewriter
1037
1038 \backslash
1039 newcommand
1040 \family default
1041 , but this is not standarized.
1042  It might make sense for LyX to also support those when importing, but this
1043  is not implemented.
1044  Instead the new implementation will create valid standard LaTeX code by
1045  outputting what the user sees on screen in LyX:
1046 \begin_inset listings
1047 inline false
1048 status open
1049
1050 \begin_layout Standard
1051
1052
1053 \backslash
1054 begin_inset FormulaMacro
1055 \end_layout
1056
1057 \begin_layout Standard
1058
1059
1060 \backslash
1061 newcommand{
1062 \backslash
1063 xyz}[2][42][28]{
1064 \backslash
1065 sqrt{#1}+
1066 \backslash
1067 ln
1068 \backslash
1069 left(
1070 \backslash
1071 frac{#2}{82}
1072 \backslash
1073 right)} 
1074 \end_layout
1075
1076 \begin_layout Standard
1077
1078
1079 \backslash
1080 end_inset
1081 \end_layout
1082
1083 \end_inset
1084
1085  with the LaTeX code:
1086 \begin_inset listings
1087 inline false
1088 status open
1089
1090 \begin_layout Standard
1091
1092
1093 \backslash
1094 newcommand{
1095 \backslash
1096 xyz}[2][42]{
1097 \backslash
1098 sqrt{#1}+
1099 \backslash
1100 ln
1101 \backslash
1102 left(
1103 \backslash
1104 frac{#2}{82}
1105 \backslash
1106 right)}
1107 \end_layout
1108
1109 \end_inset
1110
1111 When the user creates an instance of 
1112 \backslash
1113 xyz without substituting the optional argument, e.g.
1114  by 
1115 \begin_inset listings
1116 inline false
1117 status open
1118
1119 \begin_layout Standard
1120
1121
1122 \backslash
1123 xyz
1124 \end_layout
1125
1126 \end_inset
1127
1128  LyX will create the following LaTeX code when exporting to LaTeX:
1129 \begin_inset listings
1130 inline false
1131 status open
1132
1133 \begin_layout Standard
1134
1135
1136 \backslash
1137 xyz{28}
1138 \end_layout
1139
1140 \end_inset
1141
1142 So the optional argument is not optional anymore after export, but explicit.
1143 \end_layout
1144
1145 \begin_layout Subsubsection
1146
1147 \backslash
1148 def style
1149 \end_layout
1150
1151 \begin_layout Standard
1152 Last but not least, as in the old implementation you can use 
1153 \family typewriter
1154
1155 \backslash
1156 def
1157 \family default
1158  macros, i.e.
1159  TeX style definitions.
1160  They don't support optional arguments.
1161  They don't support 
1162 \begin_inset Quotes eld
1163 \end_inset
1164
1165 patterns
1166 \begin_inset Quotes erd
1167 \end_inset
1168
1169  of the shape 
1170 \family typewriter
1171
1172 \backslash
1173 def
1174 \backslash
1175 term #1+#2{#1+#2}
1176 \family default
1177  where you can use it as in 
1178 \family typewriter
1179
1180 \backslash
1181 term 43+12
1182 \family default
1183 .
1184 \end_layout
1185
1186 \begin_layout Subsubsection
1187 Redefinition
1188 \end_layout
1189
1190 \begin_layout Standard
1191 On export LyX will correctly use 
1192 \family typewriter
1193
1194 \backslash
1195 newcommand
1196 \family default
1197  and 
1198 \family typewriter
1199
1200 \backslash
1201 renewcommand
1202 \family default
1203  if needed.
1204  This is not visible in the LyX file format though.
1205 \end_layout
1206
1207 \begin_layout Subsection
1208 How it looks to the user
1209 \end_layout
1210
1211 \begin_layout Subsubsection
1212 Creation
1213 \end_layout
1214
1215 \begin_layout Standard
1216 Macro definitions look more or less the same as in the old implementation.
1217  I.e.
1218  there is a macro definition inset showing the macro like 
1219 \family typewriter
1220
1221 \backslash
1222 foo{#1}:={a#1b}
1223 \family default
1224 .
1225  You can create i
1226 \family typewriter
1227 t
1228 \family default
1229  via the 
1230 \begin_inset Quotes eld
1231 \end_inset
1232
1233 math-macro foo 4
1234 \begin_inset Quotes erd
1235 \end_inset
1236
1237  to create a macro 
1238 \family typewriter
1239
1240 \backslash
1241 foo
1242 \family default
1243  with 4 parameters.
1244 \end_layout
1245
1246 \begin_layout Standard
1247 A second way to create them is to write down the LaTeX definition like 
1248 \family typewriter
1249
1250 \backslash
1251 newcommand
1252 \backslash
1253 foo{abc}
1254 \family default
1255  as plain text.
1256  Select it and press Ctrl-m.
1257 \end_layout
1258
1259 \begin_layout Standard
1260 The last way, which is new with the new implementation, is to use the Insert/Mat
1261 h/Macro menu item.
1262 \end_layout
1263
1264 \begin_layout Subsubsection
1265 Modification
1266 \end_layout
1267
1268 \begin_layout Standard
1269 The are the following actions defined:
1270 \end_layout
1271
1272 \begin_layout Standard
1273 \begin_inset Tabular
1274 <lyxtabular version="3" rows="12" columns="2">
1275 <features>
1276 <column alignment="center" valignment="top" leftline="true" width="0">
1277 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1278 <row topline="true" bottomline="true">
1279 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1280 \begin_inset Text
1281
1282 \begin_layout Standard
1283 Action
1284 \end_layout
1285
1286 \end_inset
1287 </cell>
1288 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1289 \begin_inset Text
1290
1291 \begin_layout Standard
1292 Menu
1293 \end_layout
1294
1295 \end_inset
1296 </cell>
1297 </row>
1298 <row topline="true">
1299 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1300 \begin_inset Text
1301
1302 \begin_layout Standard
1303 math-macro-unfold
1304 \end_layout
1305
1306 \end_inset
1307 </cell>
1308 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1309 \begin_inset Text
1310
1311 \begin_layout Standard
1312 View/Unfold Math Macro
1313 \end_layout
1314
1315 \end_inset
1316 </cell>
1317 </row>
1318 <row topline="true">
1319 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1320 \begin_inset Text
1321
1322 \begin_layout Standard
1323 math-macro-fold
1324 \end_layout
1325
1326 \end_inset
1327 </cell>
1328 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1329 \begin_inset Text
1330
1331 \begin_layout Standard
1332 View/Fold Math Macro
1333 \end_layout
1334
1335 \end_inset
1336 </cell>
1337 </row>
1338 <row topline="true">
1339 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1340 \begin_inset Text
1341
1342 \begin_layout Standard
1343 math-macro-add-param
1344 \end_layout
1345
1346 \end_inset
1347 </cell>
1348 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1349 \begin_inset Text
1350
1351 \begin_layout Standard
1352 Edit/Math/Macro/Append Parameter
1353 \end_layout
1354
1355 \end_inset
1356 </cell>
1357 </row>
1358 <row topline="true">
1359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1360 \begin_inset Text
1361
1362 \begin_layout Standard
1363 math-macro-remove-param
1364 \end_layout
1365
1366 \end_inset
1367 </cell>
1368 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1369 \begin_inset Text
1370
1371 \begin_layout Standard
1372 Edit/Math/Macro/Remove Last Parameter
1373 \end_layout
1374
1375 \end_inset
1376 </cell>
1377 </row>
1378 <row topline="true">
1379 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1380 \begin_inset Text
1381
1382 \begin_layout Standard
1383 math-macro-append-greedy-param
1384 \end_layout
1385
1386 \end_inset
1387 </cell>
1388 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1389 \begin_inset Text
1390
1391 \begin_layout Standard
1392 Edit/Math/Macro/Append Parameter Eating From the Right
1393 \end_layout
1394
1395 \end_inset
1396 </cell>
1397 </row>
1398 <row topline="true">
1399 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1400 \begin_inset Text
1401
1402 \begin_layout Standard
1403 math-macro-make-optional
1404 \end_layout
1405
1406 \end_inset
1407 </cell>
1408 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1409 \begin_inset Text
1410
1411 \begin_layout Standard
1412 Edit/Math/Macro/Make First Non-Optional into Optional Parameter
1413 \end_layout
1414
1415 \end_inset
1416 </cell>
1417 </row>
1418 <row topline="true">
1419 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1420 \begin_inset Text
1421
1422 \begin_layout Standard
1423 math-macro-remove-greedy-param
1424 \end_layout
1425
1426 \end_inset
1427 </cell>
1428 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1429 \begin_inset Text
1430
1431 \begin_layout Standard
1432 Edit/Math/Macro/Remove Last Parameter Spitting Out To The Right
1433 \end_layout
1434
1435 \end_inset
1436 </cell>
1437 </row>
1438 <row topline="true">
1439 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1440 \begin_inset Text
1441
1442 \begin_layout Standard
1443 math-macro-make-nonoptional
1444 \end_layout
1445
1446 \end_inset
1447 </cell>
1448 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1449 \begin_inset Text
1450
1451 \begin_layout Standard
1452 Edit/Math/Macro/Make Last Optional into Non-Optional Parameter
1453 \end_layout
1454
1455 \end_inset
1456 </cell>
1457 </row>
1458 <row topline="true">
1459 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1460 \begin_inset Text
1461
1462 \begin_layout Standard
1463 math-macro-add-optional-param
1464 \end_layout
1465
1466 \end_inset
1467 </cell>
1468 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1469 \begin_inset Text
1470
1471 \begin_layout Standard
1472 Edit/Math/Macro/Insert Optional Parameter
1473 \end_layout
1474
1475 \end_inset
1476 </cell>
1477 </row>
1478 <row topline="true">
1479 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1480 \begin_inset Text
1481
1482 \begin_layout Standard
1483 math-macro-remove-optional-param
1484 \end_layout
1485
1486 \end_inset
1487 </cell>
1488 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1489 \begin_inset Text
1490
1491 \begin_layout Standard
1492 Edit/Math/Macro/Remove Optional Parameter
1493 \end_layout
1494
1495 \end_inset
1496 </cell>
1497 </row>
1498 <row topline="true" bottomline="true">
1499 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1500 \begin_inset Text
1501
1502 \begin_layout Standard
1503 math-macro-add-greedy-optional-param
1504 \end_layout
1505
1506 \end_inset
1507 </cell>
1508 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1509 \begin_inset Text
1510
1511 \begin_layout Standard
1512 Edit/Math/Macro/Append Optional Parameter Eating From the Right
1513 \end_layout
1514
1515 \end_inset
1516 </cell>
1517 </row>
1518 </lyxtabular>
1519
1520 \end_inset
1521
1522
1523 \end_layout
1524
1525 \begin_layout Subsubsection
1526 Greedy vs.
1527  Non-Greedy
1528 \end_layout
1529
1530 \begin_layout Standard
1531 As described above the key idea is that macros and eat up and spit out parameter
1532 s, depending on their arity.
1533  This makes them very dynamic and powerful, but at the same time adds some
1534  complexity which must be understood by the user to effectively use the
1535  macros.
1536 \end_layout
1537
1538 \begin_layout Standard
1539 Hence if the arity is increased (i.e.
1540  another parameter is added to a macro) there are two ways to do that: if
1541  this is done greedily the macro tries to eat up another inset from the
1542  right.
1543  This is the natural way if you import a document and then start to define
1544  a macros with LyX's math macros.
1545  Then you want that the macros take the (existing) paramenters from the
1546  right.
1547 \end_layout
1548
1549 \begin_layout Standard
1550 The second case is the non-greedy use case.
1551  E.g.
1552  you want to change a macro to take another parameter because you just found
1553  out that your notation needs another index.
1554  Then you want to insert this non-greedily.
1555  All macro instances in your text should get another parameter without touching
1556  the surrouindings.
1557 \end_layout
1558
1559 \begin_layout Standard
1560 The greedy variants of the actions have the word 
1561 \begin_inset Quotes eld
1562 \end_inset
1563
1564 greedy
1565 \begin_inset Quotes erd
1566 \end_inset
1567
1568  in their name.
1569 \end_layout
1570
1571 \begin_layout Standard
1572 Some of the actions also take a paramenter to define the position to act
1573  on in the parameter list.
1574  E.g.
1575  you can write 
1576 \family typewriter
1577 math-macro-add-param 2
1578 \family default
1579  in the mini-buffer to add a parameter at position 2.
1580 \end_layout
1581
1582 \begin_layout Subsubsection
1583 Folding/Unfolding
1584 \end_layout
1585
1586 \begin_layout Standard
1587 Sometimes it is desireable to switch to the TeX code of a macro instance,
1588  i.e.
1589  without any substitution using the macro definition.
1590  This can be done with the fold/unfold actions.
1591  You can use the 
1592 \family typewriter
1593 Ctrl-+
1594 \family default
1595  and 
1596 \family typewriter
1597 Ctrl--
1598 \family default
1599  shortcuts for that.
1600 \end_layout
1601
1602 \begin_layout Standard
1603 If you nest macro instances these actions will unfold from inside to the
1604  outside and the same for folding.
1605  This is supposed to replace the old list display when entering a macro.
1606 \end_layout
1607
1608 \begin_layout Subsubsection
1609 Toolbar
1610 \end_layout
1611
1612 \begin_layout Standard
1613 Currently there is no toolbar for math macros.
1614  Because the menu hierarchy is very deep a toolbar would make the life a
1615  lot easier.
1616  It shouldn't be hard to implement that.
1617 \end_layout
1618
1619 \begin_layout Subsubsection
1620 More natural macro definition editing
1621 \end_layout
1622
1623 \begin_layout Standard
1624 Instead of the described actions it would desirable to add another more
1625  natural way to edit macros.
1626  The vision is that the user can put the cursor everywhere inside of the
1627  macro definition inset which shows (already now!) the definition in the
1628  way
1629 \family typewriter
1630  
1631 \backslash
1632 name{#1}{#2}:={definition}
1633 \family default
1634 .
1635  The user should be able to use the backspace and the 
1636 \family typewriter
1637 {
1638 \family default
1639  key to remove and add parameters when the cursor is in the parameter definition
1640  part.
1641  For a non-greedy macro-append one could put a small (+) button or a hungry
1642  packman 
1643 \family typewriter
1644 (<
1645 \family default
1646  behind the 
1647 \family typewriter
1648 {#2}
1649 \family default
1650  in front of the definition.
1651 \end_layout
1652
1653 \begin_layout Standard
1654 For implementing this one has to customize the 
1655 \family typewriter
1656 MathInsetNest
1657 \family default
1658  a lot to handle the keypresses correctly, because it's probably not directly
1659  doable with 
1660 \family typewriter
1661 MathInsetNest
1662 \family default
1663  in a simple way with its children insets.
1664 \end_layout
1665
1666 \begin_layout Subsubsection
1667 Keyboard navigation
1668 \end_layout
1669
1670 \begin_layout Standard
1671 You can jump from 
1672 \family typewriter
1673 #n
1674 \family default
1675  to
1676 \family typewriter
1677  #n+1
1678 \family default
1679  with the cursor key and conversely backwards.
1680  This navigation is not visual.
1681  I.e.
1682  the user can define macros like 
1683 \family typewriter
1684
1685 \backslash
1686 foo{#1}{#2}:={#2+#1}
1687 \family default
1688 .
1689  Then the cursor jumps first to the right and then to the left where the
1690  #1 is.
1691  This can be confusing.
1692  One could think about a visual movement mode by taking the position of
1693  the macro argument insets into account to find the next inset for the cursor
1694  keys.
1695  This should be doable.
1696 \end_layout
1697
1698 \end_body
1699 \end_document