[Tex/LaTex] Text too long for the cell

formattinghorizontal alignmentrotatingtablesvertical alignment

I am doing this table, but my the phrases are out of the limits of the cells. How can I adjust them in two or three lines instead of one?

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}  
\usepackage{rotfloat}
\usepackage{multirow}
\begin{document}

\begin{sidewaystable}
    \begin{tabular}{|p{5cm}|p{1.2cm}|p{1.2cm}|p{1.7cm}|p{1.7cm}|p{1.2cm}|p{1.2cm}|p{1.2cm}|p{1.7cm}|p{1.7cm}|p{1.2cm}|}
        \hline
        \multirow{3}{*}{University} & \multicolumn{4}{c|}{Year 1996} &  \multirow{3}{*}{Total}  & \multicolumn{4}{c|}{Year 2010} & \multirow{3}{*}{Total} \\
        \cline{2-5} \cline{7-10}
        & \multicolumn{2}{c|}{Students} & \multirow{2}{*}{Docentes universitarios} & \multirow{2}{*}{Personal administrativo y de servicio} &  & \multicolumn{2}{c|}{Students} & \multirow{2}{*}{Docente universitario} & \multirow{2}{*}{Personal administrativo y de servicio}&  \\
        \cline{2-3} \cline{7-8}
        & Pre grade & Post grade &  &  &  & Pre grade & Post grade &  & &  \\
        \hline
        National University & 3225 & 268 & 453 & 498 & 4434 & 4903 &976 & 445 & 802 & 7126 \\
   \hline

    \end{tabular}

    \end{sidewaystable}
\end{document} 

As I am using \multirow{...} and |p{Xcm}|, the words are not centered, how can I fixed this?

Best Answer

This stays as close as possible to your original table. I don't recommend doing it this way, but it works. I fiddled with the text block height to get it to fit, since I'm assuming it fits in your actual page. I've also faked hyphenation because I wasn't sure which language to tell Babel. (Obviously, you should take my hyphenation points out and let Babel do its thing in your real document.)

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

defines a new column type, C{<width>} which is just like p but centred. If you want vertical centring as well, substitute m for p.

\renewcommand\multirowsetup{\centering}

This alters the default formatting for \multirow content from \raggedright to \centering.

Code:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[scale=.825]{geometry}% without this you'll get US letter paper layout on A4
\usepackage{rotfloat,array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage{multirow}
\renewcommand\multirowsetup{\centering}
\begin{document}
\begin{sidewaystable}
\begin{tabular}{|C{5cm}|C{1.2cm}|C{1.2cm}|C{1.7cm}|C{1.7cm}|C{1.2cm}|C{1.2cm}|C{1.2cm}|C{1.7cm}|C{1.7cm}|C{1.2cm}|}
  \hline
  \multirow{6}{*}{University} & \multicolumn{4}{c|}{Year 1996} &  \multirow{6}{*}{Total}  & \multicolumn{4}{c|}{Year 2010} & \multirow{6}{*}{Total} \\
  \cline{2-5} \cline{7-10}
  & \multicolumn{2}{c|}{Students} & \multirow{5}{1.7cm}{Docentes universit\-arios} & \multirow{5}{1.7cm}{Personal administrativo y de servicio} &  & \multicolumn{2}{c|}{Students} & \multirow{5}{1.7cm}{Docente universit\-ario} & \multirow{5}{1.7cm}{Personal administrativo y de servicio}&  \\
  \cline{2-3} \cline{7-8}
  & \multirow{3}{12mm}{Pre grade} & \multirow{3}{12mm}{Post grade} &  &  &  & \multirow{3}{12mm}{Pre grade} & \multirow{3}{12mm}{Post grade} &  & &  \\
  &&&&&&&&&&\\
  &&&&&&&&&&\\
  &&&&&&&&&&\\
  \hline
  National University & 3225 & 268 & 453 & 498 & 4434 & 4903 &976 & 445 & 802 & 7126 \\
  \hline
\end{tabular}
\end{sidewaystable}
\end{document}

tweaked table

However, this is not the best way to set tabular material. In particular, the use of vertical rules is not recommended and some horizontal rules should be heavier than others. In addition, more spacing is needed so that cell contents do not look squashed and are easier to read.

booktabs has a good discussion of best practice for typesetting professional-quality tabular material. It is worth reading. Even if it is occasionally perhaps a little overly dogmatic, it is a very useful antidote to the kind of formatting encouraged by word processors!

I would think about something more like this:

rethinking the table

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[scale=.825]{geometry}% without this you'll get US letter paper layout on A4
\usepackage{rotfloat,array,calc,booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage{multirow}
\renewcommand\multirowsetup{\centering}
\newlength\mylength
\setlength\mylength{20mm+\tabcolsep}
\begin{document}
\begin{sidewaystable}
  \begin{tabular}{C{5cm}*{4}{C{10mm}}*{2}{C{15mm}}*{2}{C{22.5mm}}*{2}{C{10mm}}}
  \toprule
  \multirow{3}*{University} & \multicolumn{4}{c}{Students} & \multicolumn{2}{c}{Docentes} & \multicolumn{2}{c}{Personal administrativo} &  \multicolumn{2}{c}{\multirow{2}{\mylength}{Total}}  \\
  & \multicolumn{2}{c}{Pre grade} & \multicolumn{2}{c}{Post grade} & \multicolumn{2}{c}{universitarios} & \multicolumn{2}{c}{y de servicio} &&\\
  \cmidrule(lr){2-3}\cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9} \cmidrule(lr){10-11}
  & 1996 & 2010  & 1996 & 2010  & 1996 & 2010 & 1996 & 2010 & 1996 & 2010  \\
  \midrule
  National University & 3225 & 268 & 453 & 498 & 4434 & 4903 &976 & 445 & 802 & 7126 \\
  \bottomrule
\end{tabular}
\end{sidewaystable}
\end{document}
Related Question