Subject: bug#78693: 14.0.9; Folding of math macros with a function spec is broken

From: Rahguzar <[email-masked]>
Date: Sun, 22 Jun 2025 15:37:06 +0500

Hi Paul,

"Paul D. Nelson" <[email-masked]> writes:

> Hi Raghuzar,
>
> Rahguzar <[email-masked]> writes:
>
>>>> One question about it: One problem I have had is that, if I have
>>>> something like
>>>>
>>>> \begin{align}
>>>>   [\hat{X},\hat{P}] = i\hbar
>>>> \end{align}
>>>>
>>>> the commutator gets folded as an optional argument to the \begin.
>>>> It is possible to not have this effect with new machinery without
>>>> specifying \begin as not taking any optional arguments?
>>>
>>> Could you please share your tex folding config, or better, the part
>>> relevant for folding \begin{align}?
>>
>> It is this:
>>
>> (setq TeX-fold-macro-spec-list
>>       `(("⬖ {1}" ("begin"))
>>         ("⬗ {1}" ("end"))))
>
> With the new folding signature support, you can achieve the desired
> effect using either
>
> ;; At most 1 args.
> (setq TeX-fold-macro-spec-list
>       `((("⬖ {1}" . 1) ("begin"))
>         (("⬗ {1}" . 1) ("end"))))
>
> or
>
> ;; At most 0 optional, 1 required args.
> (setq TeX-fold-macro-spec-list
>       `((("⬖ {1}" . (0 . 1)) ("begin")) 
>         (("⬗ {1}" . (0 . 1)) ("end"))))
>
> ---

Thanks, this works for my needs. I was worried about optional arguments
before the mandatory ones messing up the folding but those arguments
actually get swallowed by the folding. With your suggestion of using
TeX-fold-macro-nth-arg it is possible to handle optional args that come
before mandatory ones which passing a 1 or (0 . 1) as spec. A bit
confusing but it improves on the current situation and I don't think it
is possible handle folding perfectly given how "flexible" syntax in
(La)TeX is.

> Another approach here would be prettification, assuming you're OK with
> specifying each environment.  I use something like:
>
> (with-eval-after-load 'tex-mode
>   (mapc
>    (lambda (sym) (add-to-list 'tex--prettify-symbols-alist sym))
>    '(("\\begin{equation}" . ?↴)
>      ("\\end{equation}" . ?↲)
>      ("\\begin{align}" . ?⌈)
>      ("\\end{align}" . ?⌋)
>      ("\\begin{multline}" . ?⎧)
>      ("\\end{multline}" . ?⎭))))
>
> Paul

Rahguzar