Graphics, Figures & TablesHow can I specify column width and then centering text?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
lemonicetea
Posts: 17
Joined: Sat Oct 10, 2009 9:28 pm

How can I specify column width and then centering text?

Post by lemonicetea »

Hi all.

How can I specify column width and then centering text in tables? I tried p{2cm},however, the text will left aligned.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

How can I specify column width and then centering text?

Post by localghost »

Take a look at the array package. Beside other features it lets you specify which commands are active in a column.

Code: Select all

\begin{tabular}{>{\centering}p{10cm}}
  % contents
\end{tabular}
In this context you may also look at tabularx and ragged2e.


Best regards
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

How can I specify column width and then centering text?

Post by Stefan Kottwitz »

Hi,

use \centering and the array package:

Code: Select all

\usepackage{array}
...
\begin{tabular}{>{\centering\arraybackslash}p{2cm}...}
Stefan
LaTeX.org admin
lemonicetea
Posts: 17
Joined: Sat Oct 10, 2009 9:28 pm

Re: How can I specify column width and then centering text?

Post by lemonicetea »

Thank you so much guys. That was really quick replies!
lemonicetea
Posts: 17
Joined: Sat Oct 10, 2009 9:28 pm

Re: How can I specify column width and then centering text?

Post by lemonicetea »

By the way, how can i centering the whole table?
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

How can I specify column width and then centering text?

Post by localghost »

That's best done within a table environment.

Code: Select all

\begin{table}[!ht]
  \caption{Dummy table}\label{tab:dummy}
  \centering
  \begin{tabular}{>{\centering}p{10cm}}
    % contents
  \end{tabular}
\end{table}
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

How can I specify column width and then centering text?

Post by Stefan Kottwitz »

Use the center environment:

Code: Select all

\begin{center}
\begin{tabular}
...
\end{tabular}
\end{center}
or, if you want to use a table environment, use \centering instead, for instance:

Code: Select all

\begin{table}[htbp]
\centering
\begin{tabular}
...
\end{tabular}
\end{table}
Stefan
LaTeX.org admin
lemonicetea
Posts: 17
Joined: Sat Oct 10, 2009 9:28 pm

Re: How can I specify column width and then centering text?

Post by lemonicetea »

It's working! Thank you sooo much.
Post Reply