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 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?

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

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}
share|improve this answer
    
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
add comment

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.