[Tex/LaTex] Substituting character $l$ with $\ell$ throughout math mode

math-modesymbols

How do I overwrite the default $l$ character so that it is substituted by $\ell$ throughout math mode?
(More generally, is there an easy way to make such symbol substitutions?)

Best Answer

This is similar to how MinionPro deals with open vs closed mathematical g's.

\documentclass{article}
\mathcode`l="8000
\begingroup
\makeatletter
\lccode`\~=`\l
\DeclareMathSymbol{\lsb@l}{\mathalpha}{letters}{`l}
\lowercase{\gdef~{\ifnum\the\mathgroup=\m@ne \ell \else \lsb@l \fi}}%
\endgroup
\begin{document}
\( l = 2 \) \( \lim = 2 \)
\end{document}