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.

I want to implement an if-then-else depending on whether the argument contains a special character or not. Should I be looking here: http://www.tug.org/TUGboat/Articles/tb28-1/tb88glister.pdf, or does anyone know a simpler way?

share|improve this question

2 Answers 2

up vote 11 down vote accepted
\documentclass{article}
    
\usepackage{xstring}
    
\begin{document}
    
    

\IfSubStr{Rotterdam}{otter}{ true }{ false }
    
    

\end{document}
share|improve this answer
\makeatletter
\def\instring#1#2{TT\fi\begingroup
  \edef\x{\endgroup\noexpand\in@{#1}{#2}}\x\ifin@}
\makeatother

\def\mystring{abcdef}

\if\instring{a}{abcdef}\message{YES}\else\message{NO}\fi
\if\instring{a}{\mystring}\message{YES}\else\message{NO}\fi

LaTeX should show "YES" in both cases. However, it depends on the kind of "special character" you are interested in.

share|improve this answer
    
The specific character was "=". This works great, thanks! I was actually just around the corner from a similar solution, all thanks to tex.ac.uk/tex-archive/info/macros2e/macros2e.pdf, but the guide isn't so clear around what an "\ifin@ switch" is, do you happen to have a good guide to doing "advanced" stuff like this? –  oleks Aug 29 '11 at 15:30
    
Nice. I didn't know about \in@ and \ifin@ ... –  cgnieder May 25 '13 at 11:05
    
@cgnieder They're well hidden in the LaTeX kernel. ;-) Of course expl3 makes better tools available. –  egreg May 25 '13 at 11:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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