# How can i make a tabular fit the page width with the text centered?

I want to create a table that will be as wide as the page. I used this code :

\begin{tabular}{|l@{\hspace{7pt}}|c@{\hspace{50pt}}|c@{\hspace{80pt}}|}
\hline Τ1 & Triangle Inequallity & $\beta-\gamma<a<\beta+\gamma$ \\
\end{tabular}


But the text is not centered. Is there a way to fit the page automatically with the text centered?

-

## 1 Answer

with the tabularx package and defining a new column such that the text is centered:

\documentclass{article}
\usepackage{tabularx}
\usepackage{showframe}
\begin{document}

\newcolumntype{C}{>{\centering\arraybackslash} X}

{\par\centering

\begin{tabularx}{\textwidth}{|l|C|C|}
\hline T1 & Triangle Inequallity & $\beta-\gamma<a<\beta+\gamma$ \\
\end{tabularx}

\par}

\end{document}


I'm using showframeto show that the table does fit the text width. Change the margins with the geometry package to test with other widths.

-
Since the tabularx environment is set to be as wide as \textwidth, there's no need to insert the \centering command. Also, the two \par statements do nothing at the moment since they're preceded by and succeeded by empty lines, which also generate paragraph breaks. It might be cleanest to provide a single \noindent statement before \begin{tabularx}.... –  Mico May 2 '13 at 17:29
@Mico but the OP wants the text to be centered, if you remove \centering (i.e. a plain X column) the text is not centered anymore. If instead you mean the {\par\centering ... \par} stuff, without that the table results indented, since my MWE uses the article class. –  dcmst May 2 '13 at 17:34
@Mico, for some reason I'm seeing your full comment only now. I prefer not to use \noindent, the code looks much cleaner this way to my eyes. Also I thought that the {\par\centering stuff may have been more useful to the OP (also to understand what's going on in the code). Oh, and the empty lines are for clarity :) –  dcmst May 2 '13 at 17:37
Sorry about not having been sufficiently clear which \centering instruction I was referring to as being unneeded: Indeed, it was the one after \par and before \begin{tabularx}. I suppose the \centering instruction is not entirely superfluous, because it serves to undo the paragraph indent that LaTeX would otherwise insert. (However, the two \par statements are indeed superfluous.) Based on my personal aesthetics, I think it may be more conducive to understand what's going on if the paragraph indent is undone explicitly via \noindent rather than implicitly via \centering. –  Mico May 2 '13 at 17:44
@Mico I see what you mean but as I said, the \par stuff may be useful to the OP if he uses this code in a more complex document, so I used the version I thought would have been more useful for the OP, indipendently of the actual MWE. –  dcmst May 2 '13 at 17:47