如何在 LaTeX 的 for 循环中转义 =

如何在 LaTeX 的 for 循环中转义 =

在 user700902 的回答中这个帖子,有以下for循环:

\@for\@tempa:=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\do{%
\expandafter\colorizemath\@tempa{green}}

如果我想将诸如>、、(以及=之类的符号放在一起a,b,c,...,我该如何转义它们?

整个文档:

\documentclass[fontsize=11pt]{scrartcl}
\usepackage{amsmath,amssymb,amsfonts,hyperref,color,xcolor}

\newcommand*{\mathcolor}{}
\def\mathcolor#1#{\mathcoloraux{#1}}
\newcommand*{\mathcoloraux}[3]{%
  \protect\leavevmode
  \begingroup
    \color#1{#2}#3%
  \endgroup
}

\makeatletter
\def\colorizemath #1#2{%
    \expandafter\mathchardef\csname orig:math:#1\endcsname\mathcode`#1
    \mathcode`#1="8000
    \toks@\expandafter{\csname orig:math:#1\endcsname}%
    \begingroup
       \lccode`~=`#1
       \lowercase{%
    \endgroup
       \edef~{{\noexpand\color{#2}\the\toks@}}}%
   }
\@for\@tempa:=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\do{%
    \expandafter\colorizemath\@tempa{blue}}
\@for\@tempa:=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\do{%
    \expandafter\colorizemath\@tempa{blue}}
\@for\@tempa:=0,1,2,3,4,5,6,7,8,9\do{%
    \expandafter\colorizemath\@tempa{blue}}
\@for\@tempa:==\do{%
    \expandafter\colorizemath\@tempa{brown}}
\def\m@th{\mathsurround\z@\color{black}}
\makeatother


\begin{document}

\title{Test File}

\author{ABC}

\maketitle

\section{Introduction}

The equation $1+1=2$ holds.

\end{document}

答案1

您可以通过延迟切换数学代码来避免错误,因此更改

    \mathcode`#1="8000 

    \AtBeginDocument{\mathcode`#1="8000 }

但是更严重的问题是,你所做的每个定义{\color{abc}\olddefn}都使每个字符成为一个 mathord 并破坏了间距(只是=在这个例子中,因为字母无论如何都会获得 mathord 间距)下面获取原始字符的数学类并将\mathxx命令插入到定义中以= 获得\mathrel正确的间距:

在此处输入图片描述

\documentclass[fontsize=11pt]{scrartcl}
\usepackage{amsmath,amssymb,amsfonts,hyperref,color,xcolor}

\newcommand*{\mathcolor}{}
\def\mathcolor#1#{\mathcoloraux{#1}}
\newcommand*{\mathcoloraux}[3]{%
  \protect\leavevmode
  \begingroup
    \color#1{#2}#3%
  \endgroup
}

\makeatletter
\def\colorizemath #1#2{%
    \expandafter\mathchardef\csname orig:math:#1\endcsname\mathcode`#1
    \AtBeginDocument{\mathcode`#1="8000 }
    \toks@\expandafter{\csname orig:math:#1\endcsname}%
\count@\mathcode`#1 %
\divide\count@"1000 %
    \begingroup
       \lccode`~=`#1
       \lowercase{%
    \endgroup
       \edef~{%
  \ifcase\count@
    \or
    \mathop\or
    \mathbin\or
    \mathrel\or
    \mathopen\or
    \mathclose\or
    \mathpunct\or
  \fi
{\noexpand\color{#2}\the\toks@}}}%
   }
\@for\@tempa:=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\do{%
    \expandafter\colorizemath\@tempa{blue}}
\@for\@tempa:=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\do{%
    \expandafter\colorizemath\@tempa{blue}}
\@for\@tempa:=0,1,2,3,4,5,6,7,8,9\do{%
    \expandafter\colorizemath\@tempa{blue}}
\@for\@tempa:==\do{%
    \expandafter\colorizemath\@tempa{brown}}
\def\m@th{\mathsurround\z@\color{black}}
\makeatother


\begin{document}

\title{Test File}

\author{ABC}

\maketitle

\section{Introduction}

The equation $1+1=2$ holds.

\end{document}

相关内容