在 LuaTeX 格式信函实现中包含注释环境

在 LuaTeX 格式信函实现中包含注释环境

我的操作系统是 Debian GNU/Linux 10.2 (buster)。TeX Live 是从不稳定版本2019.20190605.51237-3(TeX Live 2019 的快照) 反向移植而来的。

下面的代码是 LuaTeX 格式信函实现。如果没有comment环境行,编译时不会出现问题。如果使用注释环境,则会出现以下错误。我想将注释环境与格式信函实现结合使用。我该如何修复此问题?

LaTeX Warning: Writing or overwriting file `./formletter.lua'.

(/usr/share/texlive/texmf-dist/tex/latex/comment/comment.sty
Excluding comment 'comment')

LaTeX Warning: Writing or overwriting file `./formletter_addresses.yaml'.

(./formletter.aux)
Foldmarks: yes
Head of first page
Foot of first page
Address (backaddress, addressee)
Location field: empty
Title: no
Subject: no
Excluding 'comment' comment.)
Runaway argument?
! File ended while scanning use of \next.
<inserted text> 
\par 
<*> formletter.tex

? 

代码如下。

"\luaescapestring{\unexpanded{#1}}"部分不是我的主意。我从某个地方复制了它,尽管现在我不确定是从哪里复制的。

\documentclass[12pt, twoside=semi]{scrlttr2}
\usepackage{xparse}
\usepackage{quoting}
\usepackage{comment}
\usepackage{luapackageloader}
\begin{filecontents}[overwrite,noheader]{formletter.lua}
package.cpath="/usr/local/lib/lua/5.3/?.so;/usr/lib/x86_64-linux-gnu/lua/5.3/?.so;/usr/lib/lua/5.3/?.so;/usr/local/lib/lua/5.3/loadall.so;./?.so".\
.package.cpath
package.path="/usr/local/share/lua/5.3/?.lua;/usr/local/share/lua/5.3/?/init.lua;/usr/local/lib/lua/5.3/?.lua;/usr/local/lib/lua/5.3/?/init.lua;/u\
sr/share/lua/5.3/?.lua;/usr/share/lua/5.3/?/init.lua;./?.lua"..package.path
local posix = require "posix"

local function loadyaml(config)
   local lyaml = require "lyaml"
   local s
   if posix.stat(config) ~= nil then
      local f = io.open(config)
      s = f:read("*all")
      f:close()
      return lyaml.load (s)
   end
end

local function formletter(config, text)
   t = loadyaml(config)
   for k,v in pairs(t) do
      -- texio.write_nl(v["Name"])
      -- texio.write_nl(v["MultiLineAddress"])
      tex.sprint(string.format([[\providecommand{\name}{%s}]], v["Name"]))                                                                         
      tex.sprint(string.format([[\renewcommand{\name}{%s}]], v["Name"]))                                                                           
      tex.sprint(string.format([[\providecommand{\multilineaddress}{%s}]], v["MultiLineAddress"]))                                                 
      tex.sprint(string.format([[\renewcommand{\multilineaddress}{%s}]], v["MultiLineAddress"]))                                                   
      tex.sprint(text)
   end
end

return {formletter=formletter}
\end{filecontents}
\directlua{formletter = require "formletter.lua"}
\NewDocumentCommand{\formletter}{m +m}
{
  \directlua{formletter.formletter("\luaescapestring{#1}","\luaescapestring{\unexpanded{#2}}")}
}
\begin{filecontents}[overwrite,noheader]{formletter_addresses.yaml}
- Name: Name1
  MultiLineAddress: Address1
- Name: Name2
  MultiLineAddress: Address2
\end{filecontents}

\begin{document}

\formletter{formletter_addresses.yaml}
{% Begin formletter (\formletter second argument)                                                                                                  
  \begin{letter}{\name\\\multilineaddress}

    \opening{Dear \name}
    Hello
    \begin{quoting}
      Something quoted.
    \end{quoting}
    \closing{Yours Sincerely,}
    \begin{comment}
      A comment
    \end{comment}
  \end{letter}

}% End formletter                                                                                                                                  

\end{document}

相关内容