So sorry about that, I should really have looked at the issue tracker. But yes, 
it will probably break lots of pre-existing documents. So I've uploaded a new 
version of my patch that is backwards compatible.
I've added a checkbox at the bottom of Document Settings -> Math Options to 
always use \newcommandx​. The checkbox defaults to off, so it's an opt in 
feature. Alternatively, I could modify the lyx2lyx system so that files from an 
older format version have it off by default, but new versions have it on by 
default (which is probably what it should be).

I've attached three example lyx files that try and redefine the \section​ 
command; the only difference between def.lyx​ and new.lyx​ is whether the 
checkbox is checked or not; renew.lyx​ shows how to suecesfully do the 
redefinition.
I'm happy to integrate this with whatever automated testing setup LyX has, if 
someone could point me to any documentation for it, that would be greatly 
appreciated!.

The def.lyx​ file will work on LyX's without my changes, the new.lyx​ and 
renew.lyx​ files require the new changes.
The main part of the LaTeX for def.lyx​ is as follows:

\usepackage{babel}
\begin{document}
\newcommandx\section[0][usedefault, 
addprefix=\global]{\ensuremath{\mathsection}}%

Please See $\mathsection1$.

\section{Hello World}
\end{document}

The new.lyx​ file is the same, but changes the \def​​ line above to:
\newcommandx\section[0][usedefault, addprefix=\global]{\mathsection}%



Both versions give LaTeX errors, the first one:
! Missing $ inserted.
<inserted text>
                $
l.12 \section
             {Hello World}



And the second one:

! LaTeX Error: Command \section already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.9 ...sedefault, addprefix=\global]{\mathsection}
                                                  %


Clearly the second error message is much more helpfull.
Of course if you know what you're doing and really do want to redefine 
\section​ you can do it by first undefineing it in your preamble, which is what 
renew.lyx​ does (as well as make the new \section​ work outside math mode):

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\let\section=\undefined

\makeatother

\usepackage{babel}
\begin{document}
\newcommandx\section[0][usedefault, 
addprefix=\global]{\ensuremath{\mathsection}}%

Please See $\section1$.

\section{Hello World}
\end{document}

This version compiles fine.
Interstingly, sometimes you'll have to wrap the let  in \AtBeginDocument​ for 
example if you're redefining math commands defined by unicode-math.
— Isaac Oscar Gariano​

________________________________
From: lyx-devel <lyx-devel-boun...@lists.lyx.org> on behalf of Enrico 
Forestieri <for...@lyx.org>
Sent: Wednesday, 18 October 2023 7:50 AM
To: lyx-devel@lists.lyx.org <lyx-devel@lists.lyx.org>
Subject: Re: [PATCH] Always use \newcommandx instead of \def

On Tue, Oct 17, 2023 at 08:29:12PM +0200, Enrico Forestieri wrote:
>
>On Tue, Oct 17, 2023 at 01:10:23PM -0400, Richard Kimberly Heck wrote:
>>
>>On 10/16/23 21:22, Isaac Oscar Gariano wrote:
>>>Currently \def is used for all math macros that have no option
>>>arguments. This has caused me hard to debug errors because it
>>>silently clobbers pre-existing LaTeX commands. On the other hand,
>>>\newcommandx is used if there are optional arguments, and this
>>>gives a helpful error message if the command is already defined.
>>>(if you really do want to override the command, you can just put
>>>\let\mycommand=\undefined in your LaTeX preamble, although with
>>>unicode-math I often have to put this in an \AtBeginDocument).
>>>
>>>This very simple patch just makes LyX always output \newcommandx
>>>instead of \def for math macros.
>>>Unfortunately, this means if you want to have any math macros
>>>you'll always need the xargs package (but it's a small package
>>>release all they way back in 2008 and is included in TeXlive),
>>>hopefully this won't annoy users too much.
>>
>>Since we already depend upon xargs, this won't be a problem.
>>
>>
>>However, I think this might count as a format change, because it
>>changes the exported LaTeX. If so, then it would have to wait for
>>2.5.0. But I'm not sure about this, and perhaps the people who
>>actually know about this will think it's safe. I'd like to hear.
>
>Independently from that, this is a change that needs a lot of testing
>and it is risky to perform near a release.
>
>>You might want to have a look at 76dc2c0d3002db, which is where this
>>code was introduced. It looks to me as if the previous code was
>>actually very similar to what you have, but minus the prefix=\global
>>part, and that we could then just have added that. It's a bit
>>puzzling to me why Stefan did it the way he did, but he's long since
>>not around to answer that question.
>
>I seem to remember that he was actually forced to abandon the
>\newcommandx approach due to some technical reason. Maybe there is a
>bug report with details about this.

Anyway, this actually is the following enhancement request:
https://www.lyx.org/trac/ticket/11185

--
Enrico
--
lyx-devel mailing list
lyx-devel@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-devel

Attachment: 0001-Added-an-option-to-always-use-newcommandx.patch
Description: 0001-Added-an-option-to-always-use-newcommandx.patch

Attachment: def.lyx
Description: def.lyx

Attachment: new.lyx
Description: new.lyx

Attachment: renew.lyx
Description: renew.lyx

-- 
lyx-devel mailing list
lyx-devel@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-devel

Reply via email to