Quel est le problème avec ce tabularx?

Aug 26 2020

Je déteste les tables en LaTeX! J'ai toujours des problèmes, et pourtant cela devrait être facile:

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{tabularx}

\begin{document}
 
\begin{center}
    \renewcommand{\arraystretch}{1.5}
    \begin{tabularx}{\textwidth}{|X|X|X|X|X|}
    \hline
        \rowcolor[gray]{0.95}
        \multicolumn{5}{|c|}{Title}
        \\ \hline
        \multicolumn{1}{|c|}{\textsf{Day 1}}
        & \multicolumn{1}{c|}{\textsf{Day 2}}
        & \multicolumn{1}{c|}{\textsf{Day 3}}
        & \multicolumn{1}{c|}{\textsf{Day 4}}
        & \multicolumn{1}{c|}{\textsf{Day 5}}
        \\ \hhline{|=|=|=|=|=|}
        \multicolumn{1}{|c|}{Text 1}
        & \multicolumn{1}{c|}{Text 2}
        & \multicolumn{1}{c|}{Text 3}
        & \multicolumn{1}{c|}{Text 4}
        & \multicolumn{1}{c|}{Text 5}
        \\
        \multicolumn{1}{|c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        \\ \hline
    \end{tabularx}
\end{center}

\end{document}

Aperçu du problème:

Comme le montre l'image, la table ne se ferme pas sur le côté droit. Quel est le problème avec le code?

Réponses

6 Mico Aug 26 2020 at 11:57

Tu as demandé,

Quel est le problème avec le code?

Le code encapsule (entombe?) Chaque cellule dans un \multicolumn{1}{|c|}{...}wrapper, remplaçant ainsi complètement la Xcapacité du type de colonne à fournir un saut de ligne automatique à l'intérieur des cellules. En conséquence, il n'y a absolument aucune raison de s'attendre à ce que la largeur de l' tabularxenvironnement soit égale \textwidth.

Le remède? Vous devez (a) vous débarrasser des \multicolumn{1}{|c|}{...}enveloppes, afin que la tabularxmachine puisse réellement faire son travail, et (b) définir et utiliser une version du Xtype de colonne qui centre plutôt que justifie pleinement le contenu de la cellule. Consultez le code ci-dessous pour savoir comment satisfaire la deuxième condition.

Pour l'exemple en question, réduire la valeur de \tabcolsep- le paramètre qui régit la quantité d'espace blanc intercolonne - de moitié réussit à éviter les sauts de ligne dans la dernière ligne. Voir le deuxième tabularxexemple dans la capture d'écran suivante pour le "look" résultant.

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage[total={6in,10in}, left=1.5in,top=0.5in, 
            includehead,includefoot]{geometry}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{multirow,hhline}
\usepackage{tabularx}
\usepackage{ragged2e} % for '\Centering' macro
% Define a centered version of 'X' column type:
\newcolumntype{C}{>{\Centering}X} 
% or: \newcolumntype{C}{>{\centering\arraybackslash}X} 

\begin{document}

\begin{center}
\renewcommand{\arraystretch}{1.5}
    \begin{tabularx}{\textwidth}{|*{5}{C|}} % <-- "C", not "X"
    \hline
    \rowcolor[gray]{0.95}
    \multicolumn{5}{|c|}{\texttt{tabularx} with default value of \texttt{\string\tabcolsep}}
    \\ \hline
    \textsf{Day 1}
    & \textsf{Day 2} & \textsf{Day 3} & \textsf{Day 4} & \textsf{Day 5}
    \\ \hhline{|=|=|=|=|=|}
    Text 1 & Text 2 & Text 3 & Text 4 & Text 5 \\
    12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00
    \\ \hline
    \end{tabularx}

\bigskip
\setlength\tabcolsep{3pt} % default value: 6pt

    \begin{tabularx}{\textwidth}{|*{5}{C|}} % <-- "C", not "X"
    \hline
    \rowcolor[gray]{0.95}
    \multicolumn{5}{|c|}{\texttt{tabularx} with reduced value of \texttt{\string\tabcolsep}}
    \\ \hline
    \textsf{Day 1}
    & \textsf{Day 2} & \textsf{Day 3} & \textsf{Day 4} & \textsf{Day 5}
    \\ \hhline{|=|=|=|=|=|}
    Text 1 & Text 2 & Text 3 & Text 4 & Text 5 \\
    12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00
    \\ \hline
    \end{tabularx}
\end{center}

\end{document}
1 jsbibra Aug 26 2020 at 11:14

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{tabularx}

\begin{document}
 
\begin{center}
    \renewcommand{\arraystretch}{1.5}
    \begin{tabular}{|c|c|c|c|c|}
    \hline
        \rowcolor[gray]{0.95}
        \multicolumn{5}{|c|}{Title}
        \\ \hline
        \multicolumn{1}{|c|}{\textsf{Day 1}}
        & \multicolumn{1}{c|}{\textsf{Day 2}}
        & \multicolumn{1}{c|}{\textsf{Day 3}}
        & \multicolumn{1}{c|}{\textsf{Day 4}}
        & \multicolumn{1}{c|}{\textsf{Day 5}}
        \\ \hhline{|=|=|=|=|=|}
        \multicolumn{1}{|c|}{Text 1}
        & \multicolumn{1}{c|}{Text 2}
        & \multicolumn{1}{c|}{Text 3}
        & \multicolumn{1}{c|}{Text 4}
        & \multicolumn{1}{c|}{Text 5}
        \\
        \multicolumn{1}{|c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        \\ \hline
    \end{tabular}
\end{center}

\end{document}