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