# Determine whether the first non-space character of #1 is a letter?

Let's say I have a class file with \title command, and I want to check that the title starts with a letter, and not with math or a number or a symbol. How can I do that?

This is my MnWE, the syntax of \@iffirstnotletter can be of course reasonably modified. If it recognized accented characters as well, it would be a bonus, but it's not necessary.

\documentclass{article}

\makeatletter
\newcommand\@iffirstnotletter[3]{#2} % <-- should be modified

\renewcommand\title[1]{\@iffirstnotletter{#1}{%
\GenericWarning{}{Please start the title with a letter!}%
}{}%
\def\@title{#1}%
}%
\makeatother

\title{Ahoj}

\begin{document}

\maketitle

\end{document}


EDIT:

My first idea was along the lines of this:

\def\ONETWO#1#2\ENDONETWO{#1} % strips the 1st token of
% whatever is between \ONETWO...\ENDONETWO
\def\@iffirstnotletter#1#2#3{%
% checks the \catcode of the 1st token of #1
\expandafter\ifnum\expandafter\the\expandafter\catcode
\expandafter\csname\ONETWO#1\ENDONETWO\endcsname=11\relax
#3\else#2
\fi}



!\iffirstletter{äoo}{true}{false}! = false % this may be misleading

!\iffirstletter{\relax}{true}{false}! = false

!\iffirstletter{}{true}{false}! = false

\end{document}

-
\UTFviii@two@octets{...} is what all UTF characters expand to, but that's not so important now. I can always state in the manual that the warning should be ignored in such cases. I'll, however, wait to see if someone finds a different approach that would solve the other issues, too. –  tohecz Jan 24 at 19:58
Ah, yes. I knew that once... One could add a test for that of course. (But wouldn't that then tie a user to utf8 as input encoding (what they should be using anyway)?) Or recommend XeLaTeX/LuaLaTeX where accented letters are letters :) –  cgnieder Jan 24 at 20:08
@tohecz not all characters, just ones that take two octets (some take 3 or 4) –  David Carlisle Jan 24 at 23:40

A long check with \futurelet; this covers UTF-8 multibyte characters, but in the case the first item in the argument is a multibyte character, it will give "good” anyway, as the concept of “UTF-8 letter” is not well defined. On the other hand, this will fail in cases such as “\'{U}jezd”. One could add a list of admissible control sequences for coping with this case.

\documentclass{article}
\usepackage[utf8]{inputenc}

\makeatletter
\newcommand\ifletter[1]{%
\futurelet\letter@token\checknextletter#1\relax\@nil
}
\def\checknextletter{%
\ifcat\noexpand\letter@token a%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\@gobbleto@nil{good}}%
{\checknextutfii}%
}

\def\checknextutfii{%
\expandafter\ifx\expandafter\UTFviii@two@octets\letter@token
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\@gobbleto@nil{good}}%
{\checknextutfiii}%
}

\def\checknextutfiii{%
\expandafter\ifx\expandafter\UTFviii@three@octets\letter@token
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\@gobbleto@nil{good}}%
{\checknextutfiv}%
}

\def\checknextutfiv{%
\expandafter\ifx\expandafter\UTFviii@four@octets\letter@token
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\@gobbleto@nil{good}}%
{\GenericWarning{}{Please start the title with a letter}%
}

\long\def\@gobbleto@nil#1#2\@nil{#1}

\makeatother

\begin{document}

\verb*'foo': \ifletter{foo} = good

\verb*' foo': \ifletter{ foo} = bad

\verb*'f12': \ifletter{f12} = good

\verb*'$math$': \ifletter{$math$} = bad

\verb*'$$math$$': \ifletter{$$math$$} = bad

\verb*'äoo': \ifletter{äoo} = good

`