9

I want to be able to center my table, but align the center by the end of the first cell. Currently just using the \begin{center} command i am able to center the table but i would rather the table be centered based off the end of the first cell and not by the average center of the table.

\documentclass{article}  
\begin{document}  

\begin{center}
\begin{tabular}{r | l}
I am some really long text & 10pt \\ 
I am also really long & 12pt \\ 
\end{tabular}
\end{center}
\end{document}  

Gets a table that is centered, but i would rather it went "I am some really long text {center here} 10pt.

Edit: The idea i'm kind of looking for can be seen in the way that you can align equations at any point.

lockstep
  • 250,273
  • 2
    Just to clarify: you want the vertical rule separating the columns to be centered on the page? Rather than as it is currently with the full contents evenly centered on the page. Correct? – Paul Gessler Feb 24 '14 at 03:37
  • 1
    That's exactly it, and much better worded than my original question – Paul Thompson Feb 24 '14 at 03:40

4 Answers4

9

Just a variant of Poul's answer. Instead array package can use tabularx package and define new column types R and L. There is no need to measure width of columns:

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\begin{document}
\begin{center}
  \begin{tabularx}{\textwidth}{|R|L|}
    I am some really long text & 10pt \\
    I am also really long      & 12pt 
  \end{tabularx}
\end{center}  
\hrulefill.\hrulefill % just for visual check
\end{document}  
Zarko
  • 296,517
5

I don't know why Chris (cmhughes) deleted his answer. But, for this simple job, no need of loading a heavy package like tabularx. Without it, this is how it is done.

\documentclass{article}
\usepackage{showframe}
\usepackage{array}
\newcolumntype{L}{>{\raggedright\arraybackslash}p{\dimexpr0.5\textwidth
                                                   -2\tabcolsep-\arrayrulewidth\relax}}
\newcolumntype{R}{>{\raggedleft\arraybackslash}p{\dimexpr0.5\textwidth
                                                   -2\tabcolsep-\arrayrulewidth\relax}}
\begin{document}

\begin{center}
    \begin{tabular}{R|L}
    aaaa    I am some really long text & I am some really long text aaaa\\
        I am also really long      & I am also really long     \\
    \end{tabular}
\end{center}
\hrulefill.\hrulefill % just for visual check
\end{document}

enter image description here

4

Obviously, extra columns could be inserted on either (sub)tabular to extend the table leftward or rightward, respectively, even as the dividing line between the halves remains centered.

\documentclass{article}  
\begin{document}  

\begin{center}
\makebox[0pt][r]{%
\begin{tabular}{r |}
I am some really long text\\ 
I am also really long\\ 
\end{tabular}%
}%
\makebox[0pt][l]{%
\begin{tabular}{| l}
 10pt \\ 
 12pt \\ 
\end{tabular}%
}
\end{center}
\end{document} 

enter image description here

3

Steven's answer will work, but if you'd like to avoid breaking up the tabular (at the cost of needing to specify widths):

Based on this answer, we can use facilities of the array package to define new column types L{<width}, C{<width>}, R{<width>} for which an explicit width may be declared. Choose this <width> to fit the widest content in all columns and set it for all columns:

\documentclass{article}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}  
\begin{center}
  \begin{tabular}{R{1.7in} | L{1.7in}}
    I am some really long text & 10pt \\ 
    I am also really long      & 12pt \\ 
  \end{tabular}
\end{center}
\hrulefill.\hrulefill % just for visual check
\end{document}  

enter image description here

Paul Gessler
  • 29,607