LuaLaTeX:使用 luacode 对文本进行排序时编译失败

LuaLaTeX:使用 luacode 对文本进行排序时编译失败

在本篇 MWE 中,

\RequirePackage{luatex85}
\documentclass{elsarticle}
\usepackage[tuenc]{fontspec}
\usepackage{todonotes,etoolbox}

\makeatletter
\newcommand\@todonotes@owner{default}
\define@key{todonotes}%
{owner}{\def\@todonotes@owner{#1}}

\newtoggle{ownerdefault}


\newcommand\stR[2]{% 1st reviewer comment
    \todo[%
    owner=default,
    author=1st Reviewer,noline,caption={#1 Comment},color=blue!20%
    ]%
    {#1 Comment}{\color{blue}{\bfseries#2}}}

\makeatother

\usepackage{luacode}

\begin{luacode*}
    function sort_tdo (jobname)
    local lines = {}
    -- read the lines in table 'lines'
    for line in io.lines(jobname) do
    table.insert(lines, line)
    end
    -- sort
    table.sort(lines)
    -- write all the lines
    file=io.open(jobname.."sorted", "w") 
    io.output(file) 
    for i, l in ipairs(lines) do io.write(l, "\n") 
    end
    io.close(file)
    end 
\end{luacode*}

\IfFileExists{\jobname.tdo}{\directlua{sort_tdo("\jobname.tdo")}}{}

\begin{document}

    \toggletrue{ownerdefault}
    \section*{Reviewer \#1 Comments}
    \InputIfFileExists{\jobname.tdosorted}{}
    \togglefalse{ownerdefault}

    \makeatletter \@starttoc{tdo}\makeatother % to trigger the creation of the list

    text
\end{document}

如果我抑制此部分,代码就可以编译:

\begin{luacode*}
    function sort_tdo (jobname)
    local lines = {}
    -- read the lines in table 'lines'
    for line in io.lines(jobname) do
    table.insert(lines, line)
    end
    -- sort
    table.sort(lines)
    -- write all the lines
    file=io.open(jobname.."sorted", "w") 
    io.output(file) 
    for i, l in ipairs(lines) do io.write(l, "\n") 
    end
    io.close(file)
    end 
\end{luacode*}

\IfFileExists{\jobname.tdo}{\directlua{sort_tdo("\jobname.tdo")}}{}

我收到警告:

Shell 转义功能未启用。

尽管启用了 shell-escape。

还出现了许多错误,例如:

缺失数字,视为零。\ifnum\pdf@shellescape>

缺失数字,视为零。\或

字体 \TU/lmr/m/n/7=[lmroman7-regular]:+tlig;7pt 处无法加载:未找到度量数据或数据错误。\section*{审阅者 #1 评论}

字体 \OML/cmm/m/it/10=cmmi10 at 10pt 无法加载:度量数据未找到或错误。\section*{审阅者 #1 评论}

字体 \OMS/cmsy/m/n/10=cmsy10 在 10pt 处无法加载:未找到度量数据或数据错误。\section*{审阅者 #1 评论}

字体 \OMX/cmex/m/n/5=cmex10 无法加载:未找到度量数据或数据错误。\section*{审阅者 #1 评论}

字体 \OT1/cmr/m/n/7=cmr7 在 7pt 处无法加载:未找到度量数据或度量数据错误。\section*{审阅者 #1 评论}


更新

不幸的是,此代码会引发相同的错误

\RequirePackage{luatex85,shellesc}

\documentclass{elsarticle}

\setlength\marginparwidth{0.8\dimexpr(\paperwidth - \textwidth)/2\relax}
\usepackage{etoolbox,lipsum}
\usepackage[textwidth=\marginparwidth]{todonotes}

\usepackage[tuenc]{fontspec}

\makeatletter
\def\pdf@shellescape{\directlua{tex.sprint(status.shell_escape .. " ")}}
\makeatother


\makeatletter
\newcommand\@todonotes@owner{default}
\define@key{todonotes}%
{owner}{\def\@todonotes@owner{#1}}

\newtoggle{ownerdefault}
\newtoggle{ownerB}


\newcommand\stR[2]{% 1st reviewer comment
    \todo[%
    owner=default,
    author=1st Reviewer,noline,caption={#1 Comment},color=blue!20%
    ]%
    {#1 Comment}{\color{blue}{\bfseries#2}}}

\newcommand\ndR[2]{% 2nd reviewer comment
    \todo[%
    owner=B,
    author=2nd Reviewer,noline,caption={#1 Comment},color=red!50!white%
    ]%
    {#1 Comment}{\color{red}{\bfseries#2}}}

\renewcommand{\@todonotes@addElementToListOfTodos}{%
    \if@todonotes@colorinlistoftodos%
    \addtocontents{tdo}
    {%
        \protect\iftoggle{owner\@todonotes@owner}
        {%
            \protect\contentsline {todo}
            {\protect\fcolorbox{\@todonotes@currentbordercolor}%
                {\@todonotes@currentbackgroundcolor}%
                {\protect\textcolor{\@todonotes@currentbackgroundcolor}{o}}%
                \ \@todonotes@caption
            }{\thepage}{\@currentHref}%
        }{}%
    }%
    \else%
    \addtocontents{tdo}
    {%
        \protect\iftoggle{owner\@todonotes@owner}
        {%
            \protect\contentsline {todo}
            {\@todonotes@caption
            }{\thepage}{\@currentHref}%
        }{}%
    }%
    \fi}%

\makeatother

\directlua{
function sort_tdo (jobname)
local lines = {}
%    -- read the lines in table 'lines'
for line in io.lines(jobname) do
table.insert(lines, line)
end
%    -- sort
table.sort(lines)
%    -- write all the lines
file=io.open(jobname.."sorted", "w") 
zz=io.output() 
io.output(file) 
for i, l in ipairs(lines) do io.write(l, "\string\n") 
end
io.close(file)
io.output(zz)
end 
}

\IfFileExists{\jobname.tdo}{\directlua{sort_tdo("\jobname.tdo")}}{}

\usepackage[hidelinks,colorlinks=false,bookmarks=true]{hyperref}
\usepackage[numbered]{bookmark}

\begin{document}

    \pagenumbering{gobble}% Remove page numbers (and reset to 1)
    \clearpage
    \thispagestyle{empty}

    \toggletrue{ownerdefault}
    \section*{Reviewer \#1 Comments}
    \InputIfFileExists{\jobname.tdosorted}{}

    \toggletrue{ownerB}
    \togglefalse{ownerdefault}
    \section*{Reviewer \#2 Comments}
    \InputIfFileExists{\jobname.tdosorted}{}
    \togglefalse{ownerB}

    \makeatletter \@starttoc{tdo}\makeatother
    \clearpage
    \pagenumbering{arabic}

    \stR{2nd}{comment}
    \lipsum[1-5]
    \stR{1st}{comment}
    \lipsum[1-5]
    \ndR{1st}{comment}

\end{document}

答案1

如果我制作一个更合理的最小示例,我会得到相同的错误,除非我恢复默认输出流(zz下面 lua 中的行)。感谢聊天中的 Ulrike 和 wilx)

\RequirePackage{luatex85,shellesc}

\documentclass{article}

\usepackage{todonotes}

\makeatletter

\def\pdf@shellescape{\directlua{tex.sprint(status.shell_escape .. " ")}}

\makeatother


\directlua{
    function sort_tdo (jobname)
    local lines = {}
%    -- read the lines in table 'lines'
    for line in io.lines(jobname) do
    table.insert(lines, line)
    end
%    -- sort
    table.sort(lines)
%    -- write all the lines
    file=io.open(jobname.."sorted", "w") 
    zz=io.output() 
    io.output(file) 
    for i, l in ipairs(lines) do io.write(l, "\string\n") 
    end
    io.close(file)
    io.output(zz)
    end 
}

\IfFileExists{\jobname.tdo}{\directlua{sort_tdo("\jobname.tdo")}}{}
\begin{document}

    \makeatletter \@starttoc{tdo}\makeatother % to trigger the creation of the list

    text
\end{document}

相关内容