0

http://en.wikibooks.org/wiki/LaTeX/Tables contains an example on how to do text wrapping in tables by using p{5cm}:

\begin{center}
    \begin{tabular}{ | l | l | l | p{5cm} |}
    \hline
    Day & Min Temp & Max Temp & Summary \\ \hline
    Monday & 11C & 22C & A clear day with lots of sunshine.  
    However, the strong breeze will bring down the temperatures. \\ \hline
    Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
    across most of Scotland and Northern Ireland,
    but rain reaching the far northwest. \\ \hline
    Wednesday & 10C & 21C & Rain will still linger for the morning.
    Conditions will improve by early afternoon and continue
    throughout the evening. \\
    \hline
    \end{tabular}
\end{center}

How do I get the contents of the cells in that column to be centered?

Here is what I have tried:

\begin{table}[h]
\centering
\resizebox{\textwidth}{!}{%
\begin{tabular}{p{20cm}}
\rowcolor[HTML]{3166FF}
\pbox{
\Huge Line 1 of my long title
\\ \Huge Line2 of my long title} \\ \hline
\huge Sub title
\end{tabular}
}
\end{table}

I want to achieve something like this from Word:

enter image description here

I have gotten closer, but it still doesn't look professional in LaTeX:

\begin{table}[h]
\centering
\resizebox{\textwidth}{!}{%
 \begin{tabular}{p{13cm}}
 \rowcolor[HTML]{3166FF}
 \centering \Huge This is a very long title that spans multiple lines \\ \hline
 \centering \huge Sub title
 \end{tabular}
}
\end{table}

enter image description here

  • See my answer, but do you really want to center multiple lines? – user31729 Mar 10 '15 at 9:54
  • @SoundsOfSilence Yes, I want to achieve something like this from Word: tommycarstensen.com/latex.png – tommy.carstensen Mar 10 '15 at 9:57
4

Use \newcolumntype from array package and define a style, say C with automatic wrapping and centering.

The principle way to define a new column type to use one of existing ones, i.e. l, r, p or c.

The syntax is (with an argument)

\newcolumntype{Y}[1]{>{some stuff before\arraybackslash}Z{#1}}, where Y stands for the new letter, Z is one of already existing types.

Just before \arraybackslash, basically anything can be inserted, say a \centering, so this will be used inside the Z - type.

Edit Some propositions to improve the look of the table.

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}



\begin{document}

\begin{center}

    \begin{tabular}{l*{2}{C{2cm}}C{5cm}}
      \multicolumn{4}{c}{ \large \textbf{ The good table...}} \tabularnewline[2ex]

      \toprule
    Day & Min Temp (\si{\degreeCelsius}) & Max Temp (\si{\degreeCelsius})  & Summary \tabularnewline 
    \midrule
    Monday & 11 & 22 & A clear day with lots of sunshine.  
    However, the strong breeze will bring down the temperatures. \tabularnewline[1ex]
    Tuesday & 9 & 19 & Cloudy with rain, across many northern regions. Clear spells
    across most of Scotland and Northern Ireland,
    but rain reaching the far northwest. \tabularnewline[1ex]
    Wednesday & 10 & 21 & Rain will still linger for the morning.
    Conditions will improve by early afternoon and continue
    throughout the evening. \tabularnewline[1ex]
    \bottomrule
    \end{tabular}
\end{center}


\begin{center}
    \begin{tabular}{ | l | l | l | C{5cm} |}
      \multicolumn{4}{c}{ \large \textbf{ and the bad and ugly...}} \tabularnewline[2ex]
    \hline
    Day & Min Temp & Max Temp & Summary \tabularnewline \hline
    Monday & 11C & 22C & A clear day with lots of sunshine.  
    However, the strong breeze will bring down the temperatures. \tabularnewline \hline
    Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
    across most of Scotland and Northern Ireland,
    but rain reaching the far northwest. \tabularnewline \hline
    Wednesday & 10C & 21C & Rain will still linger for the morning.
    Conditions will improve by early afternoon and continue
    throughout the evening. \tabularnewline
    \hline
    \end{tabular}
\end{center}
\end{document}

enter image description here

  • There are some issues with the formatting of the temperatures... I will edit my answer to attack that – user31729 Mar 10 '15 at 9:54
  • It works, but I don't understand it. Thanks. I am able to accept your answer in 7 minutes. – tommy.carstensen Mar 10 '15 at 9:54
  • You didn't dare showing the result. ;-) – egreg Mar 10 '15 at 9:57
  • @egreg: I knew the High Commander of the Holy Table Inquistion (i.e. you) would show up. No, I will improve the table and show the good and the bad one, just to annoy you :-P – user31729 Mar 10 '15 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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