Customizing the "Coloring according to nesting level"

Hello everyone, I have recently discovered the “Coloring according to nesting level” feature of TeXmacs. Once enabled in the tag preferences of the math parentheses (“around” environment), individual levels parentheses in math expression are colored corresponding to their nesting level as shown in the image bellow:


This feature is not mentioned in the Jolly writer book nor in the TeXmacs manual.

I am looking for a way to change the colors of the individual nesting levels. For this I searched the TeXmacs source code. The actual preference is called math-brackets and is defined in the file TeXmacs/packages/customize/math/math-brackets.ts by a oneliner <assign|math-nesting-mode|colored>. Further search led me to file src/Typeset/Concat/concat_math.cpp where the code realizing the coloring is located. The important part of code is :

static string
bracket_color (int nl) {
  switch (nl % 3) {
  case 0 : return "#662266";
  case 1 : return "#226666";
  default: return "#663322";
  }
}


void
concater_rep::typeset_around (tree t, path ip, bool colored) {
  tree old_nl=
    env->local_begin (MATH_NESTING_LEVEL, as_string (env->nesting_level + 1));
 if (colored) {
    tree old_col= env->local_begin (COLOR, bracket_color (env->nesting_level));
    typeset_around (t, ip, false);
    env->local_end (COLOR, old_col);
 }
 ...

This unfortunately means that the colors are hard coded into the C++ code. I would like to ask, if there is a way around how to change the colors without having to recompile everything.
Thanks in advance for advice.

Once this question is closed I would like to compile the information of this feature it into something short and easily readable and add it into the Undocumented scripts wiki thread.

I need to think a bit.

It looks possible to use Scheme to apply a tag to color math when one is typing, and to do this according to the nesting level; but math expressions do not get changed only when one is typing: in fact, you might have macros that modify math, for example adding or removing nesting levels, and I think that math color should be “robust” with respect to that.

Perhaps this is the reason why the coloring is done at typesetting and not through tags. Let us see if anyone else says something.

And: welcome to the forum!

Hi @FrantaNautilus, welcome to the forum!

Interesting idea! An option would be to define a preference setting for the colours and read them from the C++ code. The relevant function is get_preference, see initialize_color_decodings in language.cpp for an example.

A downside of this approach is that it makes the document rendering dependent on the ser’s settings. It’s also possible to define variables that can be set in the document and then read by the C++ code, much like the math-nesting-mode variable you refer to. Look also at how e.g. CURSOR_COLOR is defined and used in the C++ code, and how it is linked to the cursor-color document environment variable.

Hello @pireddag and @jeroen and thank you for a warm welcome. I am happy that TeXmacs has got such a great forum and community.

As for the suggestions, I see how the C++ solution should work and I will probably try to program it. First I will have to refresh my C++ and prepare an installation on my NixOS computer (to use the overlay for the source patching), so that I do not break my “work” installation of TeXmacs. Please give me some time :slight_smile:.

I would like to ask two more things that I believe are not too of topic to be a separate threads. Firstly if I manage to implement the feature, is the preferred way to implement such things via Pull requests on Github or is the Savanah the way to go? And secondly, while working with the editor I often come across some undocumented features (for example the fact that a definition environment with a glossary explain in the dueto tag leads to the references being the abbreviation instead of a number), I think these should be part of the built in documentation. Should I then include such additions in a hypothetical pull request?

Great to hear you want to have a go at it. Let us know if you need any hints on setting up a development environment.

The master source of TeXmacs is in subversion on Savannah. You can submit a patch to it on the Savannah website.

That said, TeXmacs is developed in a fairly conservative manner to preserve stability. You could consider submitting a PR to Mogan, a fork of TeXmacs driven by @darcy. They accept pull requests on several git platforms, including GitHub and the freedom respecting Codeberg.

@darcy and collaborators are doing some marvelous and important work on Mogan. They are developing new features, in conjunction with more and automated testing to ensure things don’t break drastically. Hopefully it can serve as a testing ground for features to later move into TeXmacs.

You probably have more success submitting contributions there first.

Documentation would be very very welcome, but probably best to put it in a separate PR.

1 Like

UPDATE: I am still working on becoming familiar with Texmacs source. Due to the fact that I am a university student and I need to pass some exams, I need ask everyone for patience as I am postponing the work a little.

2 Likes