第一行表格幻影问题

第一行表格幻影问题

在此代码中,幻影线在表格内部有效,但在第一个和最后一个表格中无效。如何修复此问题?

主要目标是在表格的每个单元格的每个角上放置一个幻影 tikz 标记。仍然要使外面的垂直线消失。

编辑我稍微改进了我的代码:

在此处输入图片描述

\documentclass{article}
\usepackage{tikz,array,xparse,luacode}

\usetikzlibrary{tikzmark}
\newcounter{TabTikzCount}

\makeatletter
\NewDocumentCommand{\TabTikzMark}{sm}{%
    \IfBooleanTF{#1}{%
        \\[-\ht\@arstrutbox]\noalign{\setcounter{TabTikzCount}{1}}%
        \Row{#2}%
        \\[-\dp\@arstrutbox]%
        }{%
        \\[-\ht\@arstrutbox]%
        \Row{#2}%
        \\[-\dp\@arstrutbox]}}
\makeatother

\begin{luacode}
function row (s)
    a = "\\multicolumn{1}{@{}c@{}}{\\tikzmark{\\Alph{TabTikzCount}0}"
    for i = 1,s-1
    do
    a=a.."\\strut\\hfill\\tikzmark{\\Alph{TabTikzCount}"..i.."}}&\\multicolumn{1}{@{}c@{}}{"
    end
    a=a.."\\strut\\hfill\\tikzmark{\\Alph{TabTikzCount}"..s.."}}"
    tex.print (a)
    print (a)
end
\end{luacode}

\newcommand{\Row}[1]{%
\directlua{row(#1)}
\stepcounter{TabTikzCount}}

\begin{document}

\begin{tabular}{|c|c|c|c|}
\TabTikzMark*{4}\hline
a&z&a&d\TabTikzMark{4}\hline
a&z&a&d\TabTikzMark{4}\hline
\end{tabular}

\end{document}

答案1

这是一个解决方案,得到了多位贡献者的帮助:

在此处输入图片描述

\documentclass{article}
\usepackage{tikz,array,xparse,luacode,tabularx}

\usetikzlibrary{tikzmark}
\newcounter{TikzTabCount}

\makeatletter
\NewDocumentCommand{\TikzTabEnd}{O{\NbCols}}{%
    \\[-\ht\@arstrutbox]%
    \Row{#1}\stepcounter{TikzTabCount}%
    \\[-\dp\@arstrutbox]}
\makeatother

\ExplSyntaxOn
\NewDocumentCommand{\CountColumns}{m}
 {% count the number of & tokens in \@preamble
  \regex_count:nvN { \cT\& } { @preamble } \l_tmpa_int
  % they're one less than the columns
  \cs_gset:Npx #1 { \int_eval:n { 1 + \l_tmpa_int } }
 }
\cs_generate_variant:Nn \regex_count:nnN { nv }
\ExplSyntaxOff

\NewDocumentEnvironment{TikzTab}{md<>O{c}mO{\NbCols}}{%
    \IfNoValueTF{#2}{%
        \setcounter{TikzTabCount}{2}%
        \begin{#1}[#3]{#4}%
        }{%
        \tabularx{#2}[#3]{#4}%
    }%
    \noalign{\CountColumns{#5}\tikzmark[yshift=5pt]{TTtop}}%
}{%
    \IfNoValueTF{#2}{%
        \end{#1}%
        }{%
        \endtabularx%
        }%
    \foreach \i in {0,...,4} {%
        \tikz[overlay,remember picture]
        \tikzmark{A\i}{({pic cs:TTtop}-|{pic cs:B\i})};
    }
}

\newcommand{\Row}[1]{\directlua{row(#1)}}

\begin{luacode}
function row (s)
    a = "\\multicolumn{1}{@{}c@{}}{\\tikzmark{\\Alph{TikzTabCount}0}"
    for i = 1,s-1
    do
    a=a.."\\strut\\hfill\\tikzmark{\\Alph{TikzTabCount}"..i.."}}&\\multicolumn{1}{@{}c@{}}{"
    end
    a=a.."\\strut\\hfill\\tikzmark{\\Alph{TikzTabCount}"..s.."}}"
    tex.print (a)
    --print (a)
end
\end{luacode}


\begin{document}

\tikz[overlay,remember picture]
    \fill[blue!10] (pic cs:A0) rectangle (pic cs:B4) ;

\tikz[overlay,remember picture]
    \fill[green!20] [rounded corners=8pt] (pic cs:B2) 
        -- (pic cs:C2) -- (pic cs:C4) [rounded corners=0pt]
        -- (pic cs:B4) -- cycle  ;

\foreach \j in {A,B,C} {%
\foreach \i in {0,...,4} {%
    \tikz[overlay,remember picture]
        \node[red] at (pic cs:\j\i) {\tiny \j\i} ;
}}

essai

$\begin{TikzTab}{array}{|c|c|cm{2cm}|}\hline
    \rule{1pt}{1.5em}a&z&a&d\TikzTabEnd\hline
    \rule[-.5em]{1pt}{1.5em}a&z&a&d\pi\TikzTabEnd\hline
\end{TikzTab}$

essai


\end{document}

相关内容