Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Possible Duplicate:
Changing catcode for commercial at (@) inside a macro to get it matched in pattern.

I want to test whether a string contains the @ sign. I came up with the following piece of code:

\makeatletter
\def\@testat#1@#2\@nil{%
  \def\@cdr{#2}
  \ifx\@cdr\@empty #1#2: does not have at\else #1#2: has at\fi}
\def\hasat#1{\@testat#1@\@nil}
\makeatother

Now this works fine if the document is written with \makeatletter:

\makeatletter
\newcommand\test[1]{#1}
\test{\hasat{linear@ set@}}
\hasat{linear@ set@}
\hasat{linear set}

all work fine. But without the \makeatletter, this fails miserably. If instead I define:

\def\@hasat#1{\@testat#1@\@nil\makeatother}
\def\hasat{\makeatletter\@hasat}

then only the \hasat which is not in a \test works for some reason.

Any help welcome!

share|improve this question

marked as duplicate by Harish Kumar, egreg, Stefan Kottwitz Nov 4 '12 at 23:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Dammit. Stackexchange now pops up the relevant related question: tex.stackexchange.com/questions/17393/… . Feel free to close this question as a duplicate. –  Michaël Nov 4 '12 at 23:03
    
Don't define \@cdr as part of your macro; it's already a macro used in the kernel and redefining it can lead to obscure errors. –  egreg Nov 4 '12 at 23:09
    
Strange, I would not consider this as a duplicate of the other one. If we really think they're "identical", I would say that this one is better and provides a better answer. –  yo' Nov 5 '12 at 9:59

1 Answer 1

up vote 5 down vote accepted

You can detect if the given string contains a character with the xstring package:

Code:

\documentclass{article}
\usepackage{xstring}

\newcommand*{\HasAt}[3]{%
    \StrPosition{#1}{@}[\Position]
    #1 \IfEq{\Position}{0}{#3}{#2}%
}

\begin{document}

\HasAt{ancd@foo.bar}{does}{does not} contains the @ symbol.

\HasAt{ancdfoo.bar}{does}{does not} contains the @ symbol.

\end{document}
share|improve this answer

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