Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This might be a proper question for community wiki.

How can various text processor (like Word or Writer) table capabilities be achieved using LaTeX? For example, can I achieve:

Row and column spanning

  • equal column distribution (yes, with appropriate column types and manually inputting column widths, or tabularx package)
  • equal rows distribution (?)
  • changing text direction (yes, does not depend on table enviroment?)
  • cell/row shading (yes, with colorx package)
  • using borders and lines
  • setting borders of each cell individually (?)
  • aligning cell contents inside a cell in all 9 directions (vertical, horizontal and centering)
  • etc.
share|improve this question
4  
in agreement with your suggestion I converted it to community wiki. Using one separate comprehensive answer for elaborating each task together with an example may be a good idea. Within answers, it would be good to provide also internal links to good answers to questions about the specific task. –  Stefan Kottwitz Mar 8 '11 at 11:06
    
I want to recommend this page as well: en.wikibooks.org/wiki/LaTeX/Tables I go there very often to check up on how to do things related to tables... –  jonalv Nov 5 '13 at 9:52

8 Answers 8

Merge cells operation spanning multiple rows and columns, respecting borders.

Can be done using a \multirow (multirow package) inside a \multicolumn. The borders are drawn using \hline or \cline.

Code

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|}
                                                                            \hline
   Text A                        &  Text B                       & Test C \\\hline
   \multicolumn{2}{|c|}{\multirow{2}{*}{Longer Text, etc. etc.}} & Test F \\\cline{3-3}
   \multicolumn{2}{|c|}{}                                        & Test G \\\hline
\end{tabular}
\end{document}

Result

Result

share|improve this answer
    
Excellent! That is the kind of an answer I was hoping for. –  ipavlic Mar 9 '11 at 10:37

Changing text direction

This can be done using the \rotatebox macro of graphicx. The syntax is:

\rotatebox[origin=<O>]{<angle in degree>}{<Text>}

The origin on which the text is rotated can be:
l (left), r (right), c (center) and t (top), b (bottom), B (baseline).

For example rt is the right top corner. Default is lB.

Example Code:

\documentclass[convert={png,density=150}]{standalone}
\usepackage{graphicx}
\usepackage{array}
\usepackage{booktabs}

% Draw tight framebox around cell text
\newcommand*{\x}[1]{{%
    \fboxsep=0pt
    \fboxrule=.1pt
    \fbox{\strut#1}%
}}
\let\x\empty
\begin{document}
\begin{tabular}{llllll}
  \toprule
    \rlap{\rule{35em}{.1pt}}% Marker only
    Normal 
  &  \rotatebox{90}{90 up}origin
  &  \rotatebox{180}{Up side down, deep}
  &  \rotatebox{270}{270 down}
  &  \rotatebox{45}{45 degrees up}
  &  \rotatebox[origin=rB]{-45}{135 degrees up}
  \\\midrule
    \rlap{\rule{35em}{.1pt}}% Marker only
    Normal
  &  \rotatebox[origin=rB]{90}{90 down}
  &  \rotatebox[origin=c]{180}{Up side down, baseline}
  &  \rotatebox[origin=rB]{270}{270 up}
  &  \rotatebox{-45}{45 degrees down}
  &  \rotatebox[origin=rB]{45}{135 degrees down}
  \\\midrule
\end{tabular}
\end{document}

Result:

Result

share|improve this answer

While I can see that some features are easier to access in WYSIWYG DTP software than in LaTeX, some features are best avoided to achieve a professional table design. See the discussion on table formatting where I provide a before-and-after example that highlights the problem of a Word-like layout. The booktabs design is required reading for professional table layout in LaTeX.

Things that I wish would be easier in LaTeX is table design simply based on background colors but without relying on lines. Any recommendations?

share|improve this answer
    
I believe most users here appreciate beautiful documents, and that is one of the reasons we are using TeX or friends. "Professional table design" does not solve all problems or requirements, and the example gives only the most "natural" applications. –  ipavlic Mar 8 '11 at 13:43
    
@ipavlic I'm sorry if my answer was misleading. I didn't want to suggest that using the booktabs package will lead to the best result in all cases. I do like it because it comes with a design philosophy. Clearly there are good designs that cannot be achieved with it and that is why we need other packages as well. However, I still believe that some coherent package for a particular style is most useful to achieve a consistent design. Features available in Word et al. need some design experience, too, to put them to good use and they do not lead to good design automatically. –  Christian Lindig Mar 8 '11 at 13:57
    
I wholeheartedly agree. Some use cases however demand a more low-level approach. I am particularly interested in how to distribute rows evenly. –  ipavlic Mar 8 '11 at 14:10

Horizontal lines in tables

The booktabs package provides commands for

  • full-width lines: they can be done bye \toprule, \midrule and \bottomrule. All accept an optional argument for the thickness, which can be globally adjusted by modifying the lengths \heavyrulewidth (\toprule, \bottomrule) and \lightrulewidth (\midrule), respectively.

  • partial lines: they can be drawn by \cmidrule:

    \cmidrule[width](trim){m–n}

Here's an example for illustration, also taken from my blog, resp. book:

\documentclass{article}
\usepackage{booktabs}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}}
\begin{document}
\begin{tabular}{@{}*3l@{}}
  \toprule[1.5pt]
  \multicolumn{2}{c}{\head{Input}} &
    \multicolumn{1}{c}{\head{Output}}\\
  \head{Command} & \head{Declaration} & \\
  \cmidrule(r){1-2}\cmidrule(l){3-3}
  \verb|\textrm| & \verb|\rmfamily| & \rmfamily Example text\\
  \verb|\textsf| & \verb|\sffamily| & \sffamily Example text\\
  \verb|\texttt| & \verb|\ttfamily| & \ttfamily Example text\\
  \bottomrule[1.5pt]
\end{tabular}
\end{document}

enter image description here

share|improve this answer

Look at Tabu package documentation. It introduces some new capacities to tabulars.

share|improve this answer
    
Telling from the documentation, an interesting and powerful package! –  Christian Lindig Mar 8 '11 at 18:47

Alignment within table cells

Horizontal Alignment: use the column types l, c, r, and p to align left, centered, right, and full, respectively. An example, taken from my blog, resp. book:

\documentclass{article}
\begin{document}
\begin{tabular}{|l|c|r|p{1.7cm}|}
  \hline
  left & centered & right & a fully justified paragraph cell\\
  \hline
  l & c & r & p\\
  \hline
\end{tabular}
\end{document}

horizonal alignment

Vertical Alignment: use the array package and column types p, m, and b to align top, centered, and bottom, respectively. An example, taken from my blog, resp. book:

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{@{}lp{1.2cm}m{1.2cm}b{1.2cm}@{}}
  \hline
  baseline & aligned at the top& aligned at the middle
    & aligned at the bottom\\
  \hline
\end{tabular}
\end{document}

vertical alignment

share|improve this answer

Look at the Tabsatz documentation. It is in German, but the Latex code is very clear

share|improve this answer
    
I like the examples and principles demonstrated in that document. Maybe it is time for a community wiki that collects best practices for typesetting tables. –  Christian Lindig Mar 12 '11 at 20:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.