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 just wanted to format a .csv file, so I do a test (1), shown as below:

\global\let\xpar=\par
\def\format#1,#2,#3\par{$#1\times #2=#3$\xpar}
\begingroup
\everypar={\format}\obeylines%
11,2,22
13,9,117
a,b,c
\endgroup
\end

but it doesn't work, TeX complain that

! File ended while scanning use of \format.
<inserted text> 
            \par 

so I remove \obeylines, do another test (2):

\global\let\xpar=\par
\def\format#1,#2,#3\par{$#1\times #2=#3$\xpar}
\begingroup
\format 11,2,22

\format 13,9,117

\format a,b,c

\endgroup
\end

it works fine. Who can tell me where is wrong is test (1)?

share|improve this question
add comment

2 Answers 2

\obeylines in plain is

{\catcode`\^^M=\active % these lines must end with %
  \gdef\obeylines{\catcode`\^^M\active \let^^M\par}%
  \global\let^^M\par}

That is it makes the ends of line active and let to \par

This means you need to delimit your macro by an active ^^M not a \par token, the \par token isn't in the stream being scanned, it is only generated if the end of line token expands.

\begingroup
\everypar={\format}\obeylines%
\def\format#1,#2,#3^^M{$#1\times #2=#3$\par}%
11,2,22
13,9,117
a,b,c
\endgroup
\end
share|improve this answer
    
it's really works,thanks –  brooks_luo Apr 19 '13 at 4:25
add comment

If the file is brooks.csv containing

11,2,22
13,9,117
a,b,c

then reading it line by line is probably better. This requires e-TeX, but could be realized also in Knuth TeX.

\def\format#1,#2,#3\format{$#1\times#2=#3$\par}
\newread\brooksread

\hsize=.5\hsize % just for the example

\noindent X\hrulefill X\par

\openin\brooksread=brooks.csv
\begingroup\endlinechar=-1
\loop\unless\ifeof\brooksread
  \read\brooksread to \test \show\test
  \unless\ifx\test\empty
    \expandafter\format\test\format
  \fi
\repeat
\endgroup

\noindent X\hrulefill X\par

\bye

enter image description here

share|improve this answer
    
Thanks. I will use this snippets in my script. –  brooks_luo Apr 19 '13 at 4:56
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.