Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

About a month ago, someone asked how to align a tabular environment around the intercolumn space, which was something I'd been trying to figure out for a brief eternity. Werner's answer worked more or less fine, though I had to add extra braces around \raggedright. I was wondering, though, if someone could explain to me why it worked.

Here's the code that I'm using:

\usepackage{array}
%...

\noindent
\begin{tabular}
  {>{\raggedleft}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
\end{tabular}

I think it's mostly the math for the paragraph widths that I'm not understanding, but I'd really appreciate it if someone could explain the whole thing in English. The part that I get is "insert the command \raggedright before the first paragraph-style column and \raggedleft before the second paragraph-style column"; otherwise I'm in the dark.

If I was unclear about what I'd been trying to do, here's the difference between what that block produces and what this one does:

\begin{center}
\begin{tabular}{rl}
    Kindle Edition Available & k \\
    PC/Mac Edition Available & p
\end{tabular}
\end{center}

sample output

Thanks so much for your help!

share|improve this question
1  
If you use >{{\raggedright}} then \raggedright will be executed in a group and will have no effect. –  egreg Jun 22 '12 at 16:33
    
Oh dear. Thanks. When I didn't have the extra braces in, the usual `\` row-break broke. I guess that was a non-solution, though. –  Daniel Jun 22 '12 at 16:40

2 Answers 2

up vote 12 down vote accepted

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.

share|improve this answer
    
That makes perfect sense. Thank you for making it clearer. You've been incredibly helpful with both my silly questions so far, and I really appreciate it! –  Daniel Jun 22 '12 at 16:52

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}

enter image description here

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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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