2

I want to define variable with letters and with numerical numbers. But the variable have same name with and without numeric value. Here is an example:

Basically I want to define

  \def\lam{\lambda}
  \newcommand{lam}{\lambda}
  \def\lam#1{\lambda^{#1}}

In the expression I have

  F = \lam + \lam2 + \lam3

The output should be

  F = \lambda + \lambda^2 + \lambda^3
BabaYaga
  • 157

2 Answers2

2

For example, you can do this:

\newcount\tmpnum
\def\lam{\lambda\afterassignment\lamA\tmpnum=0}
\def\lamA{\ifnum\tmpnum=0 \else ^{\the\tmpnum}\fi}

$ F = \lam + \lam2 + \lam3 $

wipet
  • 74,238
  • 1
    This is working perfectly. However to overcome this I have to add three lines of command!! – BabaYaga Jun 12 '20 at 18:25
  • 2
    @Boogeyman Only one \def line in needed for each new similar command, for example \def\delt{\delta\aftrerassignment\lamA\tmpnum=0}. In other words, the line 1 and 3 from the code above is common for each such macros. – wipet Jun 12 '20 at 18:35
  • one more question, if my expression also contains \lam and \lam1 , is there any way to force them to have same output? – BabaYaga Jun 13 '20 at 10:33
  • 1
    @Boogeyman Yes, define \def\lamA{\ifnum\tmpnum>1 ^{\the\tmpnum}\fi} – wipet Jun 13 '20 at 15:08
1

I found how to check if argument is a number here. And after you just output if that's not a number:

\documentclass{article}

\newcommand{\lam}[1]{% \ifnum0<0#1\relax $\lambda^{#1}$% \else $\lambda$~{#1}% \fi }

\begin{document}

F = \lam + \lam2 + \lam3

\end{document}