45

Is there any way to change an alignment in the column if it is set by p{length} in tabular preamble? I want a right aligned column of fixed length.

Same question about a centered column of fixed length.

[Update 12 Sep 2023 when the system indicates there is a duplicate] There is a similar question here. However, that question contains the answer in the question itself. If I knew that the solution is about adding the '\raggedright' I wouldn't ask this question. I believe, My question benefit those who are new in LaTeX and not experience in the entire LaTex arsenal.

1 Answers1

48

Insert one of the commands \raggedright, \centering, or \raggedleft into the parbox depending on whether the paragraph should be typeset flush left, centered, or flush right.

\documentclass{article}
\usepackage{array}
\usepackage{lipsum}
\begin{document}
\noindent
\begin{tabular}%
  {>{\raggedright\arraybackslash}p{3.5cm}%
   >{\centering\arraybackslash}p{3.5cm}%
   >{\raggedleft\arraybackslash}p{3.5cm}%
  }
  \lipsum[1] & \lipsum[2] & \lipsum[3]
\end{tabular}
\end{document}

enter image description here

gernot
  • 49,614
  • Is it possible to start it not from the first column? Say, I want the title of the column to be left aligned, but the content to be right aligned. (Actually, I have pictures in the boxes of the first column and I am trying to understand how to align them better. Your solution works and thanks for that, but I don't want it influence the first row) (For pictures, you can have a look to http://tex.stackexchange.com/questions/337934/vertical-alignment-in-cells-text-and-pic-combination-in-one-table). – Sergey Belyaev Nov 07 '16 at 19:06
  • 1
    @SergeyBelyaev Use \multicolomn{1}{l}{Title} in the title row to get "Title" left aligned. – Dan Nov 07 '16 at 19:13
  • This solution worked beautifully for me – Shawn Jul 30 '20 at 17:37
  • 1
    Could you explain what's the role of arraybackslash ? I've tried removing this part and haven't observed any difference. – Konrad Jan 31 '22 at 22:37
  • 2
    @Konrad Try to add a second line to the tabular. Without \arraybackslash in the right column you will get the error Extra alignment tab has been changed to \cr. \begin{tabular} defines \\ as row separator, but \raggedleft redefines it and so would break the table. \arraybackslash undoes this redefinition. – gernot Feb 01 '22 at 10:19
  • This interacts badly with \newline; try the content This spans \newline two lines & This spans \newline three lines \newline or so & This spans \newline four lines \newline which is \newline even more. – Eric Jul 07 '22 at 15:56