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.

user21820
  • 868
  • 7
  • 19

2 Answers2

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

egreg
  • 1,121,712
  • Thank you, but I am unable to understand the line \begingroup\lccode`~=`=\lowercase{\endgroup\def~}{...}. Could you please explain it? – user21820 Oct 05 '20 at 08:50
  • @user21820 It's a very common trick for obtaining an active = (in this case) without doing explicit category code changes. – egreg Oct 05 '20 at 08: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 05 '20 at 09: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 08 '20 at 08:20
  • @user21820 See https://tex.stackexchange.com/q/156759/4427 – egreg Oct 08 '20 at 08:35
  • I have indeed looked at that and many other posts on TeX SE, but I couldn't find any that explained the begingroup and endgroup. From what I gather, I guess that the effect of \lccode somehow extends past the beginning of \lowercase expansion but not beyond that, but I think I am missing something. – user21820 Oct 08 '20 at 08:49
  • 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 08 '20 at 08:55
  • Ahh... so it's because the lowercase of active ~ was defined to be = so... Ok thanks it makes sense now. – user21820 Oct 08 '20 at 09:19
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}

Niranjan
  • 3,435