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:
The documentation I read can be found here:
How can I fix my problems?
Best Answer
Here is one way of doing it:
p{<width>}
for all columns in a regulartabular
.\tabularlength
and set it to the total width of the tabular. I've used.975\linewidth
here.\tabularlength
e.g.p{.1\tabularlength}
.multicolumn
so that the lines can be broken.babel
.\-
.geometry
to get the page layout right.>{\bfseries}
for the first column to avoid having to specify bold on every line.\multicolumn
for the very first line so we don't get spurious vertical rules.\centering
rather than the environmentcenter
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 thetable
environment to set the fontsize a little smaller. Or you could adjust the margins etc. usinggeometry
. It is possible to do this for the whole document or, if necessary, a single page.The code
The output
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:
I used an additional formatting directive for the first column in this case,
\raggedright
, to make it look reasonable.Code for landscape tabular
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 thearray
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 ofarray
to define a new column type.Because my example shows the layout for both portrait and landscape, I also need to pull the addition of
\tabularlength
out of thetabular
environments so I'm not trying to define an already defined length.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
The Output