1

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.

natsirt
  • 635

2 Answers2

1

Your table is not too large, it is indented. Use \noindent and everything is fine. To have some padding at the top and bottom of cells, rather than playing with the value of \arraystretch, I'd recommend loading the cellspace package, which enables you to define minimal distances between the top of cells and the above row, and between the bottom of cells and the below row, in columns with specifier prefixed with the letter S (or whatever letter you declare if you load siunitx, which defines an S column type).

\documentclass[12pt, a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\usepackage{tabularx}
\usepackage[showframe]{geometry}
\usepackage[french]{babel}
\usepackage{tikz}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{10pt}
\setlength{\cellspacebottomlimit}{10pt}
\addparagraphcolumntypes{X}

\geometry{vmargin=1.5cm, hmargin=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}S{X}}

\noindent \begin{tabularx}{\linewidth}{|C{.6}|C{1.4}|C{1.4}|C{.6}|} \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}

enter image description here

Bernard
  • 271,350
  • Thanks. Just had a surprise with deleting the content of the last column in your MWE : I got HUGE vertical spacing ! Any idea why ? – natsirt Nov 02 '21 at 14:57
  • 1
    That's quite strange. However, note that if you replace the contents of this last cell with \mbox{} or \leavevmode, there's no problem. I really can't see why this behaviour. – Bernard Nov 02 '21 at 17:56
  • I've tried with a single \ and it did the trick, but I had rough time to discover the source of the problem ! There are too many weird issues with LaTeX and all those numerous packages one must use to do things that should be "simple", I'm tired to fight them... Many thanks for helping, though... – natsirt Nov 02 '21 at 21:19
1

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

Zarko
  • 296,517
  • Seems promising but I had to install tabularray and still have an error with ninecolors.sty. Should I update my TeXlive (2020) ? – natsirt Nov 02 '21 at 15:12
  • The error with ninecolors.sty vanished as I installed the required .sty. Are these packages present in 2021 version of TeXlive ? Thanks ! – natsirt Nov 02 '21 at 15:17
  • @natsirt, I don't know. I use MiKTeX, and there I with its wizard I add tabularry to my LaTeX installaltion. – Zarko Nov 02 '21 at 15:27
  • Okay, these packages are in Texlive 2021. But I'm still facing an odd issue, when trying to center vertically TikZ drawing (arrow exactly centered)... I had to add a white label above to have symmetry and to tweak tkizpicture environment definition to slightly move it : baseline={([yshift=-.5ex]current bounding box.center)}. Can you figure a way to achieve this in a simpler way ? Thanks in advance. – natsirt Nov 02 '21 at 21:16
  • For such exactly vertical centering you need manually tweak image baseline by traying. In this particular case a bit better centering you will get with baseline=(Base.340) – Zarko Nov 02 '21 at 21:59