4

I would like to create a table with vertical headers and over the headers I want an extra categorization. But I don't know how to fix the size of the table. Can someone please help me with this? I also always use p{\textwidth} to arrange the size of the table to fit my page. Which is not a practical solution. However, its the only form I've found to fit a table in the page margins. If someone has also a comment on this. I would be happy to get some tips.

\begin{table}[H]
    \centering
    \begin{tabular}{p{.15\textwidth}p{.05\textwidth}p{.05\textwidth}p{.05\textwidth}p{.05\textwidth}p{.05\textwidth}p{.05\textwidth}p{.05\textwidth}p{.05\textwidth}p{.05\textwidth}}
        \toprule
        \textbf{Sensitivity} 
        & \multicolumn{1}{c}{\textbf{Electricity}} 
        & \multicolumn{4}{c}{\textbf{Fuels}} 
        & \multicolumn{2}{c}{\textbf{Transport}} 
        & \multicolumn{2}{c}{\textbf{Othe parameters}} \\ \hline

        &    
        \turnbox{90}{Ren.Expansion [GW]} &
        \turnbox{90}{H$_2$}&  
        \turnbox{90}{CH$_4$}& 
        \turnbox{90}{NH$_3$}&
        \turnbox{90}{CH$_3$OH}&
        \turnbox{90}{BEV private [Mio]}&
        \turnbox{90}{Public Buses [Mio]}&
        \turnbox{90}{CO$_2$ Budget [Mton]}&
        \turnbox{90}{WACC [\%]}\\
        \midrule
         Base &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         A &  5 &  1 & 2 & 3 & 4 & \ 1 & 2 & 10 & 7 \\
         B &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         C &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         D &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         E &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
    \bottomrule
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label}
\end{table}

enter image description here

3

3 Answers 3

5

Something like this?

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{graphicx,booktabs,mhchem,makecell,array}
\let\turnbox\rotatebox % ??

\begin{document}

\begin{table}[ht]
    \centering
    \begin{tabular}{@{} l *{7}{c} wc{.1\textwidth} c @{}}
        \toprule
        \textbf{Sensitivity} &
        \multicolumn{1}{c}{\textbf{\makecell{Elec-\\tricity}}} &
        \multicolumn{4}{c}{\textbf{Fuels}} & 
        \multicolumn{2}{c}{\textbf{\makecell{Trans-\\port}}} & 
        \multicolumn{2}{c}{\textbf{\makecell{Other\\ parameters}}} \\
        & [GW] & & & & & [Mio] & [Mio] & [Mton] & [\%] \\
        \cmidrule(lr){2-2} \cmidrule(lr){3-6}
        \cmidrule(lr){7-8} \cmidrule(l){9-10}
        &
        \turnbox{90}{Ren.\ Expansion} &
        %\turnbox{90}{Fuels}&
        \turnbox{90}{\ce{H2}}   & %liquid and gas??
        \turnbox{90}{\ce{CH4}}  & %liquid and gas??
        \turnbox{90}{\ce{NH3}}  &
        \turnbox{90}{\ce{CH3OH}}&
        %\turnbox{90}{Transport}&
        \turnbox{90}{BEV private}&
        \turnbox{90}{Public Buses}&
        %\turnbox{90}{Other parameters}&
        \turnbox{90}{\ce{CO2} Budget}&
        \turnbox{90}{WACC}\\
        \midrule
         Base  &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         R1.7  &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         G-PP  &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         R-E   &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         T-E   &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         BEV-E &  5 &  1 & 2 & 3 & 4 &  1 & 2 &  10 & 7 \\
         \bottomrule
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label}
\end{table}
\end{document}
3
  • Perfect! Thanks a lot !! Would you mind explaining what this part of the code does. ... {@{} l *{7}{c} wc{.1\textwidth} c @{}} Feb 6 at 13:22
  • @Master_student_H2 - The @{} particles serve to suppress the whitespace padding at the edges of the table. *{7}{c} is the same as ccccccc -- but, hopfully, easier to read. The wc column type is provided by the array package; it defines a column with a fixed width (here: 0.1\textwidth) that centers its contents. I think it's more appropriate to use a w than a p column since automatic line breaking is neither needed nor desirable.
    – Mico
    Feb 6 at 13:35
  • Thanks a lot Mico! Feb 6 at 21:46
3

With {NiceTabular} of nicematrix. You have a built-in command \rotate and, with \RowStyle{\rotate}, that command will be applied to the whole row.

\documentclass{article}

\usepackage{nicematrix}
\usepackage{booktabs}

\usepackage[version=4]{mhchem}

\begin{document}

\begin{table}[ht]
\centering
\begin{NiceTabular}{@{}l*{9}{w{c}{7mm}}@{}}
\toprule
\RowStyle{\bfseries}
Sensitivity
    & Elec.
        &  \Block{1-4}{Fuels}
            &   &   &   &  \Block{1-2}{Transport}
                            &   &   \Block{1-2}{Other\\ parameters}  
                                    &               \\
\cmidrule{2-2}
\cmidrule(rl){3-6}
\cmidrule{7-8}
\cmidrule(l){9-10}
\RowStyle{\rotate}
    &   \Block{}{Ren. Expansion\\ [GW]}
        &   \ce{H2}
            &   \ce{CH4}
                &   \ce{NH3}
                    &   \ce{CH3OH}
                        &   BEV private [Mio]
                            &   Public Buses [Mio]
                                &   \ce{CO2} Budget [Mton]
                                    &   WACC [\%]   \\
\midrule
Base 
    & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 A  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 B  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 C  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 D  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 E  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
\bottomrule
\end{NiceTabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
\end{document}

Output of the above code

2

enter image description here

With rotating, makecell and tabularray packages for table, and mhchem for chem formulas:

\documentclass{article}

\usepackage{rotating}
\usepackage{makecell}
\renewcommand\theadfont{}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage[version=4]{mhchem}

\begin{document}
    \begin{table}[ht]
    \centering
    \settowidth\rotheadsize{Ren. Expansion}
\begin{tblr}{colsep = 3pt,
             colspec = {@{} l *{9}{Q[c,wd=3em]} @{}},
             row{1}  = {font=\small\bfseries, m},
             row{2}  = {cmd=\rothead},
             hspan=minimal 
             }
        \toprule
Sensitivity
    & Elec\-tricity
        &  \SetCell[c=4]{c}  Fuels 
            &   &   &   &  \SetCell[c=2]{c}  Transport 
                            &   &   \SetCell[c=2]{c}  {Other\\ parameters}  
                                    &               \\
    \cmidrule[lr]{2-2}
    \cmidrule[lr]{3-6}
    \cmidrule[lr]{7-8}
    \cmidrule[l]{9-10}
    &   Ren. Expansion [GW]
        &   \ce{H2}
            &   \ce{CH4}
                &   \ce{NH3}
                    &   \ce{CH3OH}
                        &   BEV private [Mio]
                            &   Public Buses [Mio]
                                &   \ce{CO2} Budget [Mton]
                                    &   WACC [\%]   \\
        \midrule
Base 
    & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 A  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 B  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 C  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 D  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
 E  & 5 & 1 & 2 & 3 & 4 & 1 & 2 & 10 & 7 \\
    \bottomrule
    \end{tblr}
\caption{Caption}
\label{tab:my_label}
    \end{table}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .