LaTeX forum ⇒ GeneralProblem with tabular, left-align and widths

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

Problem with tabular, left-align and widths

Postby mayngontay » Thu May 29, 2008 9:59 am

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,

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

Postby localghost » Thu May 29, 2008 10:53 am

I'm not sure if I understood you right. So I worked out an example that shows whether I did.
  1. \documentclass[11pt,a4paper]{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[latin1]{inputenc}
  4. \usepackage[english]{babel}
  5. \usepackage[font=small,labelfont=bf]{caption}
  6. \usepackage[bindingoffset=1cm,centering,includeheadfoot,margin=2cm]{geometry}
  7. \usepackage{array}
  8. \usepackage{txfonts} % Postscript font package (Times style)
  9.  
  10. \begin{document}
  11. \begin{centering}
  12. \begin{tabular}{>{\raggedright}p{0.18\linewidth}>{\raggedright}p{0.38\linewidth}>{\raggedright}p{0.38\linewidth}}
  13. The quick brown fox jumps over the lazy dog &
  14. The quick brown fox jumps over the lazy dog &
  15. The quick brown fox jumps over the lazy dog
  16. \end{tabular}
  17. \end{centering}
  18. \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 1085 times
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

balf
Posts: 158
Joined: Sat Jan 12, 2008 1:11 am

Postby balf » Thu May 29, 2008 11:01 am

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

Postby mayngontay » Thu May 29, 2008 11:20 am

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: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Thu May 29, 2008 11:36 am

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.
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

User avatar
Stefan Kottwitz
Site Admin
Posts: 9322
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Thu May 29, 2008 2:45 pm

Hi mayngontay,
the code above working if you insert \arraybackslash:
  1. \begin{tabular}{>{\raggedright}p{0.18\linewidth}>{\raggedright}p{0.38\linewidth}>{\raggedright\arraybackslash}p{0.38\linewidth}}

Stefan
Site admin

avp3000
Posts: 49
Joined: Thu Nov 15, 2007 4:22 pm

Postby avp3000 » Thu May 29, 2008 5:05 pm

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:
  1. >{\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 652 times

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

Postby mayngontay » Fri May 30, 2008 2:03 am

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,


Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests