Graphics, Figures & TablesCenter Data in Table Cell containing Text

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
nenu28
Posts: 28
Joined: Mon Jun 27, 2011 11:24 am

Center Data in Table Cell containing Text

Post by nenu28 »

Hi,

I would like to create a table containing text and data in the center of the cell, like this example : Monday, Tuesday, Wednesday should be placed at the center of the cell. The same for the temperature data. But the text no change !!

Thanks in advance for your suggestion.

Here is the code of the example

Code: Select all

\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}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Center Data in Table Cell containing Text

Post by Stefan Kottwitz »

Hi,

for horizontal centering use c columns instead of l, such as

Code: Select all

\begin{tabular}{ | c | c | c | p{5cm} |}
For vertical centering you could use m columns with the array package, such as

\usepackage{array}

Code: Select all

\begin{tabular}{ | m{2cm} | m{2cm} | m{2cm} | p{5cm} |}
For getting both, you could define a new style, further use m instead of p:

Code: Select all

\documentclass{article}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\begin{center}
    \begin{tabular}{ | C{2cm} | C{2cm} | C{2cm} | m{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}
\end{document}
\end{document}
Stefan
LaTeX.org admin
nenu28
Posts: 28
Joined: Mon Jun 27, 2011 11:24 am

Re: Center Data in Table Cell containing Text

Post by nenu28 »

Thank you so much M. stefan K. It works !
Post Reply