Overwrite \def to check if the command exists?

LyX supports math-macros, but sadly when passed to the latex processor, they are defined by a \def rather than a newcommand.

I have found overwriting commands (e.g. using \def\v{\mathbf{v}}) to result in hard-to-debug errors, especially when BibTeX files entries introduce names, that require the original meaning of the command. Hence I'd like to change this behaviour -- asking for a change in LyX would however be unlikely to be successful (breaking backward compatibility) and at best require waiting for the next binary release (windows) or compiling it myself with a patch (linux).

Hence I was wondering if it is possible to (locally) overwrite \def to call \newcommand instead.

I tried

\documentclass[]{article}
\begin{document}

\begingroup % for locality of the redefinition
\let\odef\def
\def\xx{{\bf x}} \xx
\endgroup

\end{document}


but this fails with

! TeX capacity exceeded, sorry [input stack size=5000].
\@ifstar #1->\@ifnextchar *{\@firstoftwo {#1
}}
l.18  \def\xx
{{\bf x}} \xx


presumably related to \newcommand using \def internally.

• \@ifdefinable or \ifundefined might help here. – Manuel Nov 7 '14 at 14:23
• If you think that LyX could be improved someway (even though you've solved your own problem, perhaps you want to improve it for others), please do file an enhancement request at lyx.org/trac – scottkosty Nov 7 '14 at 14:40
• \bf was deprecated 20 years ago... – cfr Nov 8 '14 at 2:41

\documentclass[]{article}
\begin{document}

\let\odef\def
\odef\def#1{\odef\dummy{}{\let\def\odef\newcommand{#1}{}}\odef#1}
% <LYXDEFINITIONS>
\global\long\def\xx{{\bf x hello}} \xx
\global\long\def\yy{{\bf y world}} \yy
% </LYXDEFINITIONS>
\let\def\odef

\end{document}


The trick was to return the original definition to \def for the duration of \newcommand.

Now only the question of undesirable side effects remains (though when used only for LyX's math macros there shouldn't be any).

• Do you mean that passing LyX something like \newcommand{\xx}{\mathbf{x}} is not accepted? Every day I discover something that makes me happy of not having any trace of that software on my machines. – egreg Nov 10 '14 at 16:24
• @egreg We are very open to bug reports and enhancement requests. I think most of your objects to LyX are about its philosophy, but if you have suggestions that you think would make things better for LyX users, we would really value your feedback. Please open any tickets at lyx.org/trac or send an email to lyx-devel@lists.lyx.org. I imagine it would be painful for you to spend time helping to improve software you fundamentally disagree with though. – scottkosty Nov 10 '14 at 17:23
• @scottkosty Sorry. My best wishes for the software you're developing; but my opinion is that a simple document is better written directly with LaTeX and, when things get complex, LyX doesn't seem to help. – egreg Nov 10 '14 at 17:54
• @egreg please don't be sorry. I understand your opinion. Thank you for the kind wishes and feedback. – scottkosty Nov 10 '14 at 18:28
• @egreg: LyX accepts literal \newcommand just fine. The advantage of LyX mathmacros however is that the macro will be visually represented by its expansion in the equation editor, making equations more readable and easier to edit (my main reason for using LyX over plain-text LaTeX). Mathmacros however use \def and thus nothing warns you about having redefined an existing macro, until it causes errors. By that time the macro may occur dozens of times across several .lyx files, and you have to change each use of it. Note that the issue also applies in LaTeX when defining user-macros with \def – kdb Nov 12 '14 at 14:03