1

In the next code, I hoped the tikzmark should be placed on the tabular black lines, but there is a drift.

The main goal is to put a phantom tikz mark in each corner of each cell of a table.

enter image description here

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

\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

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

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

\end{document}
  • 1
    In \TabTikzMark you have \Row{3} hardcoded, I think it should be \Row{#2}. If I make that change, then I get the nodes in the right places. – Andrew Stacey Mar 4 '18 at 16:55
  • You are asking a lot of questions about this code but you haven't given any top level description of the intent here, the interaction with tabular seems flawed but hard to correct without knowing what the code is trying to do – David Carlisle Mar 4 '18 at 17:00
  • @LoopSpace Thank you, I have forgotten this. – Tarass Mar 4 '18 at 17:09
  • @DavidCarlisle Put a tikz mark in each corner of each case of a table. I will add it to this question, I did it for some others, followin your advice. – Tarass Mar 4 '18 at 17:12
  • but if you just want the marks on the outer corners of the table why are you adding marks within the table? you could put a mark at each corner of a box without knowing whether the content of a box is a table or a word or an image etc – David Carlisle Mar 4 '18 at 17:21
2

You are adding white space:

enter image description here

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

\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

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

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

\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.