\tikzmark
在循环中的使用\foreach
会导致以下错误消息:
! 未定义的控制序列。\tikzmark ...reid \@undefined \let \tikzmark@next =\tikzmark@outside \else > ...
l.45 \TabTikzMark*{3} \hline
使用以下代码:
\documentclass{article}
\usepackage{tikz,array,xparse}
\usetikzlibrary{tikzmark}
\newcounter{TabTikzCount}
\makeatletter
\NewDocumentCommand{\TabTikzMark}{sm}{%
\IfBooleanTF{#1}{%
\\[-\ht\@arstrutbox]\noalign{\setcounter{TabTikzCount}{1}}%
\Row{3}
\\[-\dp\@arstrutbox]%
}{%
\\[-\ht\@arstrutbox]
\Row{3}
\\[-\dp\@arstrutbox]}}
\makeatother
\newcommand{\Row}[1]{
\xdef\MyRow{{\Alph{TabTikzCount}0}}
\foreach \n [evaluate=\n as \m using int(\n-1)] in {2,...,#1} {%
%\xdef\MyRow{\MyRow\strut\hfill\Alph{TabTikzCount}\m&}
\xdef\MyRow{\MyRow\strut\hfill\tikzmark{\Alph{TabTikzCount}\m}&}
}%
%\MyRow\hfill\Alph{TabTikzCount}#1%
\MyRow\hfill\tikzmark{\Alph{TabTikzCount}#1}%
\stepcounter{TabTikzCount}}
\begin{document}
essai
\begin{tabular}{|c|c|c|}
\TabTikzMark*{3}\hline
a&z&a\TabTikzMark{3}\hline
a&z&a\TabTikzMark{3}\hline
\end{tabular}
essai
\tikz[overlay,remember picture]\draw[->] (pic cs:A1) -- (pic cs:B2) ;
\end{document}
答案1
这样可以避免预扩展\tikzmark
并且运行时不会出现错误,但你没有说出你想要的结果,我不确定这是否是预期的输出
\documentclass{article}
\usepackage{tikz,array,xparse}
\usetikzlibrary{tikzmark}
\newcounter{TabTikzCount}
\makeatletter
\NewDocumentCommand{\TabTikzMark}{sm}{%
\IfBooleanTF{#1}{%
\\[-\ht\@arstrutbox]\noalign{\setcounter{TabTikzCount}{1}}%
\Row{3}
\\[-\dp\@arstrutbox]%
}{%
\\[-\ht\@arstrutbox]
\Row{3}
\\[-\dp\@arstrutbox]}}
\makeatother
\newcommand{\Row}[1]{%
\xdef\MyRow{{\Alph{TabTikzCount}0}}%
\foreach \n [evaluate=\n as \m using int(\n-1)] in {2,...,#1} {%
\let\tikzmark\relax
%\xdef\MyRow{\MyRow\strut\hfill\Alph{TabTikzCount}\m&}%
\xdef\MyRow{\MyRow\strut\hfill\tikzmark{\Alph{TabTikzCount}\m}&}%
}%
%\MyRow\hfill\Alph{TabTikzCount}#1%
\MyRow\hfill\tikzmark{\Alph{TabTikzCount}#1}%
\stepcounter{TabTikzCount}}
\begin{document}
essai
\begin{tabular}{|c|c|c|}
\TabTikzMark*{3}\hline
a&z&a\TabTikzMark{3}\hline
a&z&a\TabTikzMark{3}\hline
\end{tabular}
essai
\tikz[overlay,remember picture]\draw[->] (pic cs:A1) -- (pic cs:B2) ;
\end{document}