Graphics, Figures & TablesText alignment in a table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
fiatlux
Posts: 2
Joined: Fri Aug 27, 2010 6:49 pm

Text alignment in a table

Post by fiatlux »

Hello everyone,
First of all, sorry for my English.
I'm a beginner with Latex. I have a question. I've set the left margin of my document to 2cm. Then I created this table:

Code: Select all

\begin{tabular}{ p{5.5cm} p{4cm} } 
	one & two \\
	three & four \\
\end{tabular}
It works perfectly, but the text in the first column seems to be aligned to the left, but not where the margin is. It looks like it's aligned on 2.5 or 3cm. Can anybody tell me why?

PS: then I assumed that I had to force the alignment to the left, so I replace the first line with this one:

Code: Select all

\begin{tabular}{ >{\raggedright}p{5.5cm} p{4cm} }
but it generates an error.... why?


Thanks in advance!!

Recommended reading 2024:

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

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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Text alignment in a table

Post by gmedina »

fiatlux wrote:...It works perfectly, but the text in the first column seems to be aligned to the left, but not where the margin is. It looks like it's aligned on 2.5 or 3cm. Can anybody tell me why?
It seems that you're not taking into account the indentation of the first line of paragraphs and the space between columns that LaTeX internally uses; the first additional space can be suppressed by using \noindent (to suppress the paragraph indention); the second additional space can be suppressed by using the @{} construct in the table format. Take a look at the following example:

Code: Select all

\documentclass{article}

\begin{document}

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text

\noindent\begin{tabular}{@{}p{5.5cm} p{4cm}@{}} 
   one & two \\
   three & four \\
\end{tabular}

\end{document}
fiatlux wrote:PS: then I assumed that I had to force the alignment to the left, so I replace the first line with this one:

Code: Select all

\begin{tabular}{ >{\raggedright}p{5.5cm} p{4cm} }
but it generates an error.... why?
Without complete, minimal and compilable code (in the sense of a minimal working example) all I can do is to guess (and I don't like that). Two possible causes come to my mind:

1) Perhaps you are not loading the array package (which implemements the >{} and <{} constructs).
2) Since \raggedright (and also \raggedleft and \centering) internally redefine the line changing command \\ you will have to (a) use \tabularnewline instead of the usual \\ to cange lines in the tabular environment or (b) to place \arraybackslash right after \raggedright.

An example:

Code: Select all

\documentclass{article}
\usepackage{array}

\begin{document}

\noindent\begin{tabular}{@{}>{\raggedright\arraybackslash}p{5.5cm} p{4cm}@{}} 
   one & two \\
   three & four \\
\end{tabular}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Text alignment in a table

Post by localghost »

fiatlux wrote:[…] then I assumed that I had to force the alignment to the left, so I replace the first line with this one:

Code: Select all

\begin{tabular}{ >{\raggedright}p{5.5cm} p{4cm} }
but it generates an error.... why? […]
General statements like »generates an error« are trivial thus not very helpful. gmedina already asked for a minimal example (including log file) that would clarify what you are doing [1]. And as he already mentioned, this construction will require at least the array package.
gmedina wrote:[…] Since \raggedright (and also \raggedleft and \centering) internally redefine the line changing command \\ you will have to (a) use \tabularnewline instead of the usual \\ to cange lines in the tabular environment or (b) to place \arraybackslash right after \raggedright. […]
This is only necessary if the last column is formatted this way.

Code: Select all

\documentclass{article}
\usepackage{array}

\begin{document}
  \noindent
  \begin{tabular}{@{}>{\raggedright}p{5.5cm}p{4cm}@{}}
    one & two \\
    three & four
  \end{tabular}
\end{document}
[1] View topic: Avoidable mistakes


Best regards and welcome to the board
Thorsten
fiatlux
Posts: 2
Joined: Fri Aug 27, 2010 6:49 pm

Re: Text alignment in a table

Post by fiatlux »

Thanks for the answers!
Indeed, I had forgotten the array package... :?
And sorry for the "generates an error" thing.
Post Reply