![如何在 LuaLaTeX 中自动在末尾打印逐字块?](https://linux22.com/image/352527/%E5%A6%82%E4%BD%95%E5%9C%A8%20LuaLaTeX%20%E4%B8%AD%E8%87%AA%E5%8A%A8%E5%9C%A8%E6%9C%AB%E5%B0%BE%E6%89%93%E5%8D%B0%E9%80%90%E5%AD%97%E5%9D%97%EF%BC%9F.png)
这是Mico 的这个回答。我尝试根据自己的需要调整他的 Lua 代码,并且行号已经起作用了。
实际上有一个真正的可用性问题,这就是我的问题:如何分别打印缓冲区\tdendlongoutput
或从回调中删除?目前我需要一个额外的命令来实现这一点。
以下是我迄今为止尝试过的:
- 在回调删除的正下方插入指令,
- 用于
tex.print
打印使用运行该函数的 LaTeX 宏。
梅威瑟:
%!TeX TS-program=lualatex+shellescape
\documentclass{scrreprt}
\directlua{%
verb_table = {}
i = 1
function store_lines (str)
if string.find (str , "\noexpand\\tdendlongoutput" ) then
luatexbase.remove_from_callback ("process_input_buffer" , "store_lines")
else
table.insert(verb_table, tostring(i) .. " " .. str)
i = i + 1
end
return ""
end
function register_verbatim ()
verb_table = {}
i = 1
luatexbase.add_to_callback("process_input_buffer" , store_lines , "store_lines")
end
function print_lines ( catcode )
if catcode then
tex.print ( catcode , verb_table)
else
tex.print ( verb_table )
end
end
}
\def\tdlongoutput{\directlua{register_verbatim()}}
\def\tduseoutputastex{\directlua{print_lines()}}
\def\createcatcodes{%
\bgroup%
\catcode`\\=12 \catcode`\{=12 \catcode`\}=12%
\catcode`\$=12 \catcode`\&=12 \catcode`\^^M=13%
\catcode`\#=12 \catcode`\^=12 \catcode`\_=12%
\catcode`\ =13 \catcode`\~=12 \catcode`\%=12%
\savecatcodetable 1%
\egroup%
}
\createcatcodes
\bgroup%
\catcode`\^^M=13\gdef^^M{\quitvmode\par}%
\catcode`\ = 13\gdef {\quitvmode\Space}%
\egroup%
\def\Space{ }
\begin{document}\ttfamily
\tdlongoutput
total 10244
drwxr-xr-x 47 User Users 16384 Mar 31 18:26 .
dr-xr-xr-x 8 User Users 4096 Jun 27 2015 ..
drwxr-xr-x 9 User Users 4096 Dec 23 12:49 .editor
-rw-r--r-- 1 User Users 2950 Mar 31 09:01 .bash_history
\tdendlongoutput
\directlua{print_lines(1)}
\end{document}
答案1
(我修改了我的原始答案以恢复 OP 的宏的功能\tduseoutputastex
。)
您可以实现自动打印逐字块内容的目标(该块被理解为分别夹在\tdlongoutput
和行之间\tdendlongoutput
),如下所示:在 Lua 函数中store_lines
,到达一行时\tdendlongoutput
,返回字符串\tdlongoutputprint
而不仅仅是空白行。宏\tdlongoutputprint
被定义为打印出逐字缓冲区的内容。
请注意,我还对您的 Lua 代码进行了如下更改:逐字材料的行号不再与逐字材料本身一起存储。而是在打印时(即在 操作期间)动态插入\tdlongoutputprint
。
如果你只想存储逐字块没有\tdendlongoutputnoprint
自动打印出其内容,您应该用而不是 来终止逐字块\tdendlongoutput
。
%!TeX TS-program=lualatex
\documentclass{scrreprt}
%% Lua-side code
\usepackage{luacode}
\begin{luacode}
verb_table = {}
i = 1
function store_lines (str)
if str:find ( "\\tdendlongoutputnoprint" ) then
luatexbase.remove_from_callback ("process_input_buffer" , "store_lines")
return ""
elseif str:find ( "\\tdendlongoutput" ) then
luatexbase.remove_from_callback ("process_input_buffer" , "store_lines")
return ( "\\tdlongoutputprint" )
else
table.insert ( verb_table , str )
return ""
end
end
function register_verbatim ()
verb_table = {}
i = 1
luatexbase.add_to_callback( "process_input_buffer" , store_lines , "store_lines")
end
function print_lines ( catcode )
if catcode then
for i = 1 , #verb_table do
tex.print ( catcode , i.." "..verb_table[i])
end
else
tex.print ( verb_table )
end
end
\end{luacode}
%% TeX-side code
\def\tdlongoutput{\directlua{register_verbatim()}}
\newcommand\tdlongoutputprint{%
\bgroup
\setlength\parindent{0pt}
\ttfamily
\directlua{print_lines(1)}
\egroup}
\def\tduseoutputastex{\directlua{print_lines()}}
\def\createcatcodes{%
\bgroup%
\catcode`\\=12 \catcode`\{=12 \catcode`\}=12%
\catcode`\$=12 \catcode`\&=12 \catcode`\^^M=13%
\catcode`\#=12 \catcode`\^=12 \catcode`\_=12%
\catcode`\ =13 \catcode`\~=12 \catcode`\%=12%
\savecatcodetable 1%
\egroup%
}
\createcatcodes
\bgroup%
\catcode`\^^M=13\gdef^^M{\quitvmode\par}%
\catcode`\ = 13\gdef {\quitvmode\Space}%
\egroup%
\def\Space{ }
\begin{document}
\tdlongoutput % start of verbatim block
total 10244
drwxr-xr-x 47 User Users 16384 Mar 31 18:26 .
dr-xr-xr-x 8 User Users 4096 Jun 27 2015 ..
drwxr-xr-x 9 User Users 4096 Dec 23 12:49 .editor
-rw-r--r-- 1 User Users 2950 Mar 31 09:01 .bash_history
\tdendlongoutput %% print contents of verbatim block
\tdlongoutput % define a macro called "\lululu"
\def\lululu{%
Lua\kern-.01em
\LaTeX%
}
\tdendlongoutputnoprint %% no AUTOMATIC printing of verb. block
\bigskip
\tdlongoutputprint %% print contents of latest verb. block "by hand"
\bigskip
\tduseoutputastex % "register" the macro "\lululu"
\noindent\lululu
\end{document}