I know this one is quite similar to this post (from me). But even with using %
all over the place and \ignorespaces
there is still a linebreak.
See my 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}
and 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
Don't get confused by the weird setup this is because I want to have externalization (debated here, kinda solved in this post) even though wrapping the tikz environment. So I need the whole lua stuff to at first collect the content and print it later via lua back to the tex code. I guess this is where the linebreak comes from, but not sure and I definitely don't know how to fix it.
This is the output of the code above:
but the boxes (two different tikz pictures) should be side by side (packing them into on tikzpicture
is not really a possibility, since I need the line breaking after the line is full, but not after each picture).
Any ideas? (I think my problem in "debugging" is, that it's difficult to get the complete tex code generated by lua (yes I can print it, but how can I tell if there are newlines before/after). I thought there was an easy way to print this, but I cannot find it anymore)
EDIT: Example for intended linebreaking
\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}
Results in