将堆栈数据传递给 tikz 节点名称

将堆栈数据传递给 tikz 节点名称

我正在尝试使用堆栈数据结构(如此处所述:推送/弹出或者保存长度/尺寸?)来存储一组 tikzmark 节点名称的各种名称(如在tikzmark手动的) 散布在我的文本之中。

单独的堆栈实现(WE1)和\tikzmark(WE2)运行完美,但它们的组合行为不知何故与多个错误发生冲突。

当我尝试将弹出的堆栈数据元素作为节点名称传递到命令中时(下面的 MWE),我无论如何也想不出出了什么问题\tikzmark。这是由于我的 LaTeX 代码没有立即评估 \pop 命令而引起的问题吗(我尝试过\expandafter\edef没有成功……)?我也尝试过传递\pop{\latexstack}给 a \newcommand{},但我也没有设法让该方法起作用……

平均能量损失[不正确的组合行为...]

\documentclass{article}
\usepackage{blindtext, tikz}
    \newtoks\latexstack
    \latexstack={\empty}

    \def\push#1#2{
        \begingroup
        \toks0={{#1}}
        \edef\act{\endgroup\global#2={\the\toks0 \the#2}}\act
    }

    \def\pop#1{
        \begingroup
        \edef\act{\endgroup\noexpand\SplitOff\the#1(tail)#1}\act
    }

    \def\SplitOff#1#2(tail)#3{
        \ifx#1\empty
            \errhelp{Attempting to pop empty stack #3.}
            \errmessage{You can't pop an empty stack.}
        \else
            #1\global#3={#2}
        \fi
    }

    \newcommand\tikzmark[1]{\tikz[overlay,remember picture] \node(#1) {};}

\begin{document}
    \push{A1}{\latexstack}
    \push{B2}{\latexstack}

    \blindtext \tikzmark{\pop{\latexstack}}\\ 
    \blindtext \tikzmark{\pop{\latexstack}}\\ 

    \begin{tikzpicture}[remember picture,overlay,scale=0.5] 
        \draw[red] (A) -- (B);
    \end{tikzpicture}
\end{document}

WE1[堆栈数据结构的正确行为:A1、B2、C3 和 D4 被推送并按顺序从数据结构中弹出]

\documentclass{article}
    \newtoks\latexstack
    \latexstack={\empty}

    \def\push#1#2{
        \begingroup
        \toks0={{#1}}
        \edef\act{\endgroup\global#2={\the\toks0 \the#2}}\act
    }

    \def\pop#1{
        \begingroup
        \edef\act{\endgroup\noexpand\SplitOff\the#1(tail)#1}\act
    }

    \def\SplitOff#1#2(tail)#3{
        \ifx#1\empty
            \errhelp{Attempting to pop empty stack #3.}
            \errmessage{You can't pop an empty stack.}
        \else
            #1\global#3={#2}
        \fi
    }

\begin{document}
    \push{A1}{\latexstack}
    \push{B2}{\latexstack}
    \push{C3}{\latexstack}
    \push{D4}{\latexstack}

    \pop{\latexstack},\\ 
    \pop{\latexstack},\\ 
    \pop{\latexstack},\\ 
    \pop{\latexstack}.
\end{document}

WE2[正确做法\tikzmark:点 A 和 B 被标记,并用红线连接在一起]

\documentclass{article}
\usepackage{blindtext, tikz}

    \newcommand\tikzmark[1]{\tikz[overlay,remember picture] \node(#1) {};}

\begin{document}
    \blindtext \tikzmark{A}\\
    \blindtext \tikzmark{B}

    \begin{tikzpicture}[remember picture,overlay,scale=0.5] 
        \draw[red] (A) -- (B);
    \end{tikzpicture}
\end{document}

答案1

我认为这里的问题是扩展。当 TikZ 创建节点时,节点名称使用 完全扩展\edef

假设原始代码如下,请考虑以下内容

\push{A1}{\latexstack}
\push{B2}{\latexstack}
\edef\nodename{\pop{\latexstack}}
\show\nodename

这说明了完整的扩展并将给出(在日志文件中):

! Undefined control sequence.
\pop #1^^@- \begingroup \edef \act 
                                 {\endgroup \noexpand \SplitOff \the #1(tail...
l.36     \edef\nodename{\pop{\latexstack}
                                       }
! Undefined control sequence.
\pop ... \noexpand \SplitOff \the #1(tail)#1}\act 

l.36     \edef\nodename{\pop{\latexstack}
                                       }
> \nodename=macro:
-> \begingroup \edef {\endgroup \SplitOff {B2}{A1}\empty (tail)\latexstack }.
l.37     \show\nodename

注意问题是不是这是未定义的。如果放置在序言中的某个位置,\act错误将消失,但 的内容仍然是一大堆 LaTeX 代码,而不是所需的节点名称。\let\act=\relax\nodename

一个解决方案是使用一个\popto命令将堆栈顶部的值弹出到一个宏中,然后将其用作节点名(注意:我还重新实现了该\push宏):

\def\push#1#2{%
  \def\tmp{{#1}}%
  \expandafter\expandafter\expandafter%
    #2\expandafter\expandafter\expandafter{\expandafter\tmp\the#2}%
\ignorespaces}

\def\popto#1#2{%
  \expandafter\SplitOffTo\the#1\stop{#1}{#2}%  
}

\def\SplitOffTo#1#2\stop#3#4{%
  \def\tmp{#1}%
  \ifx\tmp\empty%
    \errhelp{Attempting to pop empty stack #3.}%
    \errmessage{You can't pop an empty stack.}%
  \else%
    \def#4{#1}\global#3={#2}%
  \fi%
}

可以像这样使用:

\newtoks\latexstack
\latexstack={\empty}
\push{A1}{\latexstack}
\push{B2}{\latexstack}

\popto{\latexstack}{\nodename}
\tikzmark{\nodename}
\show\nodename
Some text \tikzmark{\nodename}

\popto{\latexstack}{\nodename}
\tikzmark{\nodename}
\show\nodename
Some more text \tikzmark{\nodename}

日志文件将显示(类似)以下内容:

> \nodename=macro:
->B2.
l.31 \show\nodename

> \nodename=macro:
->A1.
l.34 \show\nodename

如果所有这些看起来有点麻烦,那么这些\popto东西可以被绑定在\tikzmarkpop宏中:

\documentclass[border=5]{standalone}
\usepackage{tikz}

\def\push#1#2{%
  \def\tmp{{#1}}% 
  \expandafter\expandafter\expandafter%
    #2\expandafter\expandafter\expandafter{\expandafter\tmp\the#2}%
\ignorespaces}

\def\popto#1#2{%
  \expandafter\SplitOffTo\the#1\stop{#1}{#2}%  
}

\def\SplitOffTo#1#2\stop#3#4{% 
  \def\tmp{#1}
  \ifx\tmp\empty% 
    \errhelp{Attempting to pop empty stack #3.}%
    \errmessage{You can't pop an empty stack.}%
  \else%
    \def#4{#1}\global#3={#2}%
  \fi}


\def\tikzmarkpop#1{%
\popto{#1}{\nodename}%
\tikz[remember picture, overlay]\coordinate(\nodename);} 

\newtoks\latexstack
\latexstack={\empty}

\begin{document}

\push{A1}{\latexstack}
\push{B2}{\latexstack}  

Some text\tikzmarkpop{\latexstack}
Some more text\tikzmarkpop{\latexstack}

\begin{tikzpicture}[remember picture, overlay] 
  \draw [red, <->] (A1) -- (B2);
\end{tikzpicture}
\end{document}

enter image description here

相关内容