2

I have a simple table in LibreOffice calc and I tried to convert it to latex using the Calc2Latex macro.

Then using Latex I tried to improve it. However I cannot get the text correctly !

This is what I want the text to be like in the table: enter image description here

For example, in the first column, "Message per minute per source" is divided.

However, in my latex generated table, that does not happen! It is all a big mess: enter image description here

I tried fixing the problem using tabularx and I read somewhere that I should not use multicolumn, but I am still a newbie and I do not know how to fix this!

Here is my MWE:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{microtype}
\usepackage[margin=2.54cm]{geometry}
\usepackage{graphicx,booktabs,tabularx}
\usepackage[table]{xcolor}
\usepackage{array}

%define some nice colors
\definecolor{middleware_green}{RGB}{82, 119, 17}
\definecolor{middleware_white}{RGB}{255, 255, 255}
\definecolor{middleware_light_gray}{RGB}{239, 239, 239}
\definecolor{middleware_dark_gray}{RGB}{153, 153, 153}

\renewcommand{\tabularxcolumn}[1]{>{\small}m{#1}}
\newcolumntype{F}{|>{\leavevmode\ignorespaces\raggedright\sffamily\bfseries}p{2.5cm}}%
\newcolumntype{R}{|>{\leavevmode\ignorespaces\raggedright\arraybackslash\sffamily}X}%
\newcolumntype{L}{|>{\leavevmode\ignorespaces\raggedleft\arraybackslash\sffamily}X}%
\newcolumntype{C}{|>{\leavevmode\ignorespaces\centering\arraybackslash\sffamily}X|}%
\rowcolors{2}{middleware_white}{middleware_light_gray}

\begin{document}

\begin{table}[htbp]

\begin{tabularx}{\textwidth}{|R|L|L|L|}
\hline\rowcolor{middleware_green}
\multicolumn{ 4}{|c|}{\textcolor{white}{\textbf{1 Source, 1 Session, 1 Client, Exp0}}} \\ \hline
\multicolumn{1}{|c|}{\textbf{Messages per minute per source}} & \multicolumn{1}{c|}{\textbf{Middleware Exec Time (s)}} & \multicolumn{1}{c|}{\textbf{Delay (s)}} & \multicolumn{1}{c|}{\textbf{Average Delay (s)}} \\ \hline
\multicolumn{ 1}{|r|}{60} & 60 & 0 & \multicolumn{ 1}{r|}{0.00} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 60 & 0 & \multicolumn{ 1}{l|}{} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 60 & 0 & \multicolumn{ 1}{l|}{} \\ \hline
\multicolumn{ 1}{|r|}{120} & 60 & 0 & \multicolumn{ 1}{r|}{0.00} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 60 & 0 & \multicolumn{ 1}{l|}{} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 60 & 0 & \multicolumn{ 1}{l|}{} \\ \hline
\multicolumn{ 1}{|r|}{240} & 60 & 0 & \multicolumn{ 1}{r|}{0.00} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 60 & 0 & \multicolumn{ 1}{l|}{} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 60 & 0 & \multicolumn{ 1}{l|}{} \\ \hline
\multicolumn{ 1}{|r|}{480} & 69 & 9 & \multicolumn{ 1}{r|}{16.67} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 69 & 9 & \multicolumn{ 1}{l|}{} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 92 & 32 & \multicolumn{ 1}{l|}{} \\ \hline
\multicolumn{ 1}{|r|}{1000} & 159 & 99 & \multicolumn{ 1}{r|}{109.67} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 173 & 113 & \multicolumn{ 1}{l|}{} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 177 & 117 & \multicolumn{ 1}{l|}{} \\ \hline
\multicolumn{ 1}{|r|}{2000} & 391 & 331 & \multicolumn{ 1}{r|}{311.00} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 383 & 323 & \multicolumn{ 1}{l|}{} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 339 & 279 & \multicolumn{ 1}{l|}{} \\ \hline
\multicolumn{ 1}{|r|}{4000} & 561 & 501 & \multicolumn{ 1}{r|}{512.33} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 631 & 501 & \multicolumn{ 1}{l|}{} \\ \cline{ 2- 3}
\multicolumn{ 1}{|l|}{} & 595 & 535 & \multicolumn{ 1}{l|}{} \\ \hline
\end{tabularx}

\label{}
\caption{}
\end{table}


\end{document}

The main reason i want to make the columns smaller is so they can take less space and so hopefully I can put some text next to them or put 2 tables one next to another !

1
  • Yes, try removing all the \multicolumn stuff. E.g, if you have \multicolumn{1}{l}{X}, replace this with X. If you have \multicolumn{1}{c}{<Stuff>} in a cell, then that will override the column you specified after \begin{tabular}, so it doesn't matter if you have an X column, as using \multicolumn like that disallows the line breaking in that cell. (Don't do it for the first row, of course.) Mar 17, 2014 at 23:11

2 Answers 2

2

An X column allows it's contents to break, but when you do \multicolumn{1}{c}{Lots of text} you override that column specifier. That will cell will behave as a c column, which does not allow for line breaking.

To have a the column heading centered, while still allowing for line breaks, and have the rest of the column right aligned, use a \multicolumn with your C column, e.g. \multicolumn{1}{C}{\textbf{Messages per minute per source}}.

The rest of the \multicolumn{1}{.. stuff I think you can remove. In the complete code below, there is one table with vertical rules, one without. I think you can use the one without, but I'll leave it for you to decide.

These tables are set at 0.45\textwidth, so they could be a little bit wider and there would still be room for two next to each other. If you don't need that, they can of course be much wider.

enter image description here

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{microtype}
\usepackage[margin=2.54cm]{geometry}
\usepackage{graphicx,booktabs,tabularx}
\usepackage[table]{xcolor}
\usepackage{array}

%define some nice colors
\definecolor{middleware_green}{RGB}{82, 119, 17}
\definecolor{middleware_white}{RGB}{255, 255, 255}
\definecolor{middleware_light_gray}{RGB}{239, 239, 239}
\definecolor{middleware_dark_gray}{RGB}{153, 153, 153}

\renewcommand{\tabularxcolumn}[1]{>{\small}m{#1}}
\newcolumntype{F}{>{\leavevmode\ignorespaces\raggedright\sffamily\bfseries}p{2.5cm}}%
\newcolumntype{R}{>{\leavevmode\ignorespaces\raggedright\arraybackslash\sffamily}X}%
\newcolumntype{L}{>{\leavevmode\ignorespaces\raggedleft\arraybackslash\sffamily}X}%
\newcolumntype{C}{>{\leavevmode\ignorespaces\centering\arraybackslash\sffamily}X}%
\rowcolors{2}{middleware_white}{middleware_light_gray}

\begin{document}

\begin{table}
\centering
\begin{tabularx}{.45\textwidth}{LLLL}
\hline\rowcolor{middleware_green}
\multicolumn{4}{|c|}{\textcolor{white}{\textbf{1 Source, 1 Session, 1 Client, Exp0}}} \\ \hline
\multicolumn{1}{C}{\textbf{Messages per minute per source}} &
\multicolumn{1}{C}{\textbf{Middle\-ware Exec Time (s)}} &
\multicolumn{1}{C}{\textbf{Delay (s)}} &
\multicolumn{1}{C}{\textbf{Average Delay (s)}} \\ \hline
60 & 60 & 0 & 0.00 \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \hline
120 & 60 & 0 & 0.00 \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \hline
240 & 60 & 0 & 0.00 \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \hline
480 & 69 & 9 & 16.67 \\ \cline{ 2- 3}
 & 69 & 9 &  \\ \cline{ 2- 3}
 & 92 & 32 &  \\ \hline
1000 & 159 & 99 & 109.67 \\ \cline{ 2- 3}
 & 173 & 113 &  \\ \cline{ 2- 3}
 & 177 & 117 &  \\ \hline
2000 & 391 & 331 & 311.00 \\ \cline{ 2- 3}
 & 383 & 323 &  \\ \cline{ 2- 3}
 & 339 & 279 &  \\ \hline
4000 & 561 & 501 & 512.33 \\ \cline{ 2- 3}
 & 631 & 501 &  \\ \cline{ 2- 3}
 & 595 & 535 &  \\ \hline
\end{tabularx}
\begin{tabularx}{.45\textwidth}{|L|L|L|L|}
\hline\rowcolor{middleware_green}
\multicolumn{4}{|c|}{\textcolor{white}{\textbf{1 Source, 1 Session, 1 Client, Exp0}}} \\ \hline
\multicolumn{1}{|C|}{\textbf{Messages per minute per source}} &
\multicolumn{1}{C|}{\textbf{Middle\-ware Exec Time (s)}} &
\multicolumn{1}{C|}{\textbf{Delay (s)}} &
\multicolumn{1}{C|}{\textbf{Average Delay (s)}} \\ \hline
60 & 60 & 0 & 0.00 \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \hline
120 & 60 & 0 & 0.00 \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \hline
240 & 60 & 0 & 0.00 \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \cline{ 2- 3}
 & 60 & 0 &  \\ \hline
480 & 69 & 9 & 16.67 \\ \cline{ 2- 3}
 & 69 & 9 &  \\ \cline{ 2- 3}
 & 92 & 32 &  \\ \hline
1000 & 159 & 99 & 109.67 \\ \cline{ 2- 3}
 & 173 & 113 &  \\ \cline{ 2- 3}
 & 177 & 117 &  \\ \hline
2000 & 391 & 331 & 311.00 \\ \cline{ 2- 3}
 & 383 & 323 &  \\ \cline{ 2- 3}
 & 339 & 279 &  \\ \hline
4000 & 561 & 501 & 512.33 \\ \cline{ 2- 3}
 & 631 & 501 &  \\ \cline{ 2- 3}
 & 595 & 535 &  \\ \hline
\end{tabularx}
\caption{Look, two tables!}
\end{table}
\end{document}
1
  • Thanks! Now I have (yet) another problem (I cannot make my tables show one next to another) but I will create a new question for that! Mar 18, 2014 at 0:35
1

I propose this solution, based on makecell though it's far from perfect. However, I think it's interesting because the code is much more compact.

It has problems with colours: the first two rows are fine, but the following rows are not entirely coloured, for reasons that I don't understand since the code is not fundamentally different from the code for the second row (I tested replacing the \multirow commands in the 4th row with \makecell, without any change in the resulting table).

So if anyone could explain what is the problem…

    \documentclass[a4paper,10pt]{article}
    \usepackage[utf8]{inputenc}
    \usepackage{microtype}
    \usepackage[margin=2.54cm]{geometry}
    \usepackage{graphicx,tabularx}%booktabs,
    \usepackage{array}

    %define some nice colors
    \usepackage[font = sf]{caption}
    \usepackage{makecell}
    \renewcommand{\theadfont}{\bfseries}
    \renewcommand{\cellalign}{rc}

    \usepackage[table, svgnames]{xcolor}
    \definecolor{middleware_green}{RGB}{82, 119, 17}
    \definecolor{middleware_white}{RGB}{255, 255, 255}
    \definecolor{middleware_light_gray}{RGB}{239, 239, 239}
    \definecolor{middleware_dark_gray}{RGB}{153, 153, 153}
    \usepackage{colortbl}
    \rowcolors{2}{White}{Olive!15!}
    \begin{document}

    \begin{table}[htbp]
    \sffamily\centering
    \label{}
    \caption{}
    \begin{tabular}{|r|r|r|r|}%
    \hline\rowcolor{middleware_green}
    \multicolumn{ 4}{|c|}{\Gape[8pt]{\textcolor{white}{\textbf{1 Source, 1 Session, 1 Client, Exp0}}}} \\ %
    \hline%
    \makecell*{Messages\\ per minute\\ per source} & \makecell{Middleware Exec\\ Time (s)} & \makecell{Delay (s)}& \makecell{Average\\ Delay (s)} \\
    \hline
    \multirowcell{3}{360} & \makecell{60\\ 60\\60} & \makecell{0\\0\\0} & \multirowcell{3}{0.00} \\
     \hline\rowcolor{Olive!20!}%%
     \multirowcell{3}{120} & \makecell*{60\\60\\60} & \makecell{0\\0\\0} & \multirowcell{3}{0.00} \\%
    \hline
    \multirowcell{3}{240} & \makecell*{60\\60\\60} & \makecell{0\\0\\0} & \multirowcell{3}{0.00} \\
    \hline
    \multirowcell{3}{480} & \makecell*{69\\69\\92} & \makecell{9\\9\\32}& \multirowcell{3}{16.67} \\
    \hline
    \multirowcell{3}{1000} & \makecell*{159\\173\\177} & \makecell{99\\113\\117} & \multirowcell{3}{109.67} \\
    \hline
    \multirowcell{3}{2000} & \makecell*{391\\383\\339} & \makecell{331\\323\\279} & \multirowcell{3}{311.00} \\
    \hline
    \multirowcell{3}{4000} & \makecell*{561\\631\\595} & \makecell{501\\501\\535} & \multirowcell{3}{512.33} \\
    \hline
    \end{tabular}
    \end{table}

    \end{document} 

enter image description here

1
  • Already fixed it, but thx for trying! Mar 18, 2014 at 11:31

You must log in to answer this question.

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