Centering tabular: Why does this work?
First of all, your >{{\raggedright}}
does nothing, because {\raggedright}
is inserted at the start of the cell and the effect is "do nothing", because the declaration is issued in a group. What you need is \arraybackslash
(I'll explain why later on).
\begin{tabular}
{>{\raggedleft}p{\dimexpr.5\linewidth-2\tabcolsep-.5\arrayrulewidth}|
>{\raggedright\arraybackslash}}p{\dimexpr.5\linewidth-2\tabcolsep-.5\arrayrulewidth}}
You are asking for a two column alignment. Both columns are of type p
and the width is
half the current line width, reduced by twice
\tabcolsep
and half\arrayrulewidth
You have to know that the structure of a row in your tabular is
t C t | t C t
where t
denotes a space of width \tabcolsep
, C
denotes a cell and |
denotes the vertical rule, which has width \arrayrulewidth
.
Thus, since the width of C
is as stated before, the tabular will be as wide as \linewidth
:
t + (0.5L - 2t - 0.5a) + t + a + t + (0.5L - 2t - 0.5a) + t = L
(where L is the line width).
Why \arraybackslash
? Because \raggedright
redefines \\
and so this command could not be used for ending a row. The command \arraybackslash
restores the correct meaning of \\
. It's not required in the first column specifier, but you can of course use it also there.
As egreg mentions is his comment, using >{{\raggedright}}
won't work as you can see when you add more text:
\documentclass{report}
\usepackage{array}
\begin{document}
\noindent
\begin{tabular}
{>{\raggedleft\arraybackslash}p{\dimexpr.5\linewidth-2\tabcolsep-.5\arrayrulewidth}|
>{{\raggedright}}p{\dimexpr.5\linewidth-2\tabcolsep-.5\arrayrulewidth}}
Kindle Edition Available & k \\
PC/Mac Edition Available & p text some text to test text some text to test text some text to test text some text to test text some text to test
\end{tabular}
\end{document}
Using simply >{\raggedright}
for the last column and \\
to change lines will produce an error since \centering
, \raggedright
, \raggedleft
redefine \\
; the array
package provides \arraybackslash
to restore the meaning, so the following works:
\documentclass{report}
\usepackage{array}
\begin{document}
\noindent
\begin{tabular}
{>{\raggedleft\arraybackslash}p{\dimexpr.5\linewidth-2\tabcolsep-.5\arrayrulewidth}|
>{\raggedright\arraybackslash}p{\dimexpr.5\linewidth-2\tabcolsep-.5\arrayrulewidth}}
Kindle Edition Available & k \\
PC/Mac Edition Available & p text some text to test text some text to test text some text to test text some text to test text some text to test
\end{tabular}
\end{document}