4

I've been able to make tikz matrix look like basic tabular, but it implies the use of "text height=..." and "text depth=...", which need to be readjusted everytime a new font or a different font size is used. The problem when using "minimum height=..." with "nodes={anchor=center}" is that the horizontal alignment is broken. Without "nodes={anchor=center}" the cells are not aligned most of the time (it depends on the content of the cells), but the text is.

\documentclass[a4paper]{article}

\usepackage[verbose,vmargin=30mm,hmargin=20mm]{geometry}

\usepackage{showframe} \renewcommand\ShowFrameLinethickness{0.15pt} \renewcommand*\ShowFrameColor{\color{red}}

\usepackage{tikz} \usetikzlibrary{positioning, matrix,calc}

\tikzset{ allmatrix/.style = {matrix of nodes, nodes in empty cells, row sep=-\pgflinewidth, column sep=-\pgflinewidth,}}

\setlength{\parindent}{0pt} \begin{document}

\begin{tikzpicture} \matrix at (0,0) [allmatrix,matrix anchor=north west,inner sep=0pt,nodes={ inner sep=3pt,outer sep=0pt,draw,minimum height=6mm,text width=4mm}]{ a & b & c \ d & e & f \ }; \end{tikzpicture} \par \begin{tabular}{|l|l|l|} \hline a & b & c \ \hline d & e & f \ \hline \end{tabular} \end{document}

enter image description here

2 Answers2

3

You need to define text height and nodes anchors. For example as is done in the following example:

\documentclass[a4paper]{article}
\usepackage[verbose,
            vmargin=30mm,hmargin=20mm]{geometry}

\usepackage{showframe} \renewcommand\ShowFrameLinethickness{0.15pt} \renewcommand*\ShowFrameColor{\color{red}}

\usepackage{tikz} \usetikzlibrary{matrix}

\tikzset{ allmatrix/.style = {matrix of nodes, nodes in empty cells, row sep=-\pgflinewidth, column sep=-\pgflinewidth} }

\setlength{\parindent}{0pt} \begin{document}

\matrix at (0,0) [allmatrix, matrix anchor=north west, inner sep=0pt, nodes={draw, text height=2.4ex, text depth=1ex, text width=6mm, align=center, anchor=center} ] { a & b & c \ d & e & f \ }; \end{tikzpicture} \par \begin{tabular}{|l|l|l|} \hline a & b & c \ \hline d & e & f \ \hline \end{tabular} \end{document}

enter image description here

Zarko
  • 296,517
3

The environment {NiceTabular} of nicematrix is an environment {tabular} (of the package array) which creates PGF/Tikz nodes under the cells, rows and columns.

Here is an example:

\documentclass[a4paper]{article}
\usepackage{nicematrix,tikz}

\begin{document} Here is a classical \verb|{tabular}| (of the package \verb|array|). \begin{tabular}{|c|c|c|} \hline a & b & c \ \hline d & e & f \ \hline \end{tabular}

\bigskip Here is an \verb|{NiceTabular}| (of the package \verb|nicematrix|). \begin{NiceTabular}{|c|c|c|} \Hline a & b & c \ \Hline d & e & f \ \Hline \CodeAfter \tikz \draw [red,thick] (2-|2) rectangle (3-|3) ; \end{NiceTabular} \end{document}

Output of the above code

As illustration, in the second tabular, I have drawn the red frame with the following instruction:

\tikz \draw [red,thick] (2-|2) rectangle (3-|3) ;

That shows that you have access via Tikz to the individual cells of the tabular with exactly the same delimitations as the cells of the {tabular}.

F. Pantigny
  • 40,250
  • Remark: The package nicematrix provides tools to draw the red frame without using explicitely Tikz (with the command \Block). Here, I have used Tikz only as an illustration. – F. Pantigny Mar 04 '22 at 10:48
  • this is nice indeed but it changes wholly the way i do my document since i only use tikz nodes to place my images, tables, texts where i want. For example i place an image at (current page text area.north) [anchor=north] (myimage), and i need a table at (myimage.north west) [matrix anchor=north east] an one at (myimage.north east) [matrix anchor=north west]. I do not see how to do that without tikz. – user1850133 Mar 05 '22 at 12:43
  • how would you do this englishcol/.style={column #1/.append style={execute at begin node=\begin{english},execute at end node=\end{english}} with nicematrix? – user1850133 Mar 05 '22 at 16:35
  • Maybe \begin{NiceTabular}{>{\begin{english}c<{\end{english}ccc} etc. – F. Pantigny Mar 05 '22 at 17:37