Tables
For some reason, the wonderful efficiency of LaTeX falls to pieces when confronted with any unusual table requirements. Some accumulated wisdom:
For some reason, the wonderful efficiency of LaTeX falls to pieces when confronted with any unusual table requirements. Some accumulated wisdom:
- If you want your columns to have a fixed width, then use the column specifier p{1cm}, for example. If you want the cell contents to be vertically aligned in the middle inside that cell use m{1cm} instead:
\begin{tabular}{|p{1cm}|m{1cm}|}
- If you want LaTeX to judge how wide the columns should be automatically, use the tabularx environment instead, and then X for the column specifier:
\begin{tabularx}{1.2\textwidth}{|X|X|}
The \textwidth parameter tells LaTeX how wide the table should be relative to the normal width of a page. If you don't like the default type of X column then you can change it:
\renewcommand{\tabularxcolumn}[1]{>{\arraybackslash}m{#1}}
This example would have the contents of cells vertically aligned in the middle. - If you want to apply some formatting to every cell in a column, specify this inside >{} in the column specification:
\begin{tabular}{|>{\centering}p{1cm}|m{1cm}|}
This will horizontally center the contents of the first column. - To fiddle around with the width between rows, try playing with
\renewcommand{\arraystretch}{1}
in the document preamble. - If you are struggling with gaps between columns in any way, usually it is @{} which comes to your rescue. This inputs some block and overrides a lot of the usual spacing; in particular, if you leave the brackets empty then you get a lot less space than the default. Put this in the column specifier.
\begin{tabularx}{1.2\textwidth}{|@{}X@{\hspace{1cm}}|X|}
- LaTeX also struggles with linebreaks inside the same cell. The easiest way seems to be to define a subtable inside that cell and then use that to do your linebreaking.
\begin{tabularx}{1.2\textwidth}{|@{}X@{\hspace{1cm}}|X|}
Example & \begin{tabular}[x]{@{}c@{}}\\Lunch\\Now?\end{tabular}\\
\end{tabularx}
- The tabularx environment does struggle with central horizontal alignment in its last cell, for some reason. The fix I found was to define a 'ghost' column on the end with no width.
\begin{tabularx}{1.4\textwidth}{|@{}>{\centering}X@{}|@{}>{\centering}X@{}|@{}p{-5cm}@{}}
- If your text isn't wrapping in the cells, use a different column specifier. Try using p{} or X instead of c.
- The makecell package has lots of useful little commands; see its documentation for details.
- It streamlines complicated tables considerably if you define a new column type in the preamble:
\newcolumntype{R}{@{\hspace{1em}}>{\centering}X@{}}
- The geometry package is very useful, especially now that you can change the margins mid-document with \newgeometry and then \restoregeometry back to the default.
\newgeometry{bottom=0.6cm, left=2cm, right=0.5cm, top=0.5cm}
...
\restoregeometry
Make sure you're using the newest version of the geometry package for this, from here. Also, if you're getting strange results, try changing the sequence of commands; for example, try
\newpage
\newgeometry{bottom=0.6cm, left=2cm, right=0.5cm, top=0.5cm}
instead of
\newgeometry{bottom=0.6cm, left=2cm, right=0.5cm, top=0.5cm}
\newpage
- Getting images and such where you want them can be tricky; don't forget that you can use \hspace and \vspace to create blocks of white space around it to push it where it should be.