![]() |
Writing a simple style package | ![]() |
Let us explain on an example how to write a simple style package. First of all, you have to create a new buffer using File\(\rightarrow\)New and select the source document style using Document\(\rightarrow\)Style\(\rightarrow\)source. Now save your empty style package in your personal style package directory
$HOME/.TeXmacs/packages
Similarly, the directory
$HOME/.TeXmacs/styles
contains your personal style files. After saving your empty style package, it should automatically appear in the Document\(\rightarrow\)Style\(\rightarrow\)Add package menu. Notice that style files must be saved using the .ts file extension. If you save the style file in a subdirectory of $HOME/.TeXmacs/packages, then it will automatically appear in the corresponding submenu of Document\(\rightarrow\)Style\(\rightarrow\)Add package.
Let us now create a simple macro hi which displays “Hello world”. First type ⌥⌃=, so as to create an assignment. You should see something like
<assign||> |
Now enter “hi” as the first argument and type ⌥⌃M inside the second argument in order to create a macro. You should now see something like
<assign|hi|<macro|>> |
Finally, type the text “Hello world” in the body of the macro. Your document should now consist of the following line:
<assign|hi|<macro|Hello world>> |
After saving your style package, opening a new document and selecting your package in the Document\(\rightarrow\)Style\(\rightarrow\)Add package menu, you may now use the macro hi in your document by typing \HI and hitting ↩.
In a similar way, you may create macros with arguments. For instance, assume that we started entering a macro hello in a similar way as above. Instead of typing “Hello world”, we first type ⌃← inside the macro body so as to create an additional argument on the left hand side of the cursor. We next enter the name of the argument, say “name”. You should now see something like below:
<assign|hello|<macro|name|>> |
In the second argument of the body, we now type “Hello ”, ⌥⌃#, “name”, → and “, how are you today?”. After this you should see
<assign|hello|<macro|name|Hello name, how are you today?>> |
The ⌥⌃# shortcut is used to retrieve the macro argument name. Instead of typing ⌥⌃#, “name” and →, you may also use the hybrid \-key and type \NAME followed by ↩. After saving your style package, you may again use the macro in any document which uses your package by typing \HELLO and hitting ↩.
From the internal point of view, all macro definitions are stored in the environment of the TeXmacs typesetter. Besides macros, the environment also contains normal environment variables, such as section counters or the font size. The environment variables can either be globally changed using the assign primitive, or locally, using the with primitive. For instance, when including the line
<assign|section-nr|-1> |
in your package, and using article as your major style, then the first section will be numbered \(0\). Similarly, the variant
<assign|hello|<macro|name|Hello <with|font-shape|small-caps|name>!>> |
of the hello macro displays the name of the person in Small Capitals. Notice that the with primitive can also be used to locally redefine a macro. This is for instance used in the definitions of the standard list environments, where the macro which renders list icons is changed inside the body of the list. Yet another variant of the hello macro relies on the standard person macro:
<assign|hello|<macro|name|Hello <person|name>!>> |
In order to produce the macro application <person|name>, you first have to start a compound tag using ⌥⌃C, type the name “person”, insert an argument ⌃→, and enter the argument name as before. When you are done, you may press ↩ in order to change the compound tag into a person tag. Alternatively, you may type \, “person”, ⌃→ and “name”.
By combining the above constructs, an ordinary user should already be able to produce style packages for all frequently used notations. An interesting technique for writing macros which involve complex formulas with some subformulas which may change goes as follows:
Type the formula, say \((a_1, \ldots, a_n)\), in an ordinary document.
Create the skeleton of your macro in your style package:
<assign|n-tuple|<macro|a|>> |
Copy the formula and paste it into the body of your macro:
<assign|n-tuple|<macro|a|(a<rsub|1>,…,a<rsub|n>)>> |
Replace the subformulas you want to parameterize by macro arguments:
<assign|n-tuple|<macro|a|(a<rsub|1>,…,a<rsub|n>)>> |
You may now use the macro in documents which use your package: