
Solo quería formatear un .csv
archivo, así que hago una prueba (1), que se muestra a continuación:
\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
pero no funciona, TeX se queja de eso
! File ended while scanning use of \format.
<inserted text>
\par
entonces lo elimino \obeylines
, hago otra prueba (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
funciona bien. ¿Quién puede decirme dónde está mal la prueba (1)?
Respuesta1
\obeylines
en llano es
{\catcode`\^^M=\active % these lines must end with %
\gdef\obeylines{\catcode`\^^M\active \let^^M\par}%
\global\let^^M\par}
let
Es decir , activa los extremos de la línea y\par
Esto significa que debe delimitar su macro por un activo , ^^M
no por un \par
token; el \par
token no está en la secuencia que se está escaneando; solo se genera si el token de final de línea se expande.
\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
Respuesta2
Si el archivo brooks.csv
contiene
11,2,22
13,9,117
a,b,c
entonces probablemente sea mejor leerlo línea por línea. Esto requiere e-TeX, pero también podría realizarse en 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