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

 \captionsetup{justification=centering, hangindent=-0.2em}
 \caption{some caption}
             \multirow{2}{*}{} & \multicolumn{2}{|c|}{Gesamtkosten} & \multicolumn{2}{|c|}{Länge eines Fahrzeugumlaufs} & \multicolumn{2}{|c|}{$\#$Elektrobusse} & \multicolumn{2}{|c|}{$H^V$} \\
    & \vspace{1pt} (K) & \vspace{1pt} (V) & \vspace{1pt} (K) & \vspace{1pt} (V) & \vspace{1pt} (K) & \vspace{1pt} (V) & \vspace{1pt} (K) & \vspace{1pt} (V)\\
    \multicolumn{9}{|c|}{\textbf{some description}} \\
    \hspace{-9pt} I$1$~($90$)& $44.490.046$ & $37.214.853~(16~\%)$ & $6.224$ & $5.992~(4~\%)$ & $49$ & $15~(69~\%)$ & $77$ & $76~(1~\%)$\\

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

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


\caption{Some caption} \label{tab:somelabel} 
    l *{8}{c} }             
& \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)\\
    \multicolumn{9}{c}{\textbf{some description}} \\
    I1 (90) & 44.490.046 & 37.214.853 (16\,\%) & 6.224  
    & 5.992 (4\,\%) & 49 & 15 (69\,\%) & 77 & 76 (1\,\%)\\
| improve this answer | |

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:

\def\Gm@vrule{\color{red!05}\vrule width 2pt height\textheight depth\z@}%
round-precision= 1}

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

\toprule & 
\multicolumn{2}{c}{Gesamtkosten} & 
\multicolumn{2}{c}{Länge eines Fahrzeugumlaufs} & 
\multicolumn{2}{c}{$\#$Elektrobusse} & 
\multicolumn{2}{c}{$H^V$} \\
    & (K) & (V) &  (K) & (V) &  (K) & (V) & (K) & (V)\\
\multicolumn{9}{c}{\textbf{some description}} \\
I1 (90) &
\num{44490046} &
\num{37214853} (16\,\%) &
\num{6224} & 
\num{5992} (4\,\%) &
\num{49} & 
\num{15} (69\,\%) &
\num{77} & 
\num{76} (1\,\%)\\


| improve this answer | |

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.