在 LyX 中添加 TeX 代码作为宏

在 LyX 中添加 TeX 代码作为宏

我正在使用 LyX,并尝试添加一个包含 TeX 代码的宏,但它似乎无法识别该\abs命令:

在此处输入图片描述

需要澄清的是,当添加以下 TeX 代码时,文件会进行编译:

在此处输入图片描述

并且绝对值在导出的 pdf 文件中正确显示:

在此处输入图片描述

任何帮助,将不胜感激。

答案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}

相关内容