Plain TeX 巨集包含 \par 作為參數分隔符,但不能與 \obeylines 一起使用,為什麼?

Plain TeX 巨集包含 \par 作為參數分隔符,但不能與 \obeylines 一起使用,為什麼?

我只是想格式化一個.csv文件,所以我做了一個測試(1),如下所示:

\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

但這不起作用,TeX 抱怨說

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

所以我刪除\obeylines,做另一個測試(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

效果很好。誰能告訴我測試(1)哪裡錯了?

答案1

\obeylines簡單來說就是

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

也就是說,它使行尾處於活動狀態let\par

這表示您需要透過活動^^M而不是\par標記來分隔宏,該\par標記不在正在掃描的流中,它僅在行尾標記擴充時產生。

\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

答案2

如果文件brooks.csv包含

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

然後逐行閱讀可能會更好。這需要 e-TeX,但也可以在 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

在此輸入影像描述

相關內容