0

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:

enter image description here

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

enter image description here

1 Answer 1

2

Your callback is emptying each line but leaving the line ends so essentially any input of more than one line looks like a blank line paragraph end.

One way would be to change every line to just have %

enter image description here

where the only change is adding % in the callback return

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

You can add a breakpoint (penalty as here or space) afer the \end{tikzpicture}

enter image description here

\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}

with 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
7
  • Oh that was fast ^^, thanks. Problem I have now is that now there are no linebreaks at all (even if the line is already full). I thought if I had pic pic ... (with no "manual" linebreaks in between) latex would do the linebreaking automatically after the line is full, am I wrong with this or am I doing something else wrong?
    – atticus
    Mar 11, 2022 at 23:01
  • @atticus sorry I don't understand your comment this is just commenting out the lines at the point you store them, you sill have a table with each line so when you process that table you can add line ends back if you wish, that part I did not change. You are putting all the text collected into a single tikz node so hat's an mbox and will not linebreak, if you supply a width then the nod will be a minipage ad linebreak to the specified width Mar 11, 2022 at 23:08
  • Hm as I see it, after inserting the tikzpictures, I should have \begin{tikzpicture} ... \end{tikzpicture} \begin{tikzpicture} ... \end{tikzpicture}. If I'm inserting this manually into the document, there are linebreaks inserted between the pictures if the line is full. Maybe this is best illustrated with a second example + image (see the original post)
    – atticus
    Mar 11, 2022 at 23:14
  • 1
    @atticus updated answer Mar 11, 2022 at 23:44
  • 1
    @atticus no youare removing the line ends so without the lua it's like \end{ikzpicture}%<newline}\begin{tikzpicture} and you would get no linebreak Mar 12, 2022 at 0:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .