2

I want to declare a series of commands \SA...\SZ as demonstrated here -- I want to avoid the additional \loopS command that is used though. However, merging the two lines (L1 and L2) into one (L3) does not quite work: when I call, e.g., \SA it outputs $\mathcal{Z}$ as, I guess, that is the value that is last stored in the loop variable \X. Is there a fix?

\documentclass{article}

% \def\loopS#1{\expandafter\def\csname S#1\endcsname{\mathcal{#1}}} (L1)

\newcounter{ctr}
\loop
  \stepcounter{ctr}
  \edef\X{\Alph{ctr}}
  %\expandafter\loopS\X (L2)
  \expandafter\def\csname S\X\endcsname{\mathcal{\X}} %(L3)
\ifnum\thectr<26
\repeat

\begin{document}
  $\SA$
\end{document}
ckamath
  • 167

1 Answers1

2

You need \edef; the step \edef\X{\Alph{ctr}} is not needed.

\documentclass{article}

\newcounter{ctr}
\loop
  \stepcounter{ctr}
  \expandafter\edef\csname S\Alph{ctr}\endcsname{\noexpand\mathcal{\Alph{ctr}}}
\ifnum\thectr<26
\repeat


\begin{document}
  $\SA$
\end{document}

An \edef free solution:

\documentclass{article}

\count255=`A \advance\count255 -1
\loop
  \advance\count255 1
  \begingroup\uccode`A=\count255
  \uppercase{\endgroup
    \expandafter\def\csname SA\endcsname{\mathcal{A}}%
  }
  \ifnum\count255<`Z
\repeat

\begin{document}

$\SA$

\end{document}

An expl3 solution:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\int_step_inline:nnnn { 1 } { 1 } { 26 }
 {
  \cs_new:cpx { S\int_to_Alph:n {#1} } { \exp_not:N \mathcal{ \int_to_Alph:n { #1 } } }
 }
\ExplSyntaxOff

\begin{document}

$\SA$ $\SZ$

\end{document}
egreg
  • 1,121,712