Tabularx 多列

Tabularx 多列

你好,这是我的桌子:

\documentclass[12pt, oneside,titlepage]{book} % add a4paper
\usepackage[italian]{babel}
\usepackage{geometry}
%\geometry{a4paper, top = 2cm, right = 2cm, left = 2cm, bottom = 2cm}

% Tabelle
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\tcbuselibrary{skins}
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
\tcbset{tab1/.style={fonttitle=\bfseries\large,fontupper=\normalsize\sffamily,
        colback=yellow!10!white,colframe=red!75!black,colbacktitle=Salmon!40!white,
        coltitle=black,center title,freelance,frame code={
            \foreach \n in {north east,north west,south east,south west}
            {\path [fill=red!75!black] (interior.\n) circle (3mm); };},}}
\tcbset{tab2/.style={enhanced,fonttitle=\bfseries,fontupper=\normalsize\sffamily,
        colback=yellow!10!white,colframe=red!50!black,colbacktitle=Salmon!40!white,
        coltitle=black,center title}}
    
\begin{document}
    \begin{tcolorbox}[tab2,tabularx={X|Y|Y|Y},title=Viste materializzate,boxrule=0.5pt] 
    Nome vista & Tempo (s) & Dimensione & Guadagno \\ \hline \hline
    
    A & 0.2 & 16 kB & 0 \%\\ \hline
    B & 10 & 4336 kB & 1 \% \\ \hline
    
\end{tcolorbox}
\end{document}

但我想获得与此类似的模板,即带有“Senza vista”节点和“senza indici”和“con indici”子节点等。我该怎么做?

模板

答案1

包含:{NiceTabular}nicematrix

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\sffamily
\begin{NiceTabular}{ccccc}[hvlines]
\RowStyle[color=white,rowcolor=black]{}
Semestre & \Block{1-2}{Senza Vista} && \Block{1-2}{Con Vista} \\
\RowStyle[rowcolor=black]{}
         & \RowStyle[color=white,rowcolor=gray]{}
           Senza Indici & Con Indici & Senza Indici & Con Indici \\
1 & 284.9 ms & 119.6 ms & 44.7 ms & 1.8 ms \\
2 & 636.66 ms & 137.4 ms & 45.4 ms & 3.88 ms \\
3 & 962.7 ms & 157 ms & 55.7 ms & 4.6 ms \\
4 & 1340 ms & 190.2 ms & 87.6 ms & 6.5 ms 
\end{NiceTabular}


\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

答案2

对于带有数字的列(它们在小数点处对齐),tblr使用 of和tabularrayby列类型:S

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}

\begin{document}
    \begin{table}[ht]
\sisetup{round-mode = places,
         round-precision = 2}
    \begin{tblr}{hlines, vlines,
                colspec = {c S[table-format=4.2{ms}]
                             S[table-format=3.2{ms}]
                             S[table-format=2.2{ms}]
                             S[table-format=1.2{ms}]
                          },
                 row{1} = {bg=black, fg=white},
                 row{2} = {bg=gray,  fg=white},
                 }

\SetCell[r=2]{h, bg=black}   {{{Semestre}}}
    &  \SetCell[c=2]{c} {{{Senza Vista}}}
            &       &   \SetCell[c=2]{c} {{{Con Vista}}}
                                            &           \\
    & {{{Senza Indici}}}
            & {{{Con Indici}}}
                    & {{{Senza Indici}}}
                            & {{{Con Indici}}}          \\
1   & 284.9 \,ms    & 119.6 \,ms    & 44.7 \,ms & 1.8  \,ms \\
2   & 636.66\,ms    & 137.4 \,ms    & 45.4 \,ms & 3.88 \,ms \\
3   & 962.7 \,ms    & 157   \,ms    & 55.7 \,ms & 4.6  \,ms \\
4   & 1340  \,ms    & 190.2 \,ms    & 87.6 \,ms & 6.5  \,ms
    \end{tblr}
    \end{table}
\end{document}

最终结果仅需一次编译:

在此处输入图片描述

相关内容