如果宏扩展为空,则宏避免换行

如果宏扩展为空,则宏避免换行

我知道这个和帖子(来自我)。但即使%在所有地方使用,\ignorespaces仍然有一个换行符。

查看我的 mwe: ( main2.tex)

\documentclass{article}

\usepackage{tikz}

\newcommand{\myCmd}[1]{\node[draw] {#1};\ignorespaces}

\directlua{m = require "main2.lua"}
\def\step{\directlua{m.register_verbatim()}\ignorespaces}
    \def\Endstep{\directlua{m.print_lines(1)}\ignorespaces}

\begin{document}
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\end{document}

main2.lua

local env_pre =
[[\begin{tikzpicture}[font=\scriptsize]%
\begin{scope}[local bounding box=output]%]]

local env_post =
[[\end{scope}%
\node[] (t1) {};%
\end{tikzpicture}%]]

local verb_table = {}
local function store_lines (str)
  if string.find (str , "\\noexpand\\Endstep" ) then
    luatexbase.remove_from_callback(
      "process_input_buffer" , "store_lines")
  else
    table.insert(verb_table, str)
  end
  return ""
end
local function register_verbatim()
  verb_table = {}
  luatexbase.add_to_callback(
    "process_input_buffer" , store_lines , "store_lines")
end

local function tprint(x)
    for e in x:gmatch("[^\n]*") do
        tex.sprint(e)
        print(e)
    end
end

local function print_lines()
    tprint(env_pre)
    tex.sprint(verb_table)
    for _,e in ipairs(verb_table) do
        print(e)
    end
    tprint(env_post)
end

M = {
    store_lines  = store_lines ,
    register_verbatim  = register_verbatim ,
    print_lines  = print_lines ,
}
return M

不要被奇怪的设置弄糊涂了,这是因为我想进行外部化(有争议这里,有点解决了在这篇文章中),即使包装了 tikz 环境。所以我需要整个 lua 东西来首先收集内容,然后通过 lua 将其打印回 tex 代码。我猜这就是换行符的来源,但不确定,我绝对不知道如何修复它。

这是上面代码的输出:

在此处输入图片描述

但是这些盒子(两个不同的 tikz 图片)应该并排放置(将它们打包在一起tikzpicture实际上是不可能的,因为我需要在行满之后换行,但不是在每张图片之后)。

有什么想法吗?(我认为我在“调试”中遇到的问题是,很难获得由 lua 生成的完整 tex 代码(是的,我可以打印它,但我怎么知道之前/之后是否有换行符)。我以为有一种简单的方法可以打印它,但我再也找不到它了)

编辑:预期换行的示例

\documentclass{scrartcl}

\usepackage{tikz}

\newcommand{\myCmd}[1]{\node[draw] {#1};\ignorespaces}

\directlua{m = require "../main2.lua"}
\def\step{\directlua{m.register_verbatim()}\ignorespaces}
    \def\Endstep{\directlua{m.print_lines(1)}\ignorespaces}

\begin{document}
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
x
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%

\begin{tikzpicture}
    \node[draw] {lore ipsum longer line};
\end{tikzpicture}
\begin{tikzpicture}
    \node[draw] {lore ipsum longer line};
\end{tikzpicture}
\begin{tikzpicture}
    \node[draw] {lore ipsum longer line};
\end{tikzpicture}
\begin{tikzpicture}
    \node[draw] {lore ipsum longer line};
\end{tikzpicture}
\begin{tikzpicture}
    \node[draw] {lore ipsum longer line};
\end{tikzpicture}
\begin{tikzpicture}
    \node[draw] {lore ipsum longer line};
\end{tikzpicture}
\begin{tikzpicture}
    \node[draw] {lore ipsum longer line};
\end{tikzpicture}
\begin{tikzpicture}
    \node[draw] {lore ipsum longer line};
\end{tikzpicture}
\end{document}

结果是

在此处输入图片描述

答案1

您的回调正在清空每一行但留下行尾,因此本质上任何超过一行的输入看起来都像一个空白行段落结尾。

一种方法是将每一行改为%

在此处输入图片描述

唯一的变化是添加了%回调返回

local function store_lines (str)
  if string.find (str , "\\noexpand\\Endstep" ) then
    luatexbase.remove_from_callback(
      "process_input_buffer" , "store_lines")
  else
    table.insert(verb_table, str)
  end
  return "%"
end

您可以在\end{tikzpicture}

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}

\newcommand{\myCmd}[1]{\node[draw] {#1};}

\directlua{m = require "main2.lua"}
\def\step{\directlua{m.register_verbatim()}}
    \def\Endstep{\directlua{m.print_lines(1)}}

\begin{document}
\raggedright
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\step%
  \myCmd{lore ipsum}%
\noexpand\Endstep%
\Endstep%
\end{document}

使用 Lua

local env_pre =
[[\begin{tikzpicture}[font=\scriptsize]%
\begin{scope}[local bounding box=output]%]]

local env_post =
[[\end{scope}%
\node[] (t1) {};%
\end{tikzpicture}\linebreak[0]{}]]

local verb_table = {}
local function store_lines (str)
  if string.find (str , "\\noexpand\\Endstep" ) then
    luatexbase.remove_from_callback(
      "process_input_buffer" , "store_lines")
  else
    table.insert(verb_table, str)
  end
  return "%"
end
local function register_verbatim()
  verb_table = {}
  luatexbase.add_to_callback(
    "process_input_buffer" , store_lines , "store_lines")
end

local function tprint(x)
    for e in x:gmatch("[^\n]*") do
        tex.sprint(e)
        print(e)
    end
end

local function print_lines()
    tprint(env_pre)
    tex.sprint(verb_table)
    for _,e in ipairs(verb_table) do
        print(e)
    end
    tprint(env_post)
end

M = {
    store_lines  = store_lines ,
    register_verbatim  = register_verbatim ,
    print_lines  = print_lines ,
}
return M

相关内容