8

I have been studying the way that custom enumeration labels work From source2e.pdf I get:

\def\alph#1{\expandafter\@alph\csname c@#1\endcsname}
\def\@alph#1{
     \ifcase#1\or a\or b\or c\or d\or e\or f\or g\or h\or i\or j\or
        k\or l\or m\or n\or o\or p\or q\or r\or s\or t\or u\or 
        v\or w\orx\ory\or z\else\@ctrerr\fi}

I have also been using the working example from this answer

\makeatletter
    \newcommand{\greekalpha}[1]{\c@greekalpha{#1}}
    \newcommand{\c@greekalpha}[1]{{
        \ifcase\number\value{#1} \or α \or β \or γ \or δ \or\fi}}
\makeatother

So I tried my own version of greek alphabet in an almost gargo cult manner to see what happens:

\makeatletter
    \def\greekc#1{\expandafter\@greekc\csname c@#1 \endcsname}
    \def\@greekc#1{\ifcase #1 \or α\or β\or γ\or δ\else\@ctreerr \fi}
\makeatother

Unfortunately it produces an error: 'Missing number, treated as zero' in the following example:

\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{mathspec}
\setmathsfont(Digits,Latin,Greek)[Uppercase=Regular,Lowercase=Regular,]        {Bookman Old Style}
\setmainfont{Bookman Old Style}
\usepackage{enumitem} 

\begin{document}
\newcounter{greekctr}\stepcounter{greekctr}\stepcounter{greekctr}

\makeatletter
    \def\greekc#1{\expandafter\@greekc\csname c@#1 \endcsname}
    \def\@greekc#1{\ifcase #1 \or α\or β\or γ\or δ\else\@ctreerr \fi}
\makeatother

\makeatletter
    \newcommand{\greekalpha}[1]{\c@greekalpha{#1}}
    \newcommand{\c@greekalpha}[1]{%
        {%
            \ifcase\number\value{#1} \or α \or β \or γ \or δ \or\fi
        }%
    }
\makeatother
\noindent
\begin{itemize}
    \item Value of counter: \thegreekctr
    \item Alph counter :\alph{greekctr}
    \item greekalpha: \greekalpha{greekctr}
    \item greekc: \greekc{greekctr}
\end{itemize}

Working use in enumerate environment:
\AddEnumerateCounter*{\greekalpha}{\c@greekalpha}{γ}
\begin{enumerate}[label=\greekalpha*]   
    \item First
    \item Second 
    \item Third
    \item Fourth
\end{enumerate}
\end{document}

Output

Could please somebody explain why my example does not work and also why the other two examples do?

10

You was getting an error because of an extra space inside \csname...\endcsname...

When you do \csname cs\endcsname you define a control sequence \cs because TeX ignores spaces after control sequence names. But when you do \csname cs \endcsname, the space after cs is considered and the control sequence you create is actually \cs␣ ( denotes a space).


Fun (?) fact: A lot of commands use a space after the name of the control sequence to store a "secret" version of the command. For example, if you create a document and put \show\cite in it, you'll see:

> \cite=macro:
->\protect \cite  .

then you'll ask: but then I cannot see the definition of \cite? Yes, you can! Notice that there is a space after the \cite in the second line. To see the \cite␣ macro you do:

\expandafter\show\csname cite \endcsname

with a space after cite :)


Also, there was an extra space after α, β, γ, and δ in \c@greekalpha. I removed them for you. This space was not causing an error but it would appear when printed in the pdf.

\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{mathspec}
\setmathsfont(Digits,Latin,Greek)[Uppercase=Regular,Lowercase=Regular,]        {Bookman Old Style}
\setmainfont{Bookman Old Style}
\usepackage{enumitem}

\begin{document}
\newcounter{greekctr}\stepcounter{greekctr}\stepcounter{greekctr}

% \tracingonline=1
% \tracingmacros=1

\makeatletter%                                    V no space here!
    \def\greekc#1{\expandafter\@greekc\csname c@#1\endcsname}
    \def\@greekc#1{\ifcase #1 \or α\or β\or γ\or δ\else\@ctreerr \fi}
\makeatother

\makeatletter
    \newcommand{\greekalpha}[1]{\c@greekalpha{#1}}
    \newcommand{\c@greekalpha}[1]{%
        {%                                v    v    v    v And no spaces here
            \ifcase\number\value{#1} \or α\or β\or γ\or δ\or\fi
        }%
    }
\makeatother
\noindent

\begin{itemize}
    \item Value of counter: \thegreekctr
    \item Alph counter :\alph{greekctr}
    \item greekalpha: \greekalpha{greekctr}
    \item greekc: \greekc{greekctr}
\end{itemize}

Working use in enumerate environment:
\AddEnumerateCounter*{\greekalpha}{\c@greekalpha}{γ}
\begin{enumerate}[label=\greekalpha*]
    \item First
    \item Second
    \item Third
    \item Fourth
\end{enumerate}
\end{document}
  • 1
    Thank you!!! You have no idea of the hours spent on this. – mhtsort Mar 22 '18 at 8:02
4

The problem is with \csname c@#1 \endcsname. There is a space after #1, so for example if #1 is section this results in \c@section with a final space in the name of the command sequence. This is not defined, so TeX finds an undefined control sequence instead of a number which leads to the error. To fix this you have to delete the space before \endcsname. The space after \csname is different. It is not included in the name of the command sequence because spaces after a command sequence like \csname are gobbled by TeX after functioning as a terminator of the name of the command sequence. It is even necessary, because otherwise \csnamec@ would be interpreted as one (undefined) command sequence.

  • 1
    Maybe you should rewrite that bit about "the space after \csname". This space is not only not a problem but mandatory. Else you try to use \csnamec@ which is not a defined control sequence. – Skillmon Mar 21 '18 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.