views:

336

answers:

1

I have the following problem. I have defined a macro, \func as follows

\newcommand{\func}[1]{% do something with #1  
X #1 Y
}

I now want to define another macro

\newcommand{\MyFunc}[1]{  
% parse #1 and if it contains "\func{....}", ignore all except this part
% otherwise ignore #1 
}

Can someone tell me how to implement \MyFunc

here is what should happen:

\MyFunc{123abcdefg}              % should print nothing
\MyFunc{123\func{abcd}efg}       % should print X abcd Y

I can only change the code of \MyFunc. \func should remain as it is.

+5  A: 

This can be done with standard LaTeX programming. Something like:

\documentclass{article}
\newcommand\func[1]{X #1 Y}
\makeatletter
\newcommand\MyFunc[1]{%
  \in@{\func}{#1}%
  \ifin@
    \ignore@all@but@func#1\@nil
  \fi
}
\def\ignore@all@but@func#1\func#2#3\@nil{\func{#2}}
\makeatother
\begin{document}
[\MyFunc{123abcdefg}]              % should print nothing
[\MyFunc{123\func{abcd}efg}]       % should print X abcd Y
\end{document}

Will Robertson
Charles Stewart
thanks!! thats exactly what I was looking for :)Is there any good resource for learning (La)TeX programming?
Jus12
I should mention that I had to change `\newcommand` in your code to `\DeclareRobustCommand`, and then everything worked flawlessly!(this is related to the actual document of course, not the toy example above)
Jus12
@amit: no, there's not really any good place to learn all this stuff. I recommend just reading package sources and the LaTeX2e source (`texdoc source2e`), and asking lots of questions :)
Will Robertson
Something missing: the code needs a `\makeatother` after the newcommand is finished. It works as is for this example, but will have strange effects for typical latex.
Charles Stewart
Fwiw, i'm not aware of any troubles with a missing \makeatother, but you're right. It's a bad habit of mine to omit it.
Will Robertson
To be honest, the most serious trouble I can think of, excluding contrived examples, is bad hyphenation of email addresses...
Charles Stewart
yes of course, it was assumed that `makeatother` should be there :)
Jus12