# \expandafter usage and plain TeX programming

Hello everybody out there using TeX,

Actually, I wanted to write a LaTeX macro in plain TeX in order to include source code examples with individual modifications like stripping off comments and the first 21 characters in LaTeX documents. My code so far looks as follows:

\documentclass{article}
\edef\hashmark{\string#}
\def\getfirst#1#2\getfirst{\string#1}
\def\gobble#1#2{\expandafter#2}
\begin{document}
\newcount\linecount
\global\linecount1
\loop
\if\hashmark\expandafter\getfirst\detokenize\expandafter{\myinput}\getfirst
\relax
\else
\newcount\position
\global\position1
{
\loop
\unless\ifnum\position>21
\edef\myinput{\expandafter\gobble\myinput}
\repeat
}
\fi
\repeat
\end{document}


This results in the error message ! Paragraph ended before \gobble was complete.

Unfortunately, this is already for the second time I have to ask for help here, because I'm lacking experience in developing TeX and interpreting the error messages.

All these difficulties in manipulating text input using TeX make me wonder, whether it makes really sense to use TeX to manipulate and trim my source code files instead of say sed, Perl, Python or any other programming language in which I have more experience and allowing for easy text manipulation.

Any contribution to this consideration is appreciated.

-
Note that 'plain TeX' means using the plain TeX format, not LaTeX. Here, you are talking about TeX programming, which is a subtly-different thing. Also, for displaying code and so on I'd normally use the listings package, which offers various refinement for dealing with stripping out material, special lines and the like. –  Joseph Wright Dec 25 '12 at 21:42
You should add a typical file you want to read in this way and state precisely what you mean by "21 characters". –  egreg Dec 25 '12 at 21:46
My experience with "hey, why not writing a brief TeX macro which does the job" typically involve a high number of unanticipated problems (depending on the previous level of experience, of course). And fiddling around with verbatim code and its changed catcodes was the worst. You should allocate enough time and do it only if you like it. There might be far more efficient options in terms of time investment versus outcome. –  Christian Feuersänger Dec 25 '12 at 21:58
If you feel more comfortable with another language I would use that to preprocess the source code and then use TeX for formatting the result. –  Stephan Lehmke Dec 26 '12 at 5:47
CTAN's AroBend ("Around the bend") package contains interesting plain TeX exercises which might help unexperienced users like me to gain a bit more proficiency. –  Penguin Nurse Dec 30 '12 at 12:28

The origin of the problem were empty lines as well as the last line in the input file.

So, yes, I should have added a typical input file as a pointer towards this problem. Sorry for having missed that.

Checking for lines beginning with \par seems to fix that as far as I can see:

\documentclass{article}
\def\apar{\par}
\edef\hashmark{\string#}
\def\getfirst#1#2\getfirst{\string#1}
\def\gobble#1#2{\expandafter#2}
\begin{document}
\newcount\linecount
\global\linecount1
\loop
\if\hashmark\expandafter\getfirst\detokenize\expandafter{\myinput}\getfirst
\relax
\else
\xdef\mystring{\expandafter\myinput}
\ifx\mystring\apar
\relax
\else
\newcount\position
\global\position1
{
\loop
\unless\ifnum\position>1
\xdef\mystring{\expandafter\gobble\mystring}

You have now changed the task from "read the first 21 characters" to "read the first character". I assume you'll get the same problem when a line contains less than 21 characters, because \gobble was not defined \long. –  Stephan Lehmke Dec 30 '12 at 11:50