GeneralProblem with tabular, left-align and widths

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
mayngontay
Posts: 6
Joined: Thu May 29, 2008 9:45 am

Problem with tabular, left-align and widths

Post by mayngontay »

Dear all,

I would like to use tabular environment. But I don't like justified-text in columns by using like as follows:
\begin{tabular}{p{0.18\linewidth} p{0.38\linewidth} p{0.38\linewidth}}
...
\end{tabular}

So, the code should be:
\begin{tabular}{l{0.18\linewidth} l{0.38\linewidth} l{0.38\linewidth}}
...
\end{tabular}

(l here is l in letter not 1-one, not slash)

But I got the error:
! LaTeX Error: Illegal character in array arg.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.4 ...th} l{0.38\linewidth} l{0.38\linewidth}@{}}

Could any one help me to solve this problem?

Thanks,

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Problem with tabular, left-align and widths

Post by localghost »

I'm not sure if I understood you right. So I worked out an example that shows whether I did.

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage[font=small,labelfont=bf]{caption}
\usepackage[bindingoffset=1cm,centering,includeheadfoot,margin=2cm]{geometry}
\usepackage{array}
\usepackage{txfonts}            % Postscript font package (Times style)

\begin{document}
  \begin{centering}
    \begin{tabular}{>{\raggedright}p{0.18\linewidth}>{\raggedright}p{0.38\linewidth}>{\raggedright}p{0.38\linewidth}}
      The quick brown fox jumps over the lazy dog &
      The quick brown fox jumps over the lazy dog &
      The quick brown fox jumps over the lazy dog
    \end{tabular}
  \end{centering}
\end{document}
There may be some warning because of filling the lines in the single columns. You can see the resulting document in the attachment.


Best regards
Thorsten
Attachments
olatex_77411.pdf
A tabular with left aligned columns and line break.
(12.75 KiB) Downloaded 1513 times
balf
Posts: 158
Joined: Sat Jan 12, 2008 1:11 am

Re: Problem with tabular, left-align and widths

Post by balf »

Try replacing, e.g., p{0.18\linewidth} with >{\raggedright}p{0.18\linewidth} in the preamble of your tabular environment. This requires loading the array package.
You even can define a new column type:
\newcolumntype{P}[1]{>{\raggedright}p{#1\linewidth}} and write \begin{tabular}[P{0.18}P{0.38}P{0.38}}

B.A.
mayngontay
Posts: 6
Joined: Thu May 29, 2008 9:45 am

Re: Problem with tabular, left-align and widths

Post by mayngontay »

Thank your for your quick response.
Your code works with only one row. When I add one more row like below:
\begin{tabular}{>{\raggedright}p{0.18\linewidth}>{\raggedright}p{0.38\linewidth}>{\raggedright}p{0.38\linewidth}}
The quick brown fox jumps over the lazy dog &
The quick brown fox jumps over the lazy dog &
The quick brown fox jumps over the lazy dog\\
The quick brown fox jumps over the lazy dog &
The quick brown fox jumps over the lazy dog &
The quick brown fox jumps over the lazy dog
\end{tabular}

the the error is:

! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate
l.16 ... quick brown fox jumps over the lazy dog &

Any ideas?

Thanks,
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Problem with tabular, left-align and widths

Post by localghost »

Replace \\ by \tabularnewline. The ragged2e package (part of the ms bundle) offers more capability. You then could replace \raggedright by \Raggedright and keep the \\ for the new tabular line.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10340
Joined: Mon Mar 10, 2008 9:44 pm

Problem with tabular, left-align and widths

Post by Stefan Kottwitz »

Hi mayngontay,
the code above working if you insert \arraybackslash:

Code: Select all

\begin{tabular}{>{\raggedright}p{0.18\linewidth}>{\raggedright}p{0.38\linewidth}>{\raggedright\arraybackslash}p{0.38\linewidth}}
Stefan
LaTeX.org admin
avp3000
Posts: 49
Joined: Thu Nov 15, 2007 4:22 pm

Problem with tabular, left-align and widths

Post by avp3000 »

Hi there,

I am also not sure that I understood you correcly, but i think that what bothers you is the fact that you cannot simulatiously set the width and the justification of a column in a table. I had a similar problem in the past. The most reasonable solution I found to it was the tabularx package. In this package you fix the whole width of a table and than you either specify for a column to be l(left), c(entered), r(ight) or you just write X. The width of the colums, that have the X specification will be determined automatically and will depend on the whole width of the table. However, you can also specify the justification of these columns by using the following command:

Code: Select all

>{\raggedright\arraybackslash}X
(choose between \reggedright, \reggedleft or \centering).

I have attached the documentation of the package, you can download the package from ctan.
Attachments
tabularx.pdf
The documentation to the tabularx package.
(179.06 KiB) Downloaded 1019 times
mayngontay
Posts: 6
Joined: Thu May 29, 2008 9:45 am

Problem with tabular, left-align and widths

Post by mayngontay »

Dear all,

The problem has been solved from the fifth post by localghost.
I know that there are other options proposed by balf, Stefan_K, and avp3000 to solve this issue.
Thank you (localghost, balf, Stefan_K, and avp3000) very much.
What you gave me is beyond what I expected. :D

Regards,
Post Reply