8

I want to make a table with three columns of paragraphs, each in raggedright. I can only make it work for the first two columns. If I try to add >{\raggedright}, as in the commented-out line below, I get the following error:

! Extra alignment tab has been changed to \cr.

Here's my MWE:

\documentclass{article}
\usepackage{array}
\newcommand\fox{The quick brown fox jumps.}

\begin{document}
\begin{tabular}{%
  >{\raggedright}p{1.5in}%
  >{\raggedright}p{1.5in}%
  p{1.5in}}
%  >{\raggedright}p{1.5in}} % doesn't work in 3rd column
  \fox & \fox & \fox \\
  \fox & \fox & \fox
\end{tabular}
\end{document}
  • If you use tabular* with a fixed target width (here: 5in), you should add the directive @{\extracolsep{\fill}} at the start of the second argument of the tabular* environment. – Mico Dec 23 '18 at 19:51
  • Thanks, Mico. I fixed the MWE to use tabular (the MWE was hacked down from an attempt to figure this out, which itself was hacked down from the original tabular* I was trying to include in a document). – dedded Dec 24 '18 at 3:35
6

if you use \tabularnewline, than you not need \arraybackslash for restoring meaning of \\:

\documentclass{article}
\usepackage{array}
\newcommand\fox{The quick brown fox jumps.}

\begin{document}
\begin{tabular}{*{3}{>{\raggedright}p{1.5in}}}%
  \fox & \fox & \fox    \tabularnewline
  \fox & \fox & \fox
\end{tabular}
\end{document}

enter image description here

off-topic: if you define width of all columns in table, than you not need to define table width with use tabular* environment, since it is determined by column widths. exception is, if you like to add @{\extracolsep{\fill}} by which you spread columns over prescribed table width.

  • Accepted this solution, since it explains my problem (\\ getting redefined). Not sure I don't prefer \arraybackslash though, since it allows the shorter \\ in each row. – dedded Dec 24 '18 at 3:27
10

Add \arraybackslash after \raggedright:

\documentclass{article}
\usepackage{array}
\newcommand\fox{The quick brown fox jumps.}

\begin{document}
\begin{tabular*}{5in}{%
  >{\raggedright\arraybackslash}p{1.5in}%
  >{\raggedright\arraybackslash}p{1.5in}%
  >{\raggedright\arraybackslash}p{1.5in}}% does work in 3rd column :)
  \fox & \fox & \fox \\
  \fox & \fox & \fox
\end{tabular*}
\end{document}

enter image description here

You can shorten the code a bit by repeating the same column 3 times:

\documentclass{article}
\usepackage{array}
\newcommand\fox{The quick brown fox jumps.}

\begin{document}
\begin{tabular*}{5in}{%
    *{3}{>{\raggedright\arraybackslash}p{1.5in}}}%
  \fox & \fox & \fox \\
  \fox & \fox & \fox
\end{tabular*}
\end{document}
  • with use only p column type in table i don't see why op use tabular* environment (which width is different than width of columns width). better is use ordinary tabular ... – Zarko Dec 23 '18 at 19:20
  • @Zarko In general I agree, in this particular case the widths are surprisingly well choosen. Taking the intercolumn spaces into account, the sum is only off by 0.003 inch – user36296 Dec 23 '18 at 19:24
3

You can also put \raggedright after p. I've just used it for the last column but it would be OK with the other columns. For more details see the descriptions mentioned by @DavidCarlisle in Auto Detect Last Paragraph and Pass to Macro.

\documentclass{article}
\usepackage{array}
\usepackage{lipsum}
\newcommand\fox{The quick brown fox jumps.}

\begin{document}
\begin{tabular*}{5in}{%
  >{\raggedright}p{1.5in}%
  >{\raggedright}p{1.5in}%
  p{1.5in}<{\raggedright}}
%  >{\raggedright}p{1.5in}} % doesn't work in 3rd column
  \lipsum[1] & \lipsum[1] & \lipsum[1] \\
  \fox & \fox & \fox
\end{tabular*}
\end{document}
  • that does not work in general, it assumes there is only one paragraph in the cell and no blank line at the ned, and no display math – David Carlisle Dec 23 '18 at 21:51

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.