General\@ifnextchar for more characters

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

\@ifnextchar for more characters

Post by Laurentius »

Is there a simpler way to check whether the next character is one of several than nesting \@ifnextchars ?
Last edited by cgnieder on Sun Oct 13, 2013 6:23 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

\@ifnextchar for more characters

Post by cgnieder »

How about this:

Code: Select all

\documentclass{article}
\usepackage{etoolbox,ltxcmds}
\makeatletter
% --------------------------------------------------------------------------
% list parser with no separator:
\DeclareListParser*\laurentius@add@tokens{}

% the list that holds the tokens to be checked:
\def\laurentius@tokens@list{}

% set the tokens list (the parser does not like blank lists):
\newrobustcmd*\laurentius@set@tokens[1]{%
  \def\laurentius@tokens@list{}%
  \ifblank{#1}
    {}
    {\laurentius@add@tokens{\listadd\laurentius@tokens@list}{#1}}%
}

% --------------------------------------------------------------------------
% check for token but leave it where it is:
\newrobustcmd\laurentius@iftokens[3]{%
  \laurentius@set@tokens{#1}%
  \ltx@ifnextchar{ }
    {\laurentius@iftokens@aux{#2}{#3}{}}
    {\laurentius@iftokens@aux{#2}{#3}}%
}

\newrobustcmd\laurentius@iftokens@aux[3]{%
  \ifblank{#3}{#2}{%
    \ifinlist{#3}{\laurentius@tokens@list}
      {#1}
      {#2}%
    }%
  #3%
}

\newcommand*\checkmylist{\laurentius@iftokens{([*}{in my list}{not in my list}}

\makeatother

\begin{document}

\checkmylist[]

\checkmylist{}

\checkmylist()

\checkmylist*

\checkmylist/

\end{document}
Regards
site moderator & package author
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

Re: \@ifnextchar for more characters

Post by Laurentius »

Nice. That's pretty advanced. Thanks.

-- Lorenz
Post Reply