2

my issue is that data in the third col required too much space! Is there any way that I can force that table to assign the right width to each col?

I'm using tabularx

\begin{table}
 \newcolumntype{C}{>{\centering\arraybackslash}X}
 \centering
 \captionsetup{justification=centering, hangindent=-0.2em}
 \caption{some caption}
\label{tab:somelabel}
\begin{tabularx}{1.5\textwidth}{|*{9}{C|}}              
    \hline
             \multirow{2}{*}{} & \multicolumn{2}{|c|}{Gesamtkosten} & \multicolumn{2}{|c|}{Länge eines Fahrzeugumlaufs} & \multicolumn{2}{|c|}{$\#$Elektrobusse} & \multicolumn{2}{|c|}{$H^V$} \\
    \cline{2-9}
    & \vspace{1pt} (K) & \vspace{1pt} (V) & \vspace{1pt} (K) & \vspace{1pt} (V) & \vspace{1pt} (K) & \vspace{1pt} (V) & \vspace{1pt} (K) & \vspace{1pt} (V)\\
    \hline
    \multicolumn{9}{|c|}{\textbf{some description}} \\
    \hline  
    \hspace{-9pt} I$1$~($90$)& $44.490.046$ & $37.214.853~(16~\%)$ & $6.224$ & $5.992~(4~\%)$ & $49$ & $15~(69~\%)$ & $77$ & $76~(1~\%)$\\
    \end{tabularx}
\end{table}

Thanks in advance!

  • Welcome to TeX SX! Could you post 1) a full compilable code; 2) a code from real life: LaTeX won't knnow how to hyphenate words like loooooooooong, so it will overflow into the next cell. – Bernard Sep 26 '17 at 19:56
  • those are not the issue! they perfectly fit. But if the content of a cell is longer than the col header, e.g. $37.214.853~(16~\%)$, then tis cell goes over the right hand side – Thomas H. Sep 26 '17 at 20:08
  • so, I've edited the headlines above with the real ones (it's German)... and as everything is included in a landscape, the table is 1.5 times \textwidth – Thomas H. Sep 26 '17 at 20:13
  • I don't want to have a linebreak there ... I'd like the col to be as width as it should be in ordner to display the content in between the cell – Thomas H. Sep 26 '17 at 20:16
1

Since you don't want to allow linebreaks in the cells, there's no point in using a tabularx environment. I suggest you use a tabular* environment instead. By design, the columns won't all have the same widths. That's OK, though, right?

I would also like to suggest that you give your table a much more open "look", both by omitting all vertical rules and by using well-spaced horizontal rules (courtesy of the booktabs package).

enter image description here

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage[ngerman]{babel}
\usepackage{array,caption,rotating,booktabs}

\begin{document}
\begin{sidewaystable}
\setlength\tabcolsep{0pt}
\captionsetup{justification=centering, 
              hangindent=-0.2em,
              skip=0.333\baselineskip}
\caption{Some caption} \label{tab:somelabel} 
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
    l *{8}{c} }             
\toprule
& \multicolumn{2}{c}{Gesamtkosten} 
& \multicolumn{2}{c}{Länge eines Fahrzeugumlaufs} 
& \multicolumn{2}{c}{\# Elektrobusse} 
& \multicolumn{2}{c}{$H^V$} \\
\cmidrule{2-3} \cmidrule{4-5} \cmidrule{6-7} \cmidrule{8-9} 
    & (K) & (V) & (K) & (V) & (K) & (V) & (K) & (V)\\
    \midrule
    \multicolumn{9}{c}{\textbf{some description}} \\
    \midrule  
    I1 (90) & 44.490.046 & 37.214.853 (16\,\%) & 6.224  
    & 5.992 (4\,\%) & 49 & 15 (69\,\%) & 77 & 76 (1\,\%)\\
\bottomrule
\end{tabular*}
\end{sidewaystable}
\end{document}
1

The available width is equally distributed among the X columns, so tabular X could be a good idea when the natural width of these columns will be uneven. Then could be better the L,C,R or J columns of tabulary. This environment do not try to take all the allowed space, and when needed, the space it assigned to each column proportionally to the their longest entry.

However, this is not enough in this case as you fix via \multicolumns many columns widths, exceeding the \textwidth. The best solution in this case is allow a linebreak in the longest header using a m{} column (need the array package).

Another suggestion to narrow the whole table is use a scientific notation to avoid numbers of 7 digits (really needed?). With siunitx package this can be done automatically. But you can reduce the table about a 20% only allowing linebreaks between values and percentages.

Of course, much better using only horizontal booktabs rules as Mico suggest. The image below show the modifications suggested over this MWE:

\documentclass{article}
\usepackage{tabulary,array,booktabs,xcolor}
\usepackage[showframe]{geometry}
\makeatletter
\def\Gm@hrule{}\def\Gm@hruled{}
\def\Gm@vrule{\color{red!05}\vrule width 2pt height\textheight depth\z@}%
\makeatother
\usepackage{siunitx}
\sisetup{%
scientific-notation=false,
tight-spacing=true,
round-mode=places,
round-precision= 1}
\parindent0pt\parskip2em
\begin{document}

\verb|\begin{tabulary}{\textwidth}{*9C} ... \end{tabulary}|

\begin{tabulary}{\textwidth}{*9C}
\toprule & 
\multicolumn{2}{c}{Gesamtkosten} & 
\multicolumn{2}{c}{Länge eines Fahrzeugumlaufs} & 
\multicolumn{2}{c}{$\#$Elektrobusse} & 
\multicolumn{2}{c}{$H^V$} \\
\cmidrule(rl){2-3}\cmidrule(rl){4-5}\cmidrule(rl){6-7}\cmidrule(rl){8-9}
    & (K) & (V) &  (K) & (V) &  (K) & (V) & (K) & (V)\\
\cmidrule(rl){2-9}
\multicolumn{9}{c}{\textbf{some description}} \\
\cmidrule(rl){2-9}
I1 (90) &
\num{44490046} &
\num{37214853} (16\,\%) &
\num{6224} & 
\num{5992} (4\,\%) &
\num{49} & 
\num{15} (69\,\%) &
\num{77} & 
\num{76} (1\,\%)\\
\bottomrule
\end{tabulary}
\end{document}

mwe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.