环境中出现意外的 \obeylines 行为

环境中出现意外的 \obeylines 行为

\obeylines我对将 when放在一\begingroup...\endgroup对中这一事实感到困惑在环境中,该环境中的宏参数似乎仍然受到命令的影响\obeylines——我很可能忘记了一些令人尴尬的基本内容。

\documentclass{article}

\newtoks\tabtox
\newtoks\msctox

\begingroup
    \lccode`~=`\^^M
    \lowercase{%
\endgroup
    \def\tst#1 #2 #3~{%
        \global\tabtox=\expandafter{\the\tabtox
            \textbf{#3,} #1\ #2
            \par
        }~% we want a `\par`
    }%
}%

\NewDocumentEnvironment{startit}{}{%
    \begingroup
        \obeylines
        \offinterlineskip
        \everypar={\tst}
}{%
    \the\msctox\par
    \the\tabtox
}

\NewDocumentCommand{\finishthis}{+m}{%
    \endgroup
    \msctox={#1}
}

%% |=====8><-----| %%

\parindent0pt

\begin{document}

\thispagestyle{empty}

The argument to \string\finishthis\ seems still to\\be under the influence of \string\obeylines:

\begin{startit}
1 a b
2 c d
3 e f
4 g h
\finishthis{a
b
c
f}
\end{startit}

\bigskip

This works as expected:

\tabtox={}%
\begingroup
\obeylines
\offinterlineskip
\everypar={\tst}
1 a b
2 c d
3 e f
4 g h
\endgroup
\msctox={a
b
c
f}

\the\msctox\par
\the\tabtox
\end{document}

在此处输入图片描述

答案1

您想在组结束后抓取参数,以便它获得正常的 catcode 5 行结束:

在此处输入图片描述

\documentclass{article}

\newtoks\tabtox
\newtoks\msctox

\begingroup
    \lccode`~=`\^^M
    \lowercase{%
\endgroup
    \def\tst#1 #2 #3~{%
        \global\tabtox=\expandafter{\the\tabtox
            \textbf{#3,} #1\ #2%%%%%%%%%%
            \par
        }~% we want a `\par`
    }%
}%

\NewDocumentEnvironment{startit}{}{%
    \begingroup
        \obeylines
        \offinterlineskip
        \everypar={\tst}%%%%%%%%%%
}{%
    \the\msctox\par
    \the\tabtox
}

\NewDocumentCommand{\finishthis}{}{%
    \endgroup
    \finishthisinternal
}
\def\finishthisinternal#1{%
    \msctox={#1}%%%%%%%%%%
}

%% |=====8><-----| %%

\parindent0pt

\begin{document}

\thispagestyle{empty}

This works as expected:

\begin{startit}
1 a b
2 c d
3 e f
4 g h
\finishthis{a
b
c
f}
\end{startit}

\bigskip

This works as expected:

\tabtox={}%
\begingroup
\obeylines
\offinterlineskip
\everypar={\tst}
1 a b
2 c d
3 e f
4 g h
\endgroup
\msctox={a
b
c
f}

\the\msctox\par
\the\tabtox
\end{document}

相关内容