Booktabs Cell Coloring Overflow e Position Bias

Oct 18 2020

Voglio riempire le booktabscelle della tabella in modo personalizzato. Ho fatto due esempi come di seguito, uno con crenatura e l'altro è una booktabstabella diritta . Il primo problema è in entrambe le tabelle, la colorazione che non è al bordo della tabella trabocca fino al bordo della colonna successiva (la linea rossa nell'immagine lo indica). Il secondo problema è la crenatura delle macro (o come si chiamano), distrae il posizionamento dei testi delle celle, come si vede nell'immagine. I testi sono sbilanciati verso il fondo della cella.

  • Come posso evitare il trabocco nella colorazione di una singola cella?
  • Come posso rimuovere il bias di posizione? (Forse questo può essere risolto intrinsecamente dopo aver trovato una soluzione alla prima domanda)

\begin{table}[b!]
    \centering
    \caption{Kern'ed Table.}
    \label{tab:graphExplanation}
    %\renewcommand{\arraystretch}{1.15}
    \extrarowheight=\aboverulesep
    \addtolength{\extrarowheight}{\belowrulesep}
    \aboverulesep=0pt
    \belowrulesep=0pt
    \begin{tabular}{>{\kern-\tabcolsep}cccc<{\kern-\tabcolsep}} 
        \toprule
        
        \multicolumn{2}{c}{MulC1} 
        & \multicolumn{2}{c}{MulC2}  \\
        \cmidrule(r){1-2} 
        \cmidrule(r){3-4}
        
        \textbf{Col1} & \textbf{Col2} & \textbf{Col1} & \textbf{Col2}  \\
        
        Cell & \cellcolor[HTML]{C0C0C0} Cell & Cell & \cellcolor[HTML]{C0C0C0}Cell \\
        
        Cell &  \cellcolor[HTML]{C0C0C0}Cell & Cell &  \cellcolor[HTML]{C0C0C0}Cell \\
        \bottomrule
    \end{tabular}
\end{table}
\begin{table}[b!]
    \centering
    \caption{UnKern'ed Table.}
    \label{tab:graphExplanation}
    %\renewcommand{\arraystretch}{1.15}

    \begin{tabular}{@{}cccc@{}} 
        \toprule
        
        \multicolumn{2}{c}{MulC1} 
        & \multicolumn{2}{c}{MulC2}  \\
        \cmidrule(r){1-2} 
        \cmidrule(r){3-4}
        
        \textbf{Col1} & \textbf{Col2} & \textbf{Col1} & \textbf{Col2}  \\
        
        Cell & \cellcolor[HTML]{C0C0C0} Cell & Cell & \cellcolor[HTML]{C0C0C0}Cell \\
        
        Cell &  \cellcolor[HTML]{C0C0C0}Cell & Cell &  \cellcolor[HTML]{C0C0C0}Cell \\
        \bottomrule
    \end{tabular}
\end{table}

Risposte

3 leandriis Oct 18 2020 at 19:39

Ecco due esempi che utilizzano una colonna vuota tra le prime due e le ultime due colonne:

Invece delle righe del booktabspacchetto, ho usato i comandi regolari \hlinee \cline. Per aumentare simmetricamente le altezze delle righe della tabella, ho utilizzato il cellspacepacchetto o le opzioni corrispondenti dal nicematrixpacchetto.

Ovviamente puoi cambiare la spaziatura sopra e sotto il testo, nonché a sinistra ea destra del testo in una cella a tuo piacimento. L'aggiunta \setlength{\tabcolsep}{3pt}, ad esempio, produrrà il seguente output:

\documentclass{article}

%%%%%%%%%% used in the first example %%%%%%%%%%%%%%%%%
\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{\cellspacetoplimit}
\usepackage{colortbl}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%% used in the second example %%%%%%%%%%%%%%%%
\usepackage{nicematrix}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}



    \begin{tabular}{0c0c0l0c0c} 
        \hline
        \multicolumn{2}{0c}{MulC1} 
        && \multicolumn{02}{c}{MulC2}  \\
        \cline{1-2} \cline{4-5}
        
        \textbf{Col1} & \textbf{Col2} && \textbf{Col1} & \textbf{Col2}  \\
        
        Cell & \cellcolor[HTML]{C0C0C0}Cell && Cell & \cellcolor[HTML]{C0C0C0}Cell \\
        
        Cell &  \cellcolor[HTML]{C0C0C0}Cell && Cell &  \cellcolor[HTML]{C0C0C0}Cell \\
        \hline
    \end{tabular}
    
\bigskip

\NiceMatrixOptions{cell-space-top-limit = 5pt,cell-space-bottom-limit = 5pt}
\begin{NiceTabular}{cclcc}[code-before =\cellcolor{lightgray}{3-2,3-5,4-2,4-5}]
\hline
        \multicolumn{2}{c}{MulC1} 
        && \multicolumn{2}{c}{MulC2}  \\
        \cline{1-2} \cline{4-5}
        
        \textbf{Col1} & \textbf{Col2} && \textbf{Col1} & \textbf{Col2}  \\
        
        Cell & Cell && Cell & Cell \\
        
        Cell &  Cell && Cell &  Cell \\
        \hline
\end{NiceTabular}

\end{document}
3 jsbibra Oct 18 2020 at 21:22

Con l'ottimo nicematrixpacchetto non ci sono problemi

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}
 \begin{NiceTabular}{lccc}[code-before=\cellcolor{blue!30}{3-2,4-2,3-4,4-4}]
        \toprule[2pt]
      \Block{1-2}   {MulC1}     && \Block{1-2}{MulC2} \\
        \cmidrule(l){1-2}        \cmidrule(l){3-4}
        \textbf{Col1} & \textbf{Col2} & \textbf{Col1} & \textbf{Col2}  \\
        Cell &  Cell & Cell & Cell \\
        Cell &  Cell & Cell &  Cell \\
        \bottomrule[2pt]
    \end{NiceTabular}
\end{document}
2 Zarko Oct 18 2020 at 19:25

Sto solo indovinando (troppo a lungo per un commento). Ti piace avere la seguente tabella:

Perché sta usando una parte di ogni tabella. Inoltre vengono modificati \cmidrules:

\documentclass{article}          
\usepackage{booktabs}
\usepackage[table]{xcolor}


\begin{document}
    \begin{table}[b!]
    \centering
    \caption{Kern'ed Table.}
    \label{tab:graphExplanation}
\begin{tabular}{>{\kern-\tabcolsep}cccc }
    \toprule
\multicolumn{2}{c}{MulC1}                   & \multicolumn{2}{c}{MulC2}         \\
    \cmidrule{1-2}
    \cmidrule(l){3-4}

\textbf{Col1}   & \textbf{Col2}             & \textbf{Col1} 
                                                    & \textbf{Col2}             \\
Cell            & \cellcolor{gray!50}{Cell} & Cell  & \cellcolor{gray!50}{Cell} \\
Cell            & \cellcolor{gray!50}{Cell} & Cell  & \cellcolor{gray!50}{Cell} \\
    \specialrule{\heavyrulewidth}{0pt}{2pt}
\end{tabular}
    \end{table}
\end{document}

Modificare:

  • il contenuto dell'ultima colonna è centrato
  • bottomrule è sostituito da \specialrule

Addendum: NEL caso in cui desideri aggiungere \midruletra le intestazioni delle colonne e il corpo della tabella, puoi farlo con \specialrule{\lightrulewidth}{2pt}{0pt}:

\begin{tabular}{>{\kern-\tabcolsep}cccc }
    \toprule
\multicolumn{2}{c}{MulC1}                   & \multicolumn{2}{c}{MulC2}         \\
    \cmidrule{1-2}
    \cmidrule(l){3-4}

\textbf{Col1}   & \textbf{Col2}             & \textbf{Col1}
                                                    & \textbf{Col2}             \\
    \specialrule{\lightrulewidth}{2pt}{0pt}
Cell            & \cellcolor{gray!50}{Cell} & Cell  & \cellcolor{gray!50}{Cell} \\
Cell            & \cellcolor{gray!50}{Cell} & Cell  & \cellcolor{gray!50}{Cell} \\
    \specialrule{\heavyrulewidth}{0pt}{2pt}
\end{tabular}

che dà: