I am trying to define a macro \@ifnextchars with three arguments, the first argument being a list of characters, which expands to either the second or the third argument depending on whether one of the character of the first argument follows the command.
Here follows a minimalistic example of what I have done and how I would like to use it.
\documentclass{article}
\makeatletter
\newcommand\@ifnextchars[3]{%
\expandafter\ifx\expandafter\@empty#1\@empty%
\def\@ifnextchars@tmp{#3}%
\else%
\def\@ifnextchars@tmp{%
\expandafter\expandafter\expandafter\@ifnextchars@aux\expandafter%
{\@car#1\@nil}{\@cdr#1\@nil}{#2}{#3}%
}%
\fi%
\@ifnextchars@tmp%
}
\newcommand\@ifnextchars@aux[4]{%
\expandafter\@ifnextchar#1{%
#3%
}{%
\@ifnextchars{#2}{#3}{#4}%
}%
}
\newcommand\whereinsentence{%
\@ifnextchars{.!?}{%
I am in the end of a sentence.
}{%
I am not in the end of a sentence.
}%
}
\newcommand\vowel{%
\@ifnextchars{aeiouy}{%
Vowel:
}{%
Not vowel:
}%
}
\makeatother
\begin{document}
Here is \whereinsentence a sentence\whereinsentence.
\vowel a.
\vowel b.
\end{document}
Unfortunately, it doesn’t compile:
Runaway argument?
{I am in the end of a sentence. }{\@ifnextchars {\@cdr \@cdr .!?\@nil \ETC.
! Paragraph ended before \@cdr was complete.
<to be read again>
\par
l.44
My guess is that \@cdr didn’t expand when needed. I find this strange because I made sure to expand my \@cdr with \expandafter…
So I guess that I do not understand well how \expandafter works ☹
Can someone help me?
I was inspired by this post: Generalize \@ifnextchar to consider more than one character.
However, this last post was about creating a variant of \@ifnextchar looking for entire word. I am just interested in knowing whether one of the given characters are present directly afterwards.
I have made this \ifx yielding a \def to make sure that the \@ifnextchar does not react with the \fi. I do not find this very satisfying: if anyone has a better solution, I am welcoming it with pleasure ☺
Thanks in advance.
\@addpunctdefined inamsthm.sty. You're expanding\@car, but not\@cdr. – egreg Jul 23 '16 at 19:56\@addpunctis of few aid for what I am trying to do. But thanks ☺ – Martin Bodin Jul 25 '16 at 07:49