55
\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?

1
  • 1
    You can post on this site in Unicode just fine: şéê? If you want to know about Unicode in TeX, that's also possible; there have been a number of questions here about doing just that. And in general, if you have a specific question about this site, you should ask it over at meta. Commented Dec 18, 2010 at 18:48

2 Answers 2

75

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.

4
  • I am very new to Tex and still do not know how to implement your solution.
    – Aku
    Commented Dec 18, 2010 at 17:42
  • 3
    @Aku: just add my code lines to your preamble (either the 2 line at the beginning or the variant with 2 lines). Then change your tabular definition to use the capital P: \begin{tabular}{P{2cm}|P{4cm}|P{3cm}|P{3cm}}.
    – Stefan Kottwitz
    Commented Dec 18, 2010 at 17:48
  • 2
    Is it possible to make this work with tabularx as well? If I do exactly the same in a tabularx the text simply stays left aligned. I need a tabularx because I want my table to have a flexible column, maintaining exactly 100% text width overall.
    – bluenote10
    Commented Aug 9, 2015 at 15:13
  • @bluenote10, \raggedright aligns left. For right-aligning, with tabularx, you can redefine the X column: \newcolumntype{R}{>{\raggedleft\arraybackslash}X} (notice this uses \raggedleft) Commented Jul 15, 2020 at 8:57
12

The version 2.4f of array has introduced new types of columns: w{...}{...} and W{...}{...}. The first argument is l, c or r and the second is the width of the column. With W, you have a warning if the content of the cell is too wide.

\documentclass{article}
\usepackage{array}

\begin{document}

\begin{tabular}{|wl{2cm}|wr{3cm}|}
\hline
qsdf & poiu \\
\hline
pqsdg aer & pou azer \\
\hline
\end{tabular}

\end{document}

Output of the above code

3
  • This is the answer!
    – kostrykin
    Commented May 3, 2021 at 16:24
  • 1
    How can I impose text wrapping in such a column?
    – kapple
    Commented Nov 10, 2021 at 11:08
  • @kapple: Use standard column types p, m, b of the package array. Commented Nov 10, 2021 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .