# Make the pattern of a \def depend on another macro

I would like to set up a macro defined something like this:

\def\processline string: #1.#2\relax{\gdef\errorline{#1}}


The catch is that the string should be given by the contents of another macro. I tried to do this using \expandafters, and it resulted in an error. (Note that the code below is saved in a file named errortest.tex.)

\documentclass{article}
\edef\filename{\jobname-funny_name}
\def\processline errortest-funny_name.asy: #1.#2\relax{\gdef\errorline{#1}}
% The following line does not work:
%\expandafter\def\expandafter\processline\filename.asy: #1.#2\relax{\gdef\errorline{#1}}
\edef\oneline{errortest-funny_name.asy: 5.6 syntax error}
{\expandafter\processline\oneline\relax}

\begin{document}
stuff

\errorline
\end{document}


Why does this fail? How can it be made to work?

-

It's a category code problem, \jobname does not produce normal category codes when expanded. You can get around it by using \scantokens:

\documentclass{article}

\edef\filename{\scantokens\expandafter{\jobname\noexpand}-funny_name}
\expandafter\def\expandafter\processline\filename.asy: #1.#2\relax{\gdef\errorline{#1}}
\edef\oneline{errortest-funny_name.asy: 5.6 syntax error}
{\expandafter\processline\oneline\relax}

\begin{document}
stuff

\errorline
\end{document}


Alternatively, you can also use \filename directly in the definition of \oneline (this will have the advantage of allowing more exotic characters in the processed filenames):

\documentclass{article}

\edef\filename{\jobname-funny_name}
\expandafter\def\expandafter\processline\filename.asy: #1.#2\relax{\gdef\errorline{#1}}
\expandafter\edef\expandafter\oneline\expandafter{\filename.asy: 5.6 syntax error}
{\expandafter\processline\oneline\relax}

\begin{document}
stuff

\errorline
\end{document}

-
Thanks! Your second suggestion won't work for me because \oneline is actually a line read from an error log file, but the first suggestion is exactly what I need. –  Charles Staats Feb 24 at 16:18