xparse 逐字换行

xparse 逐字换行

我正在尝试使用xparse及其verbatim参数定义一个简单的命令:

\DeclareDocumentCommand\macro{v}{#1}

但看到

! LaTeX error: "xparse/verbatim-newline"
!
! Verbatim argument of \macrocode ended by end of line.
!
! See the LaTeX3 documentation for further information.
!
! For immediate help type H <return>.

当参数是多行代码(即包含换行符)时:

\macro{This
Is
A
Test}

但对于类似的东西来说没有问题

  \macro{This    Is    A    Test}

就我而言,我需要它与新线路一起工作。

更新:

\documentclass{book}
\usepackage{luatex,xparse}
\directlua{tex.enableprimitives('',tex.extraprimitives())}
\begin{document}

\DeclareDocumentCommand{\macro}{+v}{\directlua{print("\luaescapestring{#1}")}}
\DeclareDocumentCommand{\macroa}{+v}{#1}

\macro{
This
Is
A
Test
}

\macroa{
This
Is
A
Test
}

\end{document}

宏打印“ThisIsATest”而不带换行符。我需要打印确切地传递给宏的内容。macroa 给出 fi Thisfi Isfi Afi Testfi。

顺便说一句,这里是它的工作原理的 MWE,但为了方便起见,我想将行为“包装”在宏中:

\documentclass{book}   
\usepackage{luatex}    
\directlua{tex.enableprimitives('',tex.extraprimitives())}    
\begin{document}

\endlinechar`\^^J%
\catcode`\^^M=13%
\directlua{print("\luaescapestring\expandafter{\detokenize{%
This
Is
A
Test
}}")}%
\catcode`\^^M=5

\end{document}

除了一些随机字符被输出到pdf(在我的工作示例中,这种情况不会发生,因为我猜我使用了不同的字体编码)。

答案1

使用以下命令,您会在日志文件中打印出一些行,但 PDF 中不会有任何输出:

\documentclass{book}
\usepackage{luatex}
\directlua{tex.enableprimitives('',tex.extraprimitives())}

\def\myprint{%
  \begingroup
  \endlinechar`\^^J%
  \catcode`\^^M=13%
  \myprintaux}
\def\myprintaux#1#2{%
  \directlua{print("\luaescapestring\expandafter{\detokenize{#1}}")}\endgroup}

\begin{document}

\myprint
{This
is
a
test
}

\end{document}

您需要使用该形式(右括号必须独占一行)。

答案2

正如xparse文档v参数类型与 非常相似\verb,并且必须在一行内结束。您可以使用“长”指示符抓取多行+

\DeclareDocumentCommand{\macro}{+v}{#1}

你应该注意到你将得到

This^^MIs^^MA^^MTest

当你在这里逐字逐句地抓取时(IE您会得到行尾标记,而不是\par)。

相关内容