[Tex/LaTex] How to have columns of different sizes using tabularx and make table respect page height

columnskiletablestabularx

I am using Kile and I am trying to learn how to use tabularx to have tables that fit within the width of my pages.

However, I have two big problems:

  • I have to use X for column size and therefore cannot set the size of my columns (for example, if I have 4 columns, I want column A to be 10% of the size, and columns B, C and D to have the rest equally distributed).
  • Now that my table finally fits within the width of the page, it does not respect the height of the page …

Now to fix this, I read the documentation and used the following MWE:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\begin{document}
\begin{table}
{%
\newcommand{\mc}[3]{\multicolumn{#1}{#2}{#3}}
\begin{center}
\begin{tabularx}{\textwidth}{>{\hsize=.25\hsize}X>{\hsize=1.25\hsize}X>{\hsize=1.25\hsize}X>{\hsize=1.25\hsize}X}\cline{2-4}
\textbf{} & \textbf{Hibernate OGM} & \textbf{EclipseLink NoSQL} & \textbf{DataNucleus}\\\hline
\mc{1}{|l|}{\textbf{Goal}} & Complement JPA with NoSQL, key-value stores & Integrates in the father project main goal of providing a complete persistence solution & Being a standards compliant and efficient JPA and JDO platform\\\hline
\mc{1}{|l|}{\textbf{NoSQL and Datastores supported}} & Infinispan, EHCache, MongoDB & MongoDB, Oracle NoSQL, Oracle AQ, JMS, XML files & Google Big Table, MongoDB, Cassandra, Excel, OOXML, ODF, XML, HBase, AppEngine/DataStore, Neo4j, JSON, Amazon S3, GoogleStorage, LDAP, NeoDatis, db4o\\\hline
\mc{1}{|l|}{\textbf{Operations supported}} & Object Oriented queries (JP-QL), CRUD of entities, Polymorphic entities, Embeddable objects, Basic types (partial), Unidirectional and Bidirectional relationships (partial), Collections, Hibernate Search queries, JPA and Hibernate ORM API & Object Oriented Queries, Polymorphic entities, Basic types, Unidirectional relationships, Collections, JPA (partial), Complex hierarchical, Indexed hierarchical data, Mapped hierarchical data, CRUD operations, Embedded objects and collections, Inheritance, Subset of JP-QL and Criteria API, Denormalization & CRUD operations, Embedded objects and collections, Inheritance, Relationships (Unidirectional and Bidirectional), Queries for JP-QL, JDOQL and SQL (partial),  Basic types, Joins.\\\hline
\mc{1}{|l|}{\textbf{No support for}} & Denormalization, Complex joins and aggregations & Joins & Aggregations? (not specified in documentation)\\\hline
\mc{1}{|l|}{\textbf{Future}} & High performance sequence generator, parallel key fetching, support for Map/Reduce, more NoSQL classes, better mixing of NoSQL and RDBMS & ? & JPA2.1 full feature list, Official support for Cassandra, Considering a plugin for REDIS\\\hline
\mc{1}{|l|}{\textbf{Commercial support}} & Red Hat & Oracle (via TopLink) & Supported by DataNucleus team\\\hline
\mc{1}{|l|}{\textbf{Documentation}} & Scattered, inactive forums, official documentation lacking & Bureaucratic forums, information is complete and gathered mainly in the official website & Active forums, acceptable official documentation, but the big advantage comes from user support in form of blogs and posts scattered around the Internet\\\hline
\end{tabularx}
\end{center}
}%
\end{table}


\end{document}

However, even though the preamble seems to be correct (for a case with 4 columns, where columns B, C and D should be 6x greater than A) nothing happens.

What do I exactly want?
Lets say the total width of the table is 100%. I would like for column A to have 10% of that width, and columns B, C and D to have 30% each one.

I would also like to make this table fit in one page, without extended to extra pages. So no, i cannot use longtable …

The MWE currently generates something like this:

enter image description here

The documentation I read can be found here:

How can I fix my problems?

Best Answer

Here is one way of doing it:

  • Use p{<width>} for all columns in a regular tabular.
  • To do this, create a new length \tabularlength and set it to the total width of the tabular. I've used .975\linewidth here.
  • Then define each of the columns in terms of \tabularlength e.g. p{.1\tabularlength}.
  • To get the first column to behave, dispense with multicolumn so that the lines can be broken.
  • This means we want hyphenation so load babel.
  • This isn't quite enough as this doesn't allow hyphenation of the first word in the column. So hyphenate these words by hand using \-.
  • Use geometry to get the page layout right.
  • Use >{\bfseries} for the first column to avoid having to specify bold on every line.
  • We still need one \multicolumn for the very first line so we don't get spurious vertical rules.
  • Use \centering rather than the environment center since the latter adds additional spacing which we don't want here.

This actually fits with (a little) room to spare. If the table were bigger, you could consider using \small within the table environment to set the fontsize a little smaller. Or you could adjust the margins etc. using geometry. It is possible to do this for the whole document or, if necessary, a single page.

The code

\documentclass[a4paper,10pt,british]{article}
\usepackage{babel,geometry}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\begin{document}

\begin{table}
  \newlength{\tabularlength}
  \setlength{\tabularlength}{.975\linewidth}
  \centering
  \begin{tabular}{|>{\bfseries}p{.1\tabularlength}|*{3}{p{.3\tabularlength}}}
  \cline{2-4}
  \multicolumn{1}{l}{} & \textbf{Hibernate OGM} & \textbf{EclipseLink NoSQL} & \textbf{DataNucleus}\\\hline
  Goal & Complement JPA with NoSQL, key-value stores & Integrates in the father project main goal of providing a complete persistence solution & Being a standards compliant and efficient JPA and JDO platform\\\hline
  NoSQL and Datastores supported & Infinispan, EHCache, MongoDB & MongoDB, Oracle NoSQL, Oracle AQ, JMS, XML files & Google Big Table, MongoDB, Cassandra, Excel, OOXML, ODF, XML, HBase, AppEngine/DataStore, Neo4j, JSON, Amazon S3, GoogleStorage, LDAP, NeoDatis, db4o\\\hline
  Oper\-ations supported & Object Oriented queries (JP-QL), CRUD of entities, Polymorphic entities, Embeddable objects, Basic types (partial), Unidirectional and Bidirectional relationships (partial), Collections, Hibernate Search queries, JPA and Hibernate ORM API & Object Oriented Queries, Polymorphic entities, Basic types, Unidirectional relationships, Collections, JPA (partial), Complex hierarchical, Indexed hierarchical data, Mapped hierarchical data, CRUD operations, Embedded objects and collections, Inheritance, Subset of JP-QL and Criteria API, Denormalization & CRUD operations, Embedded objects and collections, Inheritance, Relationships (Unidirectional and Bidirectional), Queries for JP-QL, JDOQL and SQL (partial),  Basic types, Joins.\\\hline
  No support for & Denormalization, Complex joins and aggregations & Joins & Aggregations? (not specified in documentation)\\\hline
  Future & High performance sequence generator, parallel key fetching, support for Map/Reduce, more NoSQL classes, better mixing of NoSQL and RDBMS & ? & JPA2.1 full feature list, Official support for Cassandra, Considering a plugin for REDIS\\\hline
  Com\-mer\-cial support & Red Hat & Oracle (via TopLink) & Supported by DataNucleus team\\\hline
  Docu\-ment\-ation & Scattered, inactive forums, official documentation lacking & Bureaucratic forums, information is complete and gathered mainly in the official website & Active forums, acceptable official documentation, but the big advantage comes from user support in form of blogs and posts scattered around the Internet\\\hline
  \end{tabular}
\end{table}

\end{document}

The output

Tabular fitted to page

An alternative layout

I think you might also want to consider including the tabular in landscape orientation as this might make it easier for people to digest. For example:

Tabular in landscape

I used an additional formatting directive for the first column in this case, \raggedright, to make it look reasonable.

Code for landscape tabular

\documentclass[a4paper,10pt,british]{article}
\usepackage{babel,geometry}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{pdflscape}

\begin{document}

\begin{landscape}

\begin{table}
  \newlength{\tabularlength}
  \setlength{\tabularlength}{.975\linewidth}
  \centering
  \begin{tabular}{|>{\bfseries\raggedright}p{.1\tabularlength}|*{3}{p{.3\tabularlength}}}
    \cline{2-4}
    \multicolumn{1}{l}{} & \textbf{Hibernate OGM} & \textbf{EclipseLink NoSQL} & \textbf{DataNucleus}\\\hline
    Goal & Complement JPA with NoSQL, key-value stores & Integrates in the father project main goal of providing a complete persistence solution & Being a standards compliant and efficient JPA and JDO platform\\\hline
    NoSQL and Datastores supported & Infinispan, EHCache, MongoDB & MongoDB, Oracle NoSQL, Oracle AQ, JMS, XML files & Google Big Table, MongoDB, Cassandra, Excel, OOXML, ODF, XML, HBase, AppEngine/DataStore, Neo4j, JSON, Amazon S3, GoogleStorage, LDAP, NeoDatis, db4o\\\hline
    Oper\-ations supported & Object Oriented queries (JP-QL), CRUD of entities, Polymorphic entities, Embeddable objects, Basic types (partial), Unidirectional and Bidirectional relationships (partial), Collections, Hibernate Search queries, JPA and Hibernate ORM API & Object Oriented Queries, Polymorphic entities, Basic types, Unidirectional relationships, Collections, JPA (partial), Complex hierarchical, Indexed hierarchical data, Mapped hierarchical data, CRUD operations, Embedded objects and collections, Inheritance, Subset of JP-QL and Criteria API, Denormalization & CRUD operations, Embedded objects and collections, Inheritance, Relationships (Unidirectional and Bidirectional), Queries for JP-QL, JDOQL and SQL (partial),  Basic types, Joins.\\\hline
    No support for & Denormalization, Complex joins and aggregations & Joins & Aggregations? (not specified in documentation)\\\hline
    Future & High performance sequence generator, parallel key fetching, support for Map/Reduce, more NoSQL classes, better mixing of NoSQL and RDBMS & ? & JPA2.1 full feature list, Official support for Cassandra, Considering a plugin for REDIS\\\hline
    Com\-mer\-cial support & Red Hat & Oracle (via TopLink) & Supported by DataNucleus team\\\hline
    Docu\-ment\-ation & Scattered, inactive forums, official documentation lacking & Bureaucratic forums, information is complete and gathered mainly in the official website & Active forums, acceptable official documentation, but the big advantage comes from user support in form of blogs and posts scattered around the Internet\\\hline
  \end{tabular}
\end{table}

\end{landscape}

\end{document}

Centred columns & vertical rules

As noted by Flame_Phoenix in the comments, m{<width>} gives a column of specified width which is centred vertically. This specifier requires the array package.

Adding a vertical rule before 'Hibernate OGM' is just a question of adjusting the \multicolumn for the first entry of the tabular. Centring all cells horizontally, as well as vertically, can be done by adding >{\centering} in the tabular configuration. However, this gets a bit tedious and is harder to adjust consistently. A better solution uses the ability of array to define a new column type.

\newcolumntype{M}{>{\centering}m{.3\tabularlength}}

Because my example shows the layout for both portrait and landscape, I also need to pull the addition of \tabularlength out of the tabular environments so I'm not trying to define an already defined length.

\newlength{\tabularlength}

The m column type treats \\ as a new line within the column rather than the end of the row. I therefore need to replace \\\hline with \tabularnewline\hline.

Finally, I've added a vertical rule on the far right for neatness.

The code

\documentclass[a4paper,10pt,british]{article}
\usepackage{babel,geometry}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{pdflscape}
\newlength{\tabularlength}
\newcolumntype{M}{>{\centering}m{.3\tabularlength}}

\begin{document}

\begin{table}
  \setlength{\tabularlength}{.975\linewidth}
  \centering
  \begin{tabular}{|>{\centering\bfseries}m{.1\tabularlength}|*{3}{M}|}
  \cline{2-4}
  \multicolumn{1}{l|}{} & \textbf{Hibernate OGM} & \textbf{EclipseLink NoSQL} & \textbf{DataNucleus}\tabularnewline\hline
  Goal & Complement JPA with NoSQL, key-value stores & Integrates in the father project main goal of providing a complete persistence solution & Being a standards compliant and efficient JPA and JDO platform\tabularnewline\hline
  NoSQL and Datastores supported & Infinispan, EHCache, MongoDB & MongoDB, Oracle NoSQL, Oracle AQ, JMS, XML files & Google Big Table, MongoDB, Cassandra, Excel, OOXML, ODF, XML, HBase, AppEngine/DataStore, Neo4j, JSON, Amazon S3, GoogleStorage, LDAP, NeoDatis, db4o\tabularnewline\hline
  Oper\-ations supported & Object Oriented queries (JP-QL), CRUD of entities, Polymorphic entities, Embeddable objects, Basic types (partial), Unidirectional and Bidirectional relationships (partial), Collections, Hibernate Search queries, JPA and Hibernate ORM API & Object Oriented Queries, Polymorphic entities, Basic types, Unidirectional relationships, Collections, JPA (partial), Complex hierarchical, Indexed hierarchical data, Mapped hierarchical data, CRUD operations, Embedded objects and collections, Inheritance, Subset of JP-QL and Criteria API, Denormalization & CRUD operations, Embedded objects and collections, Inheritance, Relationships (Unidirectional and Bidirectional), Queries for JP-QL, JDOQL and SQL (partial),  Basic types, Joins.\tabularnewline\hline
  No support for & Denormalization, Complex joins and aggregations & Joins & Aggregations? (not specified in documentation)\tabularnewline\hline
  Future & High performance sequence generator, parallel key fetching, support for Map/Reduce, more NoSQL classes, better mixing of NoSQL and RDBMS & ? & JPA2.1 full feature list, Official support for Cassandra, Considering a plugin for REDIS\tabularnewline\hline
  Com\-mer\-cial support & Red Hat & Oracle (via TopLink) & Supported by DataNucleus team\tabularnewline\hline
  Docu\-ment\-ation & Scattered, inactive forums, official documentation lacking & Bureaucratic forums, information is complete and gathered mainly in the official website & Active forums, acceptable official documentation, but the big advantage comes from user support in form of blogs and posts scattered around the Internet\tabularnewline\hline
  \end{tabular}
\end{table}

\begin{landscape}

\begin{table}
  \setlength{\tabularlength}{.975\linewidth}
  \centering
  \begin{tabular}{|>{\bfseries\centering}m{.1\tabularlength}|*{3}{M}|}
    \cline{2-4}
    \multicolumn{1}{l|}{} & \textbf{Hibernate OGM} & \textbf{EclipseLink NoSQL} & \textbf{DataNucleus}\tabularnewline\hline
    Goal & Complement JPA with NoSQL, key-value stores & Integrates in the father project main goal of providing a complete persistence solution & Being a standards compliant and efficient JPA and JDO platform\tabularnewline\hline
    NoSQL and Datastores supported & Infinispan, EHCache, MongoDB & MongoDB, Oracle NoSQL, Oracle AQ, JMS, XML files & Google Big Table, MongoDB, Cassandra, Excel, OOXML, ODF, XML, HBase, AppEngine/DataStore, Neo4j, JSON, Amazon S3, GoogleStorage, LDAP, NeoDatis, db4o\tabularnewline\hline
    Operations supported & Object Oriented queries (JP-QL), CRUD of entities, Polymorphic entities, Embeddable objects, Basic types (partial), Unidirectional and Bidirectional relationships (partial), Collections, Hibernate Search queries, JPA and Hibernate ORM API & Object Oriented Queries, Polymorphic entities, Basic types, Unidirectional relationships, Collections, JPA (partial), Complex hierarchical, Indexed hierarchical data, Mapped hierarchical data, CRUD operations, Embedded objects and collections, Inheritance, Subset of JP-QL and Criteria API, Denormalization & CRUD operations, Embedded objects and collections, Inheritance, Relationships (Unidirectional and Bidirectional), Queries for JP-QL, JDOQL and SQL (partial),  Basic types, Joins.\tabularnewline\hline
    No support for & Denormalization, Complex joins and aggregations & Joins & Aggregations? (not specified in documentation)\tabularnewline\hline
    Future & High performance sequence generator, parallel key fetching, support for Map/Reduce, more NoSQL classes, better mixing of NoSQL and RDBMS & ? & JPA2.1 full feature list, Official support for Cassandra, Considering a plugin for REDIS\tabularnewline\hline
    Commercial support & Red Hat & Oracle (via TopLink) & Supported by DataNucleus team\tabularnewline\hline
    Document\-ation & Scattered, inactive forums, official documentation lacking & Bureaucratic forums, information is complete and gathered mainly in the official website & Active forums, acceptable official documentation, but the big advantage comes from user support in form of blogs and posts scattered around the Internet\tabularnewline\hline
  \end{tabular}
\end{table}

\end{landscape}

\end{document}

The Output

All centred tabular

Related Question