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.

I have a table produced by the following code:

\documentclass[a4paper]{article}
\usepackage{tabularx}
\usepackage{rotating}

\begin{document}

\begin{table}[ht]

\begin{center}
\begin{tabularx}{\textwidth}{|l|X|l|l|l|l|l|l|}
\hline
Item    &   Task    & 
\multicolumn{1}{|c|}{
\begin{sideways}
Resident Engineer \,
\end{sideways}} &
\multicolumn{1}{c|}{
\begin{sideways}
Pavement Engineer \,
\end{sideways}} &

\multicolumn{1}{c|}{
\begin{sideways}
Surveyor \,
\end{sideways}} &
\multicolumn{1}{c|}{
\begin{sideways}
Environmental Specialist \,
\end{sideways}} &
\multicolumn{1}{c|}{
\begin{sideways}
Materials Technicians \,
\end{sideways}} &
\multicolumn{1}{c|}{
\begin{sideways}
Inspectors of Works \,
\end{sideways}} \\
\hline
Task 1  &   Mobilization and Project Inception & P  & P & P &  P    & P & P     \\
\hline
Task 2  &   Carry out data collection \& review of existing data  & P& S    & P &P  &P  & P\\
\hline
Task 3  &   Compiling and submitting the Inception report  &P   &   S&  &   &P  &       \\
\hline

\end{tabularx}
\end{center}

\end{table}


\end{document}

The problem is that Item and Task cells are not centered. How can I center the text in these two cells (vertical and horizontal alignment)?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Since you know the contents of the "longest" entries, you can use this to your advantage and raise the contents of the first two heading entries accordingly.

enter image description here

\documentclass[a4paper]{article}
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\usepackage{graphicx}% http://ctan.org/pkg/graphicx

\begin{document}

\begin{table}[ht]
  \newlength{\tabheadheight}
  \settoheight{\tabheadheight}{\rotatebox{90}{Environmental Specialist\,}}
  \begin{tabularx}{\textwidth}{|c|X|l|l|l|l|l|l|}
    \hline
    \raisebox{\dimexpr.5\tabheadheight-.5\normalbaselineskip}{Item} &
    \centering\raisebox{\dimexpr.5\tabheadheight-.5\normalbaselineskip}{Task} & 
    \rotatebox{90}{Resident Engineer} &
    \rotatebox{90}{Pavement Engineer} &
    \rotatebox{90}{Surveyor} &
    \rotatebox{90}{Environmental Specialist\,} &
    \rotatebox{90}{Materials Technicians} &
    \rotatebox{90}{Inspectors of Works} \\
    \hline
    Task 1  &   Mobilization and Project Inception & P  & P & P &  P    & P & P     \\
    \hline
    Task 2  &   Carry out data collection \& review of existing data  & P& S    & P &P  &P  & P\\
    \hline
    Task 3  &   Compiling and submitting the Inception report  &P   &   S&  &   &P  &       \\
    \hline
  \end{tabularx}
\end{table}

The use of graphicx is sufficient to obtain rotated headings via \rotatebox{90}{<stuff>}. Also, using the center environment to centre a tabularx of width \textwidth is not necessary, since it fits to the text block width.

In the above example, the length \tabheadheight is used to capture the height of the "longest" entry. Subsequently, the first two headings are raised by .5\tabheadheight-.5\normalbaselineskip, setting them vertically centred.

share|improve this answer
    
Thanks a lot @Werner for the elegant solution and detailed explanation of how it works. I especially like the use of the graphicx package to obtain rotated headings, it is less cumbersome than what I used. –  engineervix Mar 25 '12 at 22:07

enter image description here

You can override the column alignment to center, and then raise the text by eye (or you could measure the longest heading)

\documentclass[a4paper]{article}
\usepackage{tabularx}
\usepackage{rotating}

\begin{document}

\begin{table}[ht]

\begin{center}
\begin{tabularx}{\textwidth}{|l|X|l|l|l|l|l|l|}
\hline
\multicolumn{1}{|c|}{\raisebox{4em}{Item}}  &
\multicolumn{1}{c|}{\raisebox{4em}{Task}}   & 
\multicolumn{1}{|c|}{
\begin{sideways}
Resident Engineer \,
\end{sideways}} &
\multicolumn{1}{c|}{
\begin{sideways}
Pavement Engineer \,
\end{sideways}} &

\multicolumn{1}{c|}{
\begin{sideways}
Surveyor \,
\end{sideways}} &
\multicolumn{1}{c|}{
\begin{sideways}
Environmental Specialist \,
\end{sideways}} &
\multicolumn{1}{c|}{
\begin{sideways}
Materials Technicians \,
\end{sideways}} &
\multicolumn{1}{c|}{
\begin{sideways}
Inspectors of Works \,
\end{sideways}} \\
\hline
Task 1  &   Mobilization and Project Inception & P  & P & P &  P    & P & P     \\
\hline
Task 2  &   Carry out data collection \& review of existing data  & P& S    & P &P  &P  & P\\
\hline
Task 3  &   Compiling and submitting the Inception report  &P   &   S&  &   &P  &       \\
\hline

\end{tabularx}
\end{center}

\end{table}


\end{document}
share|improve this answer
    
Thanks @David Carlisle. This works well! –  engineervix Mar 25 '12 at 22:02

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.