Simple way to get tabular with proportional column width and both vertical and horizontal centering of cell content

tabularxtikz-pgf

I'm searching how to have simultaneously :

  • a proportional array definition (say, "this column represent 20% of the array width, the next is 30%" and so on) ;
  • content centered both vertically and horizontally.

I've searched through StackExchange and ended with this code :

\documentclass[12pt, a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\usepackage{tabularx}
\usepackage{geometry}
\usepackage[french]{babel}
\usepackage{tikz}

\geometry{top=1.5cm, bottom=1.5cm, left=1cm, right=1cm,
          headheight=1cm, headsep=0.5cm,
          footskip=1cm}

\begin{document}

Compléter le tableau suivant:

\renewcommand{\arraystretch}{3}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\newcolumntype{C}[1]{>{\hsize=#1\hsize\centering\arraybackslash}X}

\begin{tabularx}{\linewidth}{|C{.6}|C{1.4}|C{1.4}|C{.6}|}
%\begin{tabularx}{\linewidth}{|C{.15}|C{.35}|C{.35}|C{.15}|}
    \hline
    Inégalité & Phrase & Représentation graphique & Intervalle\\
    \hline
    $x<2$ & Bla & \fbox{\begin{tikzpicture}[scale=0.5, >=stealth]
                % Repère
                \def\xm{-4.9}
                \def\xM{-\xm}
                \draw[thick,->] (\xm,0) -- (\xM,0) ; 
                \foreach \x in {-4,...,4} {\node at (\x,0) {\tiny $|$} ; }
                \node[below] at (0,0) {\footnotesize $0$} ;
                \node[below] at (1,0) {\footnotesize $1$} ;
                % Intervalle
                \draw[line width=2.5pt] (-2,0) node {$]$} --(4,0) node {$]$} ;
    \end{tikzpicture}} & $x\in\ldots$ \\
    \hline
\end{tabularx}
\end{document}

Problems (see picture below) :

  • the array width is too large, and I can't figure out why ;
  • with redefining \arraystretch, the content is not anymore vertically centered ;
  • the TikZ scheme is not vertically centered. If I embed it inside a new \parbox[c], I need to repeat horizontal centering instruction, which I find redundant and inelegant.

enter image description here

Additionnally, the tabularx declaration \begin{tabularx}{\linewidth}{|C{.15}|C{.35}|C{.35}|C{.15}|} gives the same result, whereas it should'nt (if I understood right this answer, for example).

Thanks in advance for any tip or help.

Best Answer

With use of the tabularray package, table code is simple:

\documentclass[12pt, a4paper]{book}
\usepackage[hmargin=1cm, vmargin=1.5cm,
            headheight=1cm, headsep=0.5cm,
            footskip=1cm,
            showframe
            ]{geometry}

\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\usepackage[french]{babel}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\tikzset{TAB/.style = {
           > = Straight Barb,
    BRR/.style = {{Bracket[reversed,length=0.5mm]}-{Bracket[reversed,length=0.5mm]}, 
                  shorten <=-.5mm,shorten >=-0.5mm},
    lbl/.style = {below, font=\scriptsize},
                    }
        }
\usepackage{tabularray}


\begin{document}
                    
Compléter le tableau suivant:

\noindent%
\begin{tblr}{hlines, vlines,
             colspec={X[0.6,c] X[c] X[c] X[0.6,c]},
             rowsep=7pt,
             row{1} = {font=\bfseries},
             }
Inégalité & Phrase & Représentation graphique & Intervalle\\
$x<2$ & Bla & \fbox{\begin{tikzpicture}[TAB,baseline=(Base.base)]
                \draw[->] (-2.4,0) -- (2.4,0) ;
                \foreach \x in {-4,...,4}
                    \draw (0.5*\x,1mm) -- ++ (0,-2mm);
                \node (Base) [lbl] at (0.0,0) {0};
                \node        [lbl] at (0.5,0) {1};
                \draw[very thick, BRR]
                    (-1,0) -- (2,0);
                    \end{tikzpicture}
                    }
                & $x\in\ldots$      \\
\end{tblr}
\end{document}

enter image description here