3

I know that we can redefine inequality-signs such as \le by simply doing \let\oldle\le and \def\le{\mathrel{...}}. But I have no idea how to redefine the equality-sign =. So if I want to automatically make some symbols blue, I can do so for inequality-signs but not for equality-signs. What can I do to achieve automatically blue equality-signs in math mode? That is, I want to still just type = in math mode and it will automatically be made blue. I do not mind complicated code as long as it only needs to be inserted once.

6

You need to make = math active.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}

% set up = to be math active
\begingroup\lccode`~=`=\lowercase{\endgroup\def~}{\mathrel{\textcolor{blue}{\standardequal}}}
\AtBeginDocument{%
  \mathchardef\standardequal=\mathcode`= % save the standard =
  \mathcode`=="8000 % make = math active
}

\begin{document}

\begin{gather*}
a=b \\
a\standardequal b % for comparison
\end{gather*}

\end{document}

enter image description here

8
  • Thank you, but I am unable to understand the line \begingroup\lccode`~=`=\lowercase{\endgroup\def~}{...}. Could you please explain it?
    – user21820
    Oct 5 '20 at 8:50
  • @user21820 It's a very common trick for obtaining an active = (in this case) without doing explicit category code changes.
    – egreg
    Oct 5 '20 at 8:54
  • Can you give me a reference where I can learn this tricks and maybe other useful tricks as well? I have no idea what this one is doing.
    – user21820
    Oct 5 '20 at 9:03
  • I accepted your answer because it did what I asked for, but I still have absolutely no idea how it works, so it would be good if you can point me to some reference for at least this trick. For one, the \begingroup and \endgroup do not seem matched.
    – user21820
    Oct 8 '20 at 8:20
  • 1
    @user21820 The argument of \lowercase is processed, changing letters to their lowercase counterparts based on the current values of the \lccode array *without changing the category code, only the character code. Then the tokens are examined anew; however, the braces have already disappeared, so the \endgroup balances the \begingroup making just the assignment to \lccode to be undone. Thus at the end, TeX sees \def={...} (but the = has category code 13, active).
    – egreg
    Oct 8 '20 at 8:55
1

With LuaLaTeX, you can do that. See the following example. (Special thanks to @Mico's answer.)

%!TeX Program = lualatex
\documentclass{article}
\usepackage{xcolor}
\usepackage{luacode}
\begin{luacode}
    function myreplace ( s )
    s = unicode.utf8.gsub ( s , '=', '\\textcolor{blue}{=}' )
    return s
    end
\end{luacode}

\begin{document}
\directlua{luatexbase.add_to_callback("process_input_buffer",
myreplace, "myreplace")}
=
\end{document}
1
  • 1
    This is a really bad idea, because this would also match = in contexts like \looseness=1. Oct 14 '20 at 12:14

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.