3

In user700902's answer to this post, there is the following for-loop:

\@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}}

If I want to put symbols like >, (, = along with a,b,c,..., how can I escape them?

Whole Document:

\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}
  • Except for braces, the symbols you mention can be inserted without escaping – JLDiaz Mar 9 '13 at 20:32
  • @JLDiaz I don't think so... I did \@for\@tempa:==\do{% \expandafter\colorizemath\@tempa{green}} and it doesn't work. – Mika H. Mar 9 '13 at 20:59
  • @MikaH. It works for me. Are you sure, you are between \makeatletter and \makeatother? Otherwise, explain what doesn’t work. Error message? Faulty output? Unexpected issues? (Unrelated to the question: = looses its \mathrel status with this.) – Qrrbrbirlbel Mar 9 '13 at 21:29
  • @Qrrbrbirlbel Not sure how helpful this is, but what happens is: It doesn't compile, and the error message is ! Bad mathchar (32768). <to be read again> \relax l.26 \begin{document} And yes, I'm between \makeatletter and \makeatother – Mika H. Mar 9 '13 at 21:34
  • @MikaH. this is why you should always post a complete document that shows the problem, most likely it is nothing to do with for loop but the syntax required of an assignment the = is optional except when it isn't for example \let\a b is the same as \let\a=b but if you want b to be = you have to have \let\a== as teh first = is taken as part of the assignment. – David Carlisle Mar 9 '13 at 21:44
7

You can avoid the error by just delaying switching the mathcode, so change

    \mathcode`#1="8000 

to

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

But a more serious problem is that you are making every definition {\color{abc}\olddefn} which makes every character a mathord and destroys the spacing (just of = in this example as letters get mathord spacing anyway) The following gets the math classof the original character and inserts \mathxx command into the definition so = gets \mathrel and the correct spacing:

enter image description here

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.