我怎样才能让每个出现的“+”和“-”都被宏替换,但仅限于数学模式?

我怎样才能让每个出现的“+”和“-”都被宏替换,但仅限于数学模式?

我决定利用现有的答案来拼凑一个答案:https://tex.stackexchange.com/a/127507/49339

经过大量简化后,我的 MnotWE 如下所示:

\documentclass[12pt,letterpaper]{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\makeatletter
% LaTeX's \@ifnextchar gobbles spaces, therefore
% \msh@ifnextchar is defined that keeps spaces
\newcommand*{\msh@ifnextchar}[3]{%
    \def\msh@temp{\msh@@ifnextchar{#1}{#2}{#3}}%
    \futurelet\msh@token\msh@temp
}
\newcommand*{\msh@@ifnextchar}[1]{%
    \ifx\msh@token#1%
    \expandafter\@firstoftwo
    \else
    \expandafter\@secondoftwo
    \fi
}

% Commands that take the original meanings of the special characters
\mathchardef\msh@code@minus=\mathcode`\-\relax
\mathchardef\msh@code@plus=\mathcode`\+\relax

% Macro \setmathshorthands activates and defines the special
% characters
\begingroup
\@ifdefinable{\setmathshorthands}{%
    \xdef\setmathshorthands{%
        \let\noexpand-\noexpand\msh@minus
        \let\noexpand+\noexpand\msh@plus
    }%
}%
\endgroup

\newcommand*{\msh@minus}{\&}

\newcommand*{\msh@plus}{\$}

\makeatother

% Activate math shorthands in the math modes
\everymath{\setmathshorthands}
\everydisplay{\setmathshorthands}


\begin{document}
    \begin{equation*}
        a + b - c = 0
    \end{equation*}
\end{document}

我目前使用的宏只是玩具示例,我有一些更复杂的宏,最终我会用它们来替换它们。不过,目前,我们先使用这些玩具。

无论如何,编译此示例时,我收到错误:

第 50 行:插入缺失的控制序列。\begin{equation*}

做什么?

答案1

在小组中,你需要

  \catcode`\-=\active
  \catcode`\+=\active

并且\setmathshorthands,你需要

  \mathcode\number`\-="8000 %
  \mathcode\number`\+="8000 %

这些\catcode变化是必要的,以便-+被[暂时]视为活动字符,以便在未来的调用中定义它们的数学活动行为\setmathshorthands

感谢楼主引用了以下\mathcode信息mathcode 和 catcode 之间有什么区别以及如何使用 mathcode?,这是一种让这些字符仅在数学模式下被视为活动的方法,即使它们实际上并未活动:

\mathcode"8000是一种不以常规方式查找的特殊代码。如果字符具有该数学代码,则将使用活动(catcode 13)标记的定义,即使字符本身不是活动的。这在纯文本和 LaTeX 中用于允许 ' 在文本中充当正常的非活动撇号,但在数学中它具有 catcode hex 8000,因此使用活动定义,扩展为^{\prime}

-MWE(也测试在文本模式下的使用):

\documentclass[12pt,letterpaper]{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\makeatletter
% LaTeX's \@ifnextchar gobbles spaces, therefore
% \msh@ifnextchar is defined that keeps spaces
\newcommand*{\msh@ifnextchar}[3]{%
    \def\msh@temp{\msh@@ifnextchar{#1}{#2}{#3}}%
    \futurelet\msh@token\msh@temp
}
\newcommand*{\msh@@ifnextchar}[1]{%
    \ifx\msh@token#1%
    \expandafter\@firstoftwo
    \else
    \expandafter\@secondoftwo
    \fi
}

% Commands that take the original meanings of the special characters
\mathchardef\msh@code@minus=\mathcode`\-\relax
\mathchardef\msh@code@plus=\mathcode`\+\relax

% Macro \setmathshorthands activates and defines the special
% characters
\begingroup
  \catcode`\-=\active
  \catcode`\+=\active
\@ifdefinable{\setmathshorthands}{%
    \xdef\setmathshorthands{%
      \mathcode\number`\-="8000 %
      \mathcode\number`\+="8000 %
        \let\noexpand-\noexpand\msh@minus
        \let\noexpand+\noexpand\msh@plus
    }%
}%
\endgroup

\newcommand*{\msh@minus}{\&}

\newcommand*{\msh@plus}{\$}

\makeatother

% Activate math shorthands in the math modes
\everymath{\setmathshorthands}
\everydisplay{\setmathshorthands}


\begin{document}
A-b--c---d dddddddddddd dddddddddddd ddddddddddddd dddddddd fffff\-ggg
    \begin{equation*}
        a + b - c = 0 \quad \text{ravings of a mad-man}
    \end{equation*}
\end{document}

在此处输入图片描述

相关内容