2

I want to find a way how to automatically change "l" into "\ell" in a math mode.

I tried to do it using \catcode, but it's quite difficult for me.

I want it to change "l" only when it appears alone.

(e.g., do not change "l" in \log or if I define a variable lb as follows:

\[\log l = lb + l^2\]

I want to change the second and the fourth "l" only)

It would be even better if I can typeset the original "l" by using "\ell" instead (i.e., change the role of them)

Thanks in advance.

2 Answers2

7

A proof of concept. You need to mark up the multiletter variables anyway, because you don't want that your reader mistake them for products.

\documentclass{article}

\mathchardef\standardl=\mathcodel \mathcodel=\ell \newcommand{\deactivatel}{\mathcode`l=\standardl} \makeatletter \edef\operator@font{\operator@font\noexpand\deactivatel} \makeatother

\newcommand{\mlvar}[1]{\mathit{\deactivatel #1}}

\begin{document}

[ \log l+\mlvar{lb}+l^2 ]

\end{document}

The letter l is assigned as mathcode the one of \ell. But in some places (operators and multiletter variables) it is set back to the original.

enter image description here

However, you should use \ell when you want it (and in my opinion one never needs it).

egreg
  • 1,121,712
3

Here's a LuaLaTeX-based solution. The solution provides a Lua function called l2ell that does most of the actual work, along with two utility LaTeX macros, \lellOff and \lellOn, which deactivate and reactivate the l2ell function.

This approach will admittedly go astray on $\l$. However, as \l is a text-mode command (for the Polish "soft ell", to be specific), it shouldn't occur in math-y circumstances, right?

enter image description here

\documentclass{article}
\usepackage{luacode}
\begin{luacode}
   function l2ell ( s )
      s = s:gsub ( "^l(%A)" , "\\ell %1" )
      s = s:gsub ( "(%A)l$" , "%1\\ell" )
      s = s:gsub ( "(%A)l(%A)" , "%1\\ell %2" )
      return s 
   end
\end{luacode}

% LaTeX macros to activate and deactivate the Lua function: \newcommand\lellOn{\directlua{luatexbase.add_to_callback ( "process_input_buffer" , l2ell , "l2ell" )}} \newcommand\lellOff{\directlua{luatexbase.remove_from_callback ( "process_input_buffer" , "l2ell" )}} \AtBeginDocument{\lellOn} % activate the Lua function by default

\begin{document} $\log l +\ln l= lb + l^2$, $l$, $ll$, $l + l$, $ l ^ l$

\lellOff % deactivate the Lua function $\log l +\ln l= lb + l^2$, $l$, $ll$, $l + l$, $ l ^ l$ \end{document}

Mico
  • 506,678
  • Really awesome! (except for the fact that I have to figure out how to compile my tex file with LuaLaTeX). Thanks a lot! – user182849 Dec 04 '21 at 23:07
  • 1
    @user182849 - Which front-end editor do you use? If it has a drop-down menu to select which program to run, chances are that "lualatex" will be one of the options. – Mico Dec 05 '21 at 00:52
  • Thanks for the follow up. I am using texmaker, and now I have figured out how to compile it with lualatex! Your solution works well. I should read some articles like https://tex.stackexchange.com/questions/28642/frequently-loaded-packages-differences-between-pdflatex-and-lualatex before moving to LuaLaTeX, though:) – user182849 Dec 05 '21 at 01:41