How to get raggedright in tabular*?
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}
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}
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}
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.
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}