Text FormattingVertical Centering in tabularx

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Timothée
Posts: 1
Joined: Fri Apr 19, 2019 10:44 am

Vertical Centering in tabularx

Post by Timothée »

I have seen many topics on this subject, but it doesn't work with me.

I have a table, and I want to vertical align all the columns.

Consider this code:

Code: Select all

\begin{table}[h]
\centering
\sffamily \begin{tabularx}{\linewidth}{|X|c|c|c|c|c|c|X|}
  \hline
    \multicolumn{2}{|c|}{Topologie} & \#c & Motif & $v \in V$ obligatoire & $D$ & $\Delta$ & Complexité  \\
    \hline
   $G$ & $H$ & \multirow{2}{*}{$2$} & \multirow{2}{*}{$=$} & \multirow{2}{*}{$\varnothing$} & \multirow{2}{*}{$\{1, qlc \}$} & \multirow{2}{*}{qlc} & \multirow{2}{*}{Inapprox} \\
  \cline{1-2} 
  Graphe complet, intervalles propres, split-graph, étoile, $2$-coloration valide, graphe-bloc, arbre & qlc &  &  &   &  &  &  \\
  \hline
\end{tabularx} \normalfont
\caption{Synthèse des conditions étudiées.}
\label{tab:Appr}
\end{table}
As is, this produces:
TAB1.png
TAB1.png (17.68 KiB) Viewed 28462 times
But I want something like this :
TAB2.png
TAB2.png (21.05 KiB) Viewed 28462 times
So, how can I have a column verticaly align ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Vertical Centering in tabularx

Post by rais »

Well, you'll have to redefine the X column to be based on a m-column instead of a p-column, see \tabularxcolumn in tabularx' documentation.
Then you have to take into account, that the number of lines is increased by the number of line breaks introduced by the contents of these X columns. As such, 2 is not enough for your \multirow commands.
Or you rearrange the table a bit, no longer needing the \multirows alltogether:

Code: Select all

\documentclass[a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[left=2cm,right=4cm]{geometry}
\usepackage{amssymb}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}
\begin{table}[ht]
\centering\sffamily
\begin{tabularx}{\linewidth}{|@{}c@{}|c|c|c|c|c|X|}
  \hline
    Topologie & \#c & Motif & $v \in V$ obligatoire & $D$ & $\Delta$ & Complexité  \\
    \hline
    {\begin{tabularx}{4cm}{X|c}
    $G$ & $H$\\
    \hline
    Graphe complet, intervalles propres, split-graph, étoile, $2$-coloration valide, graphe-bloc, arbre & qlc\\
     \end{tabularx}} &
   $2$ & $=$ &$\varnothing$ & $\{1, qlc \}$ & qlc & Inapprox\\
  \hline
\end{tabularx}\normalfont
\caption{Synthèse des conditions étudiées.}
\label{tab:Appr}
\end{table}
\end{document}
Happy Easter;-)
Rainer
iakl
Posts: 1
Joined: Tue Nov 29, 2022 10:10 pm

Vertical Centering in tabularx

Post by iakl »

I honestly can't figure out how Latex stays alive if it makes things as simple and basic as centering a cell so difficult. It blows my mind. I have been working in latex for a couple of months now and for every basic task I have to do, latex has taken it upon itself to do it like climbing Everest. Yes, I have learned a lot. I can do a lot of things now, but I only want to finish this document and look for alternatives, because I would not support another Latex document.
Post Reply