4

I'd like to have the vertical rule in my {r|l} table to be horizontally aligned in the center of the page.

So far, I've only managed to do

\begin{center}
  \begin{tabular}{r|l}
    short & long long long \\
    short & long long long \\
    short & long long \\
    short & long long long\\
  \end{tabular}
\end{center}

\begin{center}
  \begin{tabular}{r|l}
    long long & short
  \end{tabular}
\end{center}

which yields Output

How can I center the vertical lines of my tables to be in the center of the page?

  • I'd like to have them both aligned such that the vertical line is in the middle of \pagewidth – Stephan Jan 15 at 4:56
4

You may need to define new column type (thanks @lockstep in his answer)

\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{tabular}{ R{0.465\textwidth} | L{0.465\textwidth}}
        short & long long long \\
        short & long long long \\
        short & long long \\
        short & long long long\\
    \end{tabular}

    \begin{tabular}{ R{0.465\textwidth} | L{0.465\textwidth}}
        long long & short
    \end{tabular}

\end{document}

which gives

enter image description here

  • That's exactly it, thank you! One question though: why is \raggedleft used in the right-aligned column? – Stephan Jan 15 at 4:58
  • 2
    The command \raggedleft pushes all text to the right of the line or column, keeping the left side text margin ragged. // ...One more minor correction to my answer: 0.49\textwidth gives an "Overfull \hbox (10.59pt too wide)". This is due to the column separator and the adjacent spaces on either side. So, you will have to allot somewhat less width to the columns to really center the column-separator. In my example, 0.465 worked best (editing accordingly). – Partha D. Jan 15 at 6:16
  • The answer from @CarLaTeX is definitely the more (probably the most) scientific and general solution. – Partha D. Jan 15 at 7:46
  • One could measure the distance from the sides to the column divider using tikzmarks, but the given solution is easier. – John Kormylo Jan 15 at 15:16
5

You could give a fixed length to the columns of the tables.

If you want the columns to occupy all the space possible in the half of the line, you have to calculate their column width in this way, in order to not have an overfull hbox:
.5\linewidth (half line)
-2\tabsep (minus the two spaces before and after the text column)
-.5\arrayrulewidth (minus half of the width of the vertical bar).

In my MWE I use the calc package, without it you can write \setlength{\myhalfpage}{\dimexpr .5\linewidth-2\tabcolsep-.5\arrayrulewidth} to get the same result.

You can use lockstep's definitions with this length.

\documentclass{article}
\usepackage{array}
\usepackage{calc}
\newlength{\myhalfpage}
\setlength{\myhalfpage}{.5\linewidth-2\tabcolsep-.5\arrayrulewidth}
\newcolumntype{L}{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{\myhalfpage}}
\newcolumntype{C}{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{\myhalfpage}}
\newcolumntype{R}{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{\myhalfpage}}

\begin{document}

\begin{center} 
\begin{tabular}{R|L} 
short & long long long \\
short & long long long \\ 
short & long long \\ 
short & long long long\\ 
\end{tabular} 
\end{center} 
\begin{center} 
\begin{tabular}{R|L} 
long long & short 
\end{tabular} 
\end{center}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.