28

I am trying to typeset a table of two columns, where each column consists of paragraphs and the paragraphs in the right hand column are set raggedleft. Here is a minimal example of the code I thought would work:

\documentclass{article}
\usepackage{array}
\newcommand{\lorem}{Lorem ipsum dolor sit amet, consectetur adipisicing elit.}
\begin{document}
  \begin{tabular}{p{50mm}>{\raggedleft}p{50mm}}
  \lorem&\lorem\\
  \lorem&\lorem
  \end{tabular}
\end{document}

However, that produces the error message

! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate

l.7   \lorem&
     \lorem
? 

and the output is not at all what I expected either.

Is there a better way?

3
  • I have to admit, I never got comfortable using LaTeX's tabular environment (other than in rather trivial cases). With plain TeX's \halign I know what I am doing, and I can consistently get the result I want. However, people keep telling me that using plain TeX constructs in LaTeX will get me in trouble. Nov 2 '10 at 16:08
  • Correct me if I'm wrong, but isn't \halign a TeX primitive, not a plain-tex macro? As such, it pretty much has to be the way latex constructs tables (that is, the tables themselves, not captions and tablecounters etc.)
    – morbusg
    Nov 2 '10 at 16:59
  • @morbusg: Yes, ´\halign` is indeed a primitive. But primitives are frowned upon as much as plain macros. I have certainly been told to stay away from \hbox (another primitive). IIRC, the rationale had to do with colour commands, so if I don't change colours inside the \hbox, I should be reasonably safe. Nov 2 '10 at 17:34
25

You have to restore \\ for use in tabular after inserting switches like \raggedleft in the last column. See the array manual for details.

\documentclass{article}
\usepackage{array}

\newcommand{\lorem}{Lorem ipsum dolor sit amet, consectetur adipisicing elit.}

\begin{document}
  \begin{tabular}{p{5cm}>{\raggedleft\arraybackslash}p{5cm}}
    \lorem&\lorem\\
    \lorem&\lorem
  \end{tabular}
\end{document}
3
  • 6
    Okay, thanks, that did it. But seriously, some more user oriented documenation would have been nice. \arraybackslash is described on page 18 of a 31 page manual, in the midst of a description of the package implementation. Nobody is likely to find it unless they want to learn how the array package is implemented. Oh well. Nov 2 '10 at 17:31
  • Nearly every modern PDF viewer offers a search function. So it should not be difficult to find that. Nov 2 '10 at 17:44
  • 2
    Except the documentation mentioned only \raggedright, not \raggedleft. (If I had only used the interactive search, I would have found it.) Nov 2 '10 at 18:38
20

Is there a better way?

I would say yes. I suggest to use \RaggedLeft instead: there's no \arraybackslash fix required and the justification is better, regarding that there's hyphenation within p columns.

Explanation:

For a better illustration, let's reduce the p column width. Here's the \raggedleft way:

\documentclass{article}
\usepackage{array}
\newcommand{\lorem}{Lorem ipsum dolor sit amet, consectetur adipisicing elit.}
\begin{document}
  \begin{tabular}{p{30mm}>{\raggedleft\arraybackslash}p{30mm}}
  \lorem&\lorem
  \end{tabular}
\end{document}

alt text

Now we change:

\usepackage{ragged2e}
...
  \begin{tabular}{p{30mm}>{\RaggedLeft}p{30mm}}

alt text

It's not so extremely ragged because now hyphenation is supported, just like in the p cell on the left.

Further notes:

  • The content of p cells is fully justified. This may cause undesired gaps between words and does not match the ragged way on the right side. A \RaggedRight for the left p cell could be a good and consistent idea.

  • Especially in narrow columns additionally inserting \hspace{0pt} ensures proper hyphenaton. Otherwise, TeX would not hyphenate the first word of the box.

Since a new column type makes all easier, my choice would be

\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\RaggedLeft\hspace{0pt}}p{#1}}

and in the body text I'd just write

\begin{tabular}{L{30mm}R{30mm}}

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.