Text FormattingTable completely centered

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
InquisitorMo
Posts: 44
Joined: Mon Jul 15, 2013 12:22 am

Table completely centered

Post by InquisitorMo »

I wish to create a centered table like the table below where the delineator line is centered on the page. I have tried centering it by wrapping in a {table} environment, and by using \centering, but neither has worked for me - obviously I'm doing something wrong?

Code: Select all

\documentclass[letter,12pt]{article}
\usepackage{tabularx}

\newcolumntype{z}[1]{>{\raggedleft\arraybackslash}p{#1}} 

\begin{document}
  \begin{tabularx}{\textwidth}{r|l}
    \textbf{\small{100 Main Street}} & \textbf{\small{+1 555.555.5555}} \\
    \textbf{\small{New York, NY 10011}} & \textbf{\small{MyEmailAddress@mail.com}}
  \end{tabularx}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Table completely centered

Post by hugovdberg »

The delineator line is the vertical line in the middle of the table I assume? In that case you probably have to make sure both columns are of equal width. The table environment mostly only declares it as a floating block, \centering centers the entire table without looking at its contents. So to make sure the vertical line is centered you have to manually specify the width of the columns on either side of the line. This could be easily done using p-type columns or with a new columntype as you already declared in your example.
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
User avatar
Stefan Kottwitz
Site Admin
Posts: 10300
Joined: Mon Mar 10, 2008 9:44 pm

Table completely centered

Post by Stefan Kottwitz »

You could use X columns and \hfill. For example:

Code: Select all

\documentclass[letter,12pt]{article}
\usepackage{tabularx}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{>{\hfill}X|X}
  \textbf{\small{100 Main Street}} & \textbf{\small{+1 555.555.5555}} \\
  \textbf{\small{New York, NY 10011}} & \textbf{\small{MyEmailAddress@mail.com}}
\end{tabularx}
\end{document}
I also used \noindent, just to avoid the paragraph indentation in this case.

Stefan
LaTeX.org admin
InquisitorMo
Posts: 44
Joined: Mon Jul 15, 2013 12:22 am

Re: Table completely centered

Post by InquisitorMo »

Ahh! Stefan's solution works brilliantly!

I'm not entirely sure how it works though.

My understanding is obviously limited - I know that the ">" adds a command of some sort - so then \hfill is used to fill the cell with spaces. But how does the right and left alignment occur if the 'X' parameter is used?

Also - I'm not sure what you meant by using no indent?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10300
Joined: Mon Mar 10, 2008 9:44 pm

Table completely centered

Post by Stefan Kottwitz »

> inserts a command at the beginning of a cell, this way you can style a complete column.

left alignment is the default, as in the right column, for the left column the \hfill pushes the content of the line to the right, so causes right alignment. Just for a line, if it were several lines \raggedright in a parbox (and perhaps \hfill before) could be an option. Similarly, \hfill at the right of a line would push the content to the left. It's just stretching white space.

The default behavior of a standard class is to indent the first line of a paragraph. You don't want it for the table, since it would go over the right margin then, so I used \noindent.

Indentation is to mark paragraphs. Often a paragraph break is shown by a bit vertical space between paragraphs, instead of indenting the first line. For example, it could be achieve by \usepackage{parskip}.

Stefan
LaTeX.org admin
Post Reply