由于“非可视”代码行导致文本和 tikzpicture 之间出现不必要的间距

由于“非可视”代码行导致文本和 tikzpicture 之间出现不必要的间距

我想显示某个图表的一些子图。我希望​​它们并排显示并贴上标签。这是我得到的: 在此处输入图片描述

我无法提供可用的 LaTeX 代码,因为我已经在 Lua 中实现了图形绘制功能,但它看起来像这样:

\def\coords{(0,0), (1,0), (1, 1), (0,1)}
\def\edges{1/2, 2/3, 3/4, 4/1, 2/4}
\def \scale {0.8}
\def \pos {center}
\begin{center}
     \scalebox{\scale}{(1)\xgraph{\coords}{\edges}{2/3, 3/4}{\pos}} 
     \scalebox{\scale}{(2)\xgraph{\coords}{\edges}{1/2, 4/1}{\pos}}
     \scalebox{\scale}{(3)\xgraph{\coords}{\edges}{}{\pos}}
\end{center}

xgraph 宏是:

\directlua{gd = require("lua/graphdrawing")}
\newcommand{\xgraph}[5][]{
    \begingroup
    \edef\vertexcoords{#2}
    \edef\edges{#3}
    \edef\blankedges{#4}
    \def\positioning{#5}
    %
    \directlua{
        v, e = gd.get_graph_objects("\vertexcoords", "\edges", "\blankedges")
        vertex_data = gd.tikzify(v)
        edge_data = gd.tikzify(e)}
    \edef\vertexdata{\directlua{tex.print(vertex_data)}}
    \edef\edgedata{\directlua{tex.print(edge_data)}}
    %
    \begin{tikzpicture}[scale = 1.5, nodes={circle,draw}, baseline=(current bounding box.\positioning)]
        \foreach \x/\y/\style [count=\i] in \vertexdata {
            \node[\style] (\i) at (\x, \y) {\i};
        }
        \foreach \i/\j/\style in \edgedata {
            \draw[\style] (\i)--(\j);
        }
        \draw [red] (current bounding box.south west) rectangle (current bounding box.north east);
    \end{tikzpicture}
    \endgroup
}

从图片中可以看出,标签和 TikZ 图片之间存在不必要的间距,这样标签似乎与错误的图片配对。请注意,我添加了 TikZ 图片的边界框,因此这不是导致问题的原因。最终我发现问题出在 xgraph 宏开头但在 TikZ 图片环境之前的 LaTeX 代码。当我将此代码放入 TikZ 图形中时(在我看来非常不自然),问题就解决了。

我的问题是:LaTeX 代码不应该生成输出(变量定义、\directlua 块),为什么会导致文档中出现空格?最好的解决方法是什么?将所有内容都放在 TikZ 图中似乎不太雅致,这真的是标准做法吗?

相关内容