4

I'd like to test for a character, irrespective its current catcode (i.e. active or not). This is very similar to How do I test for a character, irrespective of catcode?, however I cannot use \pdf@strcmp because I might have "half" macros (e.g. \emph without a parameter yet).

\documentclass{minimal}
\usepackage[czech]{babel}% makes - active
\begin{document}
  \def\aaa{%
    \if a\noexpand\lookahead%
      (the letter a)%
    \else\if -\noexpand\lookahead%
      (a minus)%
    \else%
      (special treatment)
    \fi\fi%
  }
  \futurelet\lookahead\aaa -
  \futurelet\lookahead\aaa a
  \futurelet\lookahead\aaa \emph{c}
\end{document}

Changing catcodes before and after is not really a solution in my case, because the original meaning would get lost just by futurelet-looking at things.

  • Welcome to TeX.SX! Have a look here to know how to start. – karlkoeller Jun 1 '13 at 7:35
3

\ifx can be used for direct comparisons of the tokens. The following example stores the tokens in macros \aaaHyphenActive and \aaaHyphenOther. In the comparisons the hyphen tokens are unpacked via \expandafter.

\documentclass{minimal}
\usepackage[czech]{babel}% makes - active

\begingroup
  \catcode`\-=\active
  \gdef\aaaHyphenActive{-}%
  \catcode`\-=12 %
  \gdef\aaaHyphenOther{-}%
\endgroup

\begin{document}
  \def\aaa{%
    \if a\noexpand\lookahead
      (the letter a)%
    \else\expandafter\ifx\aaaHyphenActive\lookahead
      (an active minus)%
    \else\expandafter\ifx\aaaHyphenOther\lookahead
      (a minus)%
    \else
      (special treatment)
    \fi\fi\fi
  }
  \futurelet\lookahead\aaa -
  \futurelet\lookahead\aaa a
  \futurelet\lookahead\aaa \emph{c}
\end{document}

Result

1

You can use expl3. Here's a rough example.

\documentclass{article}
\usepackage[czech]{babel}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\dosomething}{m}
 {
  \martin_do_something:n {#1}
 }
\cs_new_protected:Npn \martin_do_something:n #1
 {
  \peek_charcode:NTF a % test the first token against "a"
   {
    (the~letter~a)
   }
   {
    \peek_charcode:NTF - % test the first token against "-" (charcode only)
     {
      (a~hyphen)
     }
     {
      (special~treatment)
     }
   }
  #1
}
\ExplSyntaxOff

\begin{document}

\dosomething{abc}

\dosomething{-abc}

\dosomething{\emph{abc}}

\the\catcode`- % check that the hyphen was active

\catcode`-=12  % make it non active and try again

\dosomething{-abc}

\end{document}

enter image description here

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.