答案1
从 LyX 存储宏的方式(相对于文档)来看,在定义数学宏之前需要在序言中进行基本声明。
%% LyX 2.3.2-2 created this file. For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\newenvironment{lyxcode}
{\par\begin{list}{}{
\setlength{\rightmargin}{\leftmargin}
\setlength{\listparindent}{0pt}% needed for AMS classes
\raggedright
\setlength{\itemsep}{0pt}
\setlength{\parsep}{0pt}
\normalfont\ttfamily}%
\item[]}
{\end{list}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{mathtools}
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
\makeatother
\usepackage{babel}
\begin{document}
\begin{lyxcode}
\global\long\def\HW{\abs}%
% GlobalLongDef is the standard method LyX uses to store macros
% Here definition of HW for ABS is thus a simple substitution
% NOTE in the following that \HW* behaves exactly like \ABS*
~Hello~World~the~result~of~$\left|\frac{A}{X^{2}+Y^{2}}\right|$~in~LyX,~in~\textrm{\textbf{${\normalcolor T}_{E}^{X}$}}~is~${\HW*{\frac{A}{X^2+Y^2}}}$
\end{lyxcode}
\end{document}