일반 TeX 매크로에는 매개변수 구분 기호로 \par가 포함되어 있지만 \obeylines에서는 작동하지 않습니다. 이유는 무엇입니까?

일반 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

여기에 이미지 설명을 입력하세요

관련 정보