29

If I have defined tabular alignment with the p{} option,

\begin{tabular}{p{.3\textwidth}p{.7\textwidth}}
...(contents)...
\end{tabular}

Is there a way to specify that I want it \flushleft without wrapping the contents of each cell in a \parbox or minipage environment? It might be trouble in the example above, but I have dozens of cells, so is not a very feasible solution (unless I program something with Emacs Lisp).

0

2 Answers 2

27

Load the array package and write

\begin{tabular}{>{\raggedright}p{.3\textwidth}
                >{\raggedright\arraybackslash}p{.6\textwidth}}

It's necessary to use the \arraybackslash in the last column, otherwise \\ would not end the table row. You can use \newline to end lines in the last column cells (and the regular \\ in the other column cells).

By the way, a tabular specification like yours will give an overfull box, as you are not keeping into account the intercolumn spaces. If you want that the table extends all the way of the line width, say

\begin{tabular}{@{}>{\raggedright}p{.3\textwidth-\tabcolsep}
                >{\raggedright\arraybackslash}p{.7\textwidth-\tabcolsep}@{}}

after loading the calc package. Or, without it

\begin{tabular}{@{}
                >{\raggedright}
                p{\dimexpr.3\textwidth-\tabcolsep\relax}
                >{\raggedright\arraybackslash}
                p{\dimexpr.7\textwidth-\tabcolsep\relax}@{}}

The two @{} avoid the insertion of the intercolumn space before and after the tabular.

2
  • I like to define something like \def\myraggedright{\rightskip0mm plus10mm\relax} and use that instead of \raggedright in the column definition. This way you have the text flushed left without the large space between words, but with hyphenation, so that the output looks much less ragged than with \raggedright. Commented Jan 10, 2012 at 13:44
  • 2
    @ElmarZander You can also use the ragged2e package and its \RaggedRight command that does something similar to yours.
    – egreg
    Commented Jan 10, 2012 at 14:01
10

You could use a feature of the array package to insert a \raggedright command for flush left text, use >{command} before the column definition to insert one or several command(s):

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{array}
\begin{document}
\begin{tabular}{>{\raggedright\arraybackslash}p{.3\textwidth}p{.7\textwidth}}
    \blindtext & \blindtext
  \end{tabular}
\end{document}

You mentioned \flushleft - you could use it but it can insert undesired vertical space, such as

\begin{tabular}{>{\flushleft\arraybackslash}p{.3\textwidth}<{\endflushleft}p{.7\textwidth}}

If you need it often, consider defining a new column type using array features, as I did here:

\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

Some people define further useful types, such as

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

etc. So your tabular definition becomes simpler:

\begin{tabular}{P{.7\textwidth}P{.3\textwidth}}

In narrow columns, it may be good to allow hyphenation. This can be done using ragged2e. microtype could improve even more. So I would use:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{array}
\usepackage{ragged2e}
\usepackage{microtype}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}
\begin{document}
\begin{tabular}{P{.7\textwidth}P{.3\textwidth}}
    \blindtext & \blindtext \\
  \end{tabular}
\end{document}

With ragged2e commands \arraybackslash is not needed. As the first word in a box would not be hyphenated, I additionally inserted zero horizontal space.

2
  • 1
    \arraybackslash is necessary only in the last column.
    – egreg
    Commented Oct 13, 2011 at 23:30
  • 1
    Still works with it though... and I like the convenience of having a new column type!
    – hatmatrix
    Commented Oct 15, 2011 at 11:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .