[Tex/LaTex] Fixed column-width table with text left-aligned in cells

horizontal alignmenttables

\section{Section Title} %label{8.4.47}
\subsection*{Some note on Section}
\begin{footnotesize}
\begin{tabular}{p{2cm}|p{4cm}|p{3cm}|p{3cm}}
Heading 1 & Heading 2 & Heading 3  & Heading 4 \\
\hline
Text 1  & Text 2 & A\textsuperscript {6/1} B\textsuperscript {5/1} C\textsuperscript {1/2}  D\textsuperscript {7/1} (E + F)\textsuperscript{8.4.46} (G + H)\textsuperscript{8.4.45} Some more text\textsuperscript{6.1.72} \\
\end{tabular}
\end{footnotesize}

In the above table |p{3cm} is making the columns justified. I get some unwanted space between the characters, is there a way to make it align to the left, if the text does not fit in a line either put a "-" and continue to next line or simply start from a new line.

Also can I post my code with Unicode characters?

Best Answer

You could use the array package and let it insert \raggedright commands. A comfortable way is to define a new column type:

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

Now just use P instead of p to get left justified p columns.

Instead of \raggedright, you could also use the enhanced command \RaggedRight of the ragged2e package, which allows hyphenation. Further \arraybackslash, which restores the behavior of \ (changed by \raggedright), is no longer needed.

So, my preferred way would be

\usepackage{array}
\usepackage{ragged2e}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}

I'm further inserting \hspace{0pt} because otherwise there could be hyphenation problems: TeX doesn't hyphenate the first word in a box. Inserting zero space works around that.

Related Question