6

Is it possible to create macro names with special characters in it?

I would like to creates some macro's that have names like \s1.4 and also to be able to use them like \s1.4Test where Test is not treated as part of the macro name(basically anything after the float is excluded as being part of the macro name)

Basically this is to avoid having to do \s{1.4}Text which adds two extra characters and for my purposes \s1.4 has no issues(I will never have things like \s1.x and expect x not to be part of the macro so there will never be any ambiguity).

BTW, I'm defining these using a foreach:

\makeatletter
\foreach \m in {1.1, 2.2} 
{
    \expandafter\protected@xdef\csname s\m\endcsname{$_\mathbf{\m}$}
}
\makeatother

3 Answers3

6

enter image description here

plain TeX example but it would work in latex, \s is simpler but requires a . \t is more complicated but accepts an integer.

\def\s#1.{%
  \def\tmp{#1.}%
  \afterassignment\xxs\count0= }

\def\xxs{\csname s\tmp\the\count0\endcsname}


%%%%%%%%%%%%%%%%%%%

\def\t{\afterassignment\xxt\count0= }

\def\xxt{\futurelet\tmp\xxxt}

\def\xxxt{%
\if.\noexpand\tmp
\expandafter\xxxxt
\else
\csname s\the\count0\expandafter\endcsname
\fi}

\def\xxxxt.{\afterassignment\xtx\count2= }

\def\xtx{\csname s\the\count0.\the\count2\endcsname}


%%%%%%%%%%%%%%%%%%%




\expandafter\def\csname s1.4\endcsname{one point four }
\expandafter\def\csname s2.6\endcsname{ten point six }
\expandafter\def\csname s5\endcsname{five }

%%%%%%%%%%%%%%%%%%%

\s1.4more text \s2.6 and more



\t1.4more text \t2.6 and more and \t5 yet more



\bye
David Carlisle
  • 757,742
4

In the following example macro \s just collects digits and dots and typesets them as shown in the question as bold subscripts. LaTeX's \@ifnextchar cannot be used, because it gobbles spaces when it looks for the next token.

\documentclass{article}
\usepackage{ltxcmds}[2011/04/14]

\makeatletter
\newcommand*{\s}{%
  \begingroup
  \let\tmp\ltx@empty
  \s@aux
}
\newcommand*{\s@aux}{%
  \ltx@ifnextchar@nospace{.}{%
    \edef\tmp{\tmp.}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{0}{%
    \edef\tmp{\tmp0}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{1}{%
    \edef\tmp{\tmp1}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{2}{%
    \edef\tmp{\tmp2}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{3}{%
    \edef\tmp{\tmp3}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{4}{%
    \edef\tmp{\tmp4}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{5}{%
    \edef\tmp{\tmp5}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{6}{%
    \edef\tmp{\tmp6}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{7}{%
    \edef\tmp{\tmp7}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{8}{%
    \edef\tmp{\tmp8}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{9}{%
    \edef\tmp{\tmp9}%
    \expandafter\s@aux\ltx@gobble
  }{%
    \expandafter\s@end\expandafter{\tmp}%
  }}}}}}}}}}}%
}
\newcommand*{\s@end}[1]{%
  \endgroup
  \ensuremath{_\mathbf{#1}}%
}
\makeatother

\begin{document}
  \s1.4Test and \s2more.
\end{document}

Result

Update

It is easy to add the mapping layer including error messages, if \s is not followed by digits and dots or the digit/dot string is not defined.

\documentclass{article}
\usepackage{pgffor}
\usepackage{ltxcmds}[2011/04/14]

\makeatletter
\foreach \m in {1.1, 1.4, 2, 2.2}{%
  \expandafter\protected@xdef\csname s\m\endcsname{$_\mathbf{\m}$}%
}

\newcommand*{\s}{%
  \begingroup
  \let\tmp\ltx@empty
  \s@aux
}
\newcommand*{\s@aux}{%
  \ltx@ifnextchar@nospace{.}{%
    \edef\tmp{\tmp.}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{0}{%
    \edef\tmp{\tmp0}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{1}{%
    \edef\tmp{\tmp1}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{2}{%
    \edef\tmp{\tmp2}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{3}{%
    \edef\tmp{\tmp3}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{4}{%
    \edef\tmp{\tmp4}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{5}{%
    \edef\tmp{\tmp5}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{6}{%
    \edef\tmp{\tmp6}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{7}{%
    \edef\tmp{\tmp7}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{8}{%
    \edef\tmp{\tmp8}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{9}{%
    \edef\tmp{\tmp9}%
    \expandafter\s@aux\ltx@gobble
  }{%
    \ifx\tmp\@empty
      \@latex@error{\string\s: no digits/dots found}\@ehc
    \else
      \@ifundefined{s\tmp}{%
        \@latex@error{\string\s: Undefined `\tmp'}\@ehc
      }{%
        \@nameuse{s\tmp}%
      }%
    \fi 
    \endgroup
  }}}}}}}}}}}%
}
\makeatother

\begin{document}
  \s1.4Test and \s2more.
\end{document}

In the next example, the parsed argument of \s is a string that starts with one or more digits, an optional dots and optional digits.

\documentclass{article}
\usepackage{pgffor}
\usepackage{ltxcmds}[2011/04/14]

\makeatletter
\foreach \m in {1.1, 1.4, 2, 2.2}{%
  \expandafter\protected@xdef\csname s\m\endcsname{$_\mathbf{\m}$}%
}

\newcommand*{\s}{%
  \begingroup
  \let\s@tmp\ltx@empty
  \futurelet\s@tok\s@startnum
}
\newcommand*{\s@startnum}{%
  \s@digit\s@digitordot
  \s@next
}
\newcommand*{\s@digit}[1]{%
  \def\s@next{\s@add#1}%
  \ifx0\s@tok
  \else\ifx1\s@tok
  \else\ifx2\s@tok
  \else\ifx3\s@tok
  \else\ifx4\s@tok
  \else\ifx5\s@tok
  \else\ifx6\s@tok
  \else\ifx7\s@tok
  \else\ifx8\s@tok
  \else\ifx9\s@tok
  \else
    \let\s@next\s@end
  \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
}
\newcommand*{\s@add}[2]{%
  \edef\s@tmp{\s@tmp#2}%
  \futurelet\s@tok
  #1%
}
\newcommand*{\s@digitordot}{%
  \let\s@next\s@end
  \ifx.\s@tok
    \def\s@next{\s@add\s@endnum}%
  \else
    \s@digit\s@digitordot
  \fi
  \s@next
}
\newcommand*{\s@endnum}{%
  \s@digit\s@endnum
  \s@next
}
\newcommand*{\s@end}{%
  \ifx\s@tmp\@empty   
    \@latex@error{\string\s: no digits/dots found}\@ehc
  \else
    \@ifundefined{s\s@tmp}{%
      \@latex@error{\string\s: Undefined `\s@tmp'}\@ehc
    }{%
      \@nameuse{s\s@tmp}%
    }%
  \fi 
  \endgroup
}
\makeatother

\begin{document}
  \s1.4Test and \s2more.
\end{document}
Heiko Oberdiek
  • 271,626
  • This definitely works for my case but seems excessively long and not very robust. In my post I can generate the commands in a loop and define there numerical part(or any part) using a simple list and with another post I can map them to output something else. e.g., \s2 could be equivalent to \ensuremath{_\mathbf{\map{#1}}} where \map takes 2, in this case, and turns it into whatever I want. David seems to have figure out a good method that works with my original foreach and allows the mapping. – AbstractDissonance Aug 22 '12 at 13:52
1

If the second number after the period is only single digit, you can use the following:

\def\s#1.#2{$_\mathbf{#1.#2}$}
Aditya
  • 62,301