Writing macros - basic questions

Dear all,

I have just started writing some macros for TeXmacs. I have two basic questions

  1. What is a good way to write them so that they are easy to read?

For example: I have not yet understood what is the way to write a macro in multiple lines. I have tried to do it manually in a text editor, and the resulting macro is not accepted by TeXmacs. I haven’t either figured out how to indicate a continuation line.

In general, I find it difficult to learn the behaviour of the macro editor: sometimes a variable (let us call it var) gets inserted as <value|var> and I did not understand in which cases and pressing return in the middle of an already-written macro sometimes inserts the command <document| (did not check precisely and did not understand what it means).

Is there any documentation on this? Till now I did not find it.

  1. What is the return value of a TeXmacs macro? At first I assumed it must be the last expression which gives an output evaluated inside the macro, after some experiments it seems likely that all the “typesettable” results of the macro are considered its return value.
    This second question arose from my attempts to calculate a value using a macro, store it in a variable, use the variable as input for another macro.

If my questions indicate that I have not understood the principles of macros in TeXmacs, it is so :slight_smile:
I need a bit of time to adapt to functional programming. If I can edit macros with ease (see question 1) ) it will be quicker :slight_smile:

Giovanni

Giovanni, unfortunately I do not much about TeXmacs macros. Note that for complex manipulations you can use extern procedures written in scheme, which is more powerful. Miguel wrote some tutorials which you could find useful, they are mostly related to enhance TeXmacs in scheme but certainly he talks about manipulation of content, here: [http://www.texmacs.org/tmweb/contribute/team-miguel.en.html]

I would suggest you to look at some macro written by Joris (in the .ts files).

I should say that I also find the behaviour of the editor in source mode quite not intuitive, but knowing Joris is that because I still not understand the underlying principles. :slight_smile:

Hi -

I will follow the suggestion. I can do the work on Linux and there probably can edit Scheme with emacs, link to a TeXmacs document and test by “Update all”.

I do think having users exchange macros is helpful for the diffusion of TeXmacs, but I did not understand whether this opinion is shared by the developers.
Of course many user macros co-existing in a central place are not the same as a well-integrated software system (as now TeXmacs appears to me). On the other hand, keeping the two things (the well-integrated system and a collection of potentially incoherent user-written macros) separated should be possible and maybe even easy.

1 Like

writing macros will turn Texmacs into another Latex/Tex. The correct way is to use Scheme.

I started using Scheme since a while :slight_smile:

But I do not understand why you consider TeXmacs macros similar to TeX macros, to me they look different (I know almost nothing about these things, so my impression may be wrong).

I have only looked at the documents but not yet used macros. One big reason of my using Texmacs is I want to separate the task of typesetting and writing. Tex put typesetting and writing at the same place. Using macros is like mixing them again.

Perhaps TeXmacs macros are compatible with what you want: they are immediately typeset, so you can use them to enter tagged text which then you can immediately read in its formatted appearance.

You could try for example this one to format a physical quantity (= number plus unit)

<assign|SI|<macro|number|unit|<arg|number>
<with|math-font-family|ms|<with|math-font-shape|right|<arg|unit>>>>>

You enter it typing \SI and it gives you two fields inside its frame, you can insert the number in the first field and the unit in the second. I find it useful in math mode, where units consisting of a single character are by default typed in italics.

Something that I do not know is how to switch between the view of the macro as <SI|number|unit> and the view of the typeset macro. I have seen in another case (with the macro wide-std-framed-colored) the possibility of switching using the command “show hidden” from the contextual menu, I do not see it here.

interesting technique! any idea of how to do it in Scheme?

I will try to do it in Scheme, but please give me a few days (I do not have the time immediately). I will try and use the Scheme serialization of the TeXmacs tree (see here, section 5: https://www.texmacs.org/tmweb/manual/webman-format.en.html).

Oh yes. It should be a nice stuff.
Some typesetting conventions must be respected (a full or short space between the number and the unit):
The numerical value always precedes the unit, and an unbreakable space is always used to separate the unit from the number. The only exceptions to this rule are for the unit symbols for degree, minute, and second for plane angle, °, , and , respectively, for which no space is left between the numerical value and the unit symbol.

Unit symbols must be written in roman (straight) characters, regardless of the font used in the text where they are included” (The International Bureau of Weights and Measures BIPM).

I have tried you code via editing a tm file through standard editors. It does not seem to work. I have too little of experience with macros.

I have prepared a test file, but I cannot upload it to the forum. Perhaps @darcy knows how to do it? Otherwise I will paste the contents of the file in a message, it is very short.

I think you can just post it here. Thanks.

Just paste the contents of the file using ``` .

For example:

#include <stdio.io>
int main() {
  return 0;
}

Here it is

<TeXmacs|1.99.11>

<style|generic>

<\body>
  <assign|SI|<macro|number|unit|<arg|number>
  <with|math-font-family|ms|<with|math-font-shape|right|<arg|unit>>>>>

  <SI|3|m>
</body>

<\initial>
  <\collection>
    <associate|preamble|false>
  </collection>
</initial>

By the way I do not know how to set the macro definition in the “preamble” when I compose the file in the TeXmacs editor, so that it does not occupy a line of the typeset text. Thanks in advance for info on this.

It works. This is good. Thanks

I posted my first attempt in a new thread (Scheme code for SI)