6

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}
\newread\myread
\begin{document}
\openin\myread=complete.txt
  \newcount\linecount
  \global\linecount1
  \loop
    \unless\ifeof\myread
    \read\myread to \myinput
    \if\hashmark\expandafter\getfirst\detokenize\expandafter{\myinput}\getfirst
      \relax
    \else
      \newcount\position
      \global\position1
      {
        \loop
          \unless\ifnum\position>21
            \edef\myinput{\expandafter\gobble\myinput}
            \global\advance\position1
        \repeat
      }  
    \fi
    \global\advance\linecount1
  \repeat
\closein\myread
\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.

5
  • 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, 2012 at 21:42
  • 3
    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, 2012 at 21:46
  • 4
    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. Dec 25, 2012 at 21:58
  • 1
    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. Dec 26, 2012 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. Dec 30, 2012 at 12:28

1 Answer 1

2

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}
\newread\myread
\begin{document}
\openin\myread=complete.txt
  \newcount\linecount
  \global\linecount1
  \loop
    \unless\ifeof\myread
    \read\myread to \myinput
    \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}
                  \global\advance\position1
              \repeat
            }  
          \fi
        \mystring
      \fi
    \global\advance\linecount1
  \repeat
\closein\myread
\end{document}
2
  • 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. Dec 30, 2012 at 11:50
  • Yes, that's true. Sorry for that. One has to know the length of the input in advance for this solution to work. Jan 6, 2013 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .