50

I have a table of maths as below, where I've used the p{} attribute to allow the list of partitions to wrap inside the cell. The problem is that it also justifies the column, which looks very strange for the rows 6-10 because you get huge gaps. What I want is the list of partitions to be left-justified with standard spacing, like in the first few rows, but still able to spill over onto multiple rows.

\usepackage{amsmath}

\begin{document}

\begin{table}
\renewcommand{\arraystretch}{1.3}
 \begin{center}
 \label{pntable}
 \begin{tabular}{|c|p{6.5cm}|c|}
 \hline
 \textbf{Integer} & \textbf{Partitions} & \textbf{Number of partitions} \\ \hline
 1 & 1 & 1 \\ \hline
 2 & 2, 1+1 & 2 \\ \hline
 3 & 3, 2+1, 1+1+1 & 3 \\ \hline
 4 & 4, 3+1, 2+2, 2+1+1, 1+1+1+1 & 5 \\ \hline
 5 & 5, 4+1, 3+2, 3+1+1, 2+2+1, 2+1+1+1, 1+1+1+1+1 & 7 \\ \hline
 6 & 6, 5+1, 4+2, 4+1+1, 3+3, 3+2+1, 3+1+1+1, 2+2+2, 2+2+1+1, 2+1+1+1+1, 1+1+1+1+1+1 & 11 \\ \hline
 7 & 7, 6+1, 5+2, 5+1+1, 4+3, 4+2+1, 4+1+1+1, 3+3+1, 3+2+2, 3+2+1+1, 3+1+1+1+1, 2+2+2+1, 2+2+1+1+1, 2+1+1+1+1+1, 1+1+1+1+1+1+1 & 15 \\ \hline
 8 & 8, 7+1, 6+2, 6+1+1, 5+3, 5+2+1, 5+1+1+1, 4+4, 4+3+1, 4+2+2, 4+2+1+1, 4+1+1+1+1, 3+3+2, 3+3+1+1, 3+2+2+1, 3+2+1+1+1, 3+1+1+1+1+1, 2+2+2+2, 2+2+2+1+1, 2+2+1+1+1+1, 2+1+1+1+1+1+1, 1+1+1+1+1+1+1+1 & 22 \\ \hline
 9 & 9, 8+1, 7+2, 7+1+1, 6+3, 6+2+1, 6+1+1+1, 5+4, 5+3+1, 5+2+2, 5+2+1+1, 5+1+1+1+1, 4+4+1, 4+3+2, 4+3+1+1, 4+2+2+1, 4+2+1+1+1, 4+1+1+1+1+1, 3+3+3, 3+3+2+1, 3+3+1+1+1, 3+2+2+2, 3+2+2+1+1, 3+2+1+1+1+1, 3+1+1+1+1+1+1, 2+2+2+2+1, 2+2+2+1+1+1, 2+2+1+1+1+1+1, 2+1+1+1+1+1+1+1, 1+1+1+1+1+1+1+1+1 & 30 \\ \hline
 10 & 10, 9+1, 8+2, 8+1+1, 7+3, 7+2+1, 7+1+1+1, 6+4, 6+3+1, 6+2+2, 6+2+1+1, 6+1+1+1+1, 5+5, 5+4+1, 5+3+2, 5+3+1+1, 5+2+2+1, 5+2+1+1+1, 5+1+1+1+1+1, 4+4+2, 4+4+1+1, 4+3+3, 4+3+2+1, 4+3+1+1+1, 4+2+2+2, 4+2+2+1+1, 4+2+1+1+1+1, 4+1+1+1+1+1+1, 3+3+3+1, 3+3+2+2, 3+3+2+1+1, 3+3+1+1+1+1, 3+2+2+2+1, 3+2+2+1+1+1, 3+2+1+1+1+1+1, 3+1+1+1+1+1+1+1, 2+2+2+2+2, 2+2+2+2+1+1, 2+2+2+1+1+1+1, 2+2+1+1+1+1+1+1, 2+1+1+1+1+1+1+1+1, 1+1+1+1+1+1+1+1+1+1 & 42 \\ \hline
 \end{tabular}
 \end{center}
 \caption{All the partitions of $n \in \{1,\ldots,10\}$}
\end{table}

\end{document}

Apologies in advance because I'm sure this will have been answered before, but I can't find it anywhere and really need some help!

Werner
  • 603,163
Michael
  • 501
  • 1
  • 4
  • 3

3 Answers3

38

use the array package then you can use

\begin{tabular}{|c|>{\raggedright}p{6.5cm}|c|}
David Carlisle
  • 757,742
  • 1
    is this solution works for math mode only? I got error with message "extra alignment tab has been changed to \cr" and also ugly pdf result. – sugab Jan 22 '20 at 10:36
  • 4
    @bagustris if you do that in the final column use \raggedright\arraybackslash or use \tabularnewline not \\ to end the table row. – David Carlisle Jan 22 '20 at 10:53
  • For whatever reason it had to be upper case: \RaggedRight, but cannot say why. (?) – User Rebo Dec 29 '23 at 11:50
  • 2
    @UserRebo no the answer works as described. \RaggedRight (from ragged2e) does not give as much raggedness so will not work in all cases. – David Carlisle Dec 29 '23 at 12:57
21

Since the second column has math expressions, you can use the >{...}, <{...} construct from the array package, not only to get the left justification, but to change to math mode inside this column (it is not the same thing to write 3+4 in text mode than $3+4$). Your table format definition could be something like

\begin{tabular}{c>{\raggedright\arraybackslash$}p{6.5cm}<{$}c}

As a side note, I would suggest you to use the booktabs package to build your tables. Here's an example of your table:

\documentclass[a4paper,twoside]{book}

\usepackage{amsmath}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\begin{table}
 \centering
 \begin{tabular}{c>{\raggedright\arraybackslash$}p{6.5cm}<{$}c}
 \toprule
 \textbf{Integer} & \textbf{Partitions} & \textbf{Number of} \\
 & &  \textbf{partitions} \\ \midrule
 1 & 1 & 1 \\ \midrule
 2 & 2, 1+1 & 2 \\ \midrule
 3 & 3, 2+1, 1+1+1 & 3 \\ \midrule
 4 & 4, 3+1, 2+2, 2+1+1, 1+1+1+1 & 5 \\ \midrule
 5 & 5, 4+1, 3+2, 3+1+1, 2+2+1, 2+1+1+1, 1+1+1+1+1 & 7 \\ \midrule
 6 & 6, 5+1, 4+2, 4+1+1, 3+3, 3+2+1, 3+1+1+1, 2+2+2, 2+2+1+1, 2+1+1+1+1, 1+1+1+1+1+1 & 11 \\ \midrule
 7 & 7, 6+1, 5+2, 5+1+1, 4+3, 4+2+1, 4+1+1+1, 3+3+1, 3+2+2, 3+2+1+1, 3+1+1+1+1, 2+2+2+1, 2+2+1+1+1, 2+1+1+1+1+1, 1+1+1+1+1+1+1 & 15 \\ \midrule
 8 & 8, 7+1, 6+2, 6+1+1, 5+3, 5+2+1, 5+1+1+1, 4+4, 4+3+1, 4+2+2, 4+2+1+1, 4+1+1+1+1, 3+3+2, 3+3+1+1, 3+2+2+1, 3+2+1+1+1, 3+1+1+1+1+1, 2+2+2+2, 2+2+2+1+1, 2+2+1+1+1+1, 2+1+1+1+1+1+1, 1+1+1+1+1+1+1+1 & 22 \\ \midrule
 9 & 9, 8+1, 7+2, 7+1+1, 6+3, 6+2+1, 6+1+1+1, 5+4, 5+3+1, 5+2+2, 5+2+1+1, 5+1+1+1+1, 4+4+1, 4+3+2, 4+3+1+1, 4+2+2+1, 4+2+1+1+1, 4+1+1+1+1+1, 3+3+3, 3+3+2+1, 3+3+1+1+1, 3+2+2+2, 3+2+2+1+1, 3+2+1+1+1+1, 3+1+1+1+1+1+1, 2+2+2+2+1, 2+2+2+1+1+1, 2+2+1+1+1+1+1, 2+1+1+1+1+1+1+1, 1+1+1+1+1+1+1+1+1 & 30 \\ \midrule
 10 & 10, 9+1, 8+2, 8+1+1, 7+3, 7+2+1, 7+1+1+1, 6+4, 6+3+1, 6+2+2, 6+2+1+1, 6+1+1+1+1, 5+5, 5+4+1, 5+3+2, 5+3+1+1, 5+2+2+1, 5+2+1+1+1, 5+1+1+1+1+1, 4+4+2, 4+4+1+1, 4+3+3, 4+3+2+1, 4+3+1+1+1, 4+2+2+2, 4+2+2+1+1, 4+2+1+1+1+1, 4+1+1+1+1+1+1, 3+3+3+1, 3+3+2+2, 3+3+2+1+1, 3+3+1+1+1+1, 3+2+2+2+1, 3+2+2+1+1+1, 3+2+1+1+1+1+1, 3+1+1+1+1+1+1+1, 2+2+2+2+2, 2+2+2+2+1+1, 2+2+2+1+1+1+1, 2+2+1+1+1+1+1+1, 2+1+1+1+1+1+1+1+1, 1+1+1+1+1+1+1+1+1+1 & 42 \\ \bottomrule
 \end{tabular}
 \caption{All the partitions of $n \in \{1,\ldots,10\}$}
 \label{pntable}
\end{table}

\end{document}

enter image description here

Moriambar
  • 11,466
Gonzalo Medina
  • 505,128
-2

My answer is to used {p} in \begin{tabular} to left-justify the text:

\documentclass[11pt]{report}
\usepackage{amsmath,array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\begin{table}[]
\caption{Comparisons of different PVT systems, presented in 2014.}
\begin{center}
\begin{tabular}{|C{1cm}|p{6cm}|C{3cm}|C{3cm}|}
 %\hline
% \multicolumn{4}{|c|}{\textbf{Photovoltaic Thermal (PVT) System}}\\
 \hline 
 \textbf{Sr No.} & \textbf{PVT Designs} &\textbf{Average Electrical efficiency \%} &\textbf{Average Thermal efficiency \%}\\
 \hline
1& Experimental study of heat pipe PVT system in China \cite{44}. &16.7 &23.5 .\\ \hline
2& Experimental study of new hybrid solar collector PVT system in China \cite{45}. &27 &5\\ \hline
3& Experimental performance in PVT/ air collector in South Korea \cite{46}. &15 &22 \\ \hline
4& Study of Sheet and tube design in Algeria  \cite{47}.&- &22\\ \hline
\end{tabular}
\end{center}
\end{table}
\end{document}

It works for me.

  • 3
    This does not do a ragged left, but rather a paragraph style, which can cause awkward spacing for small paragraphs. – Dr Xorile Jun 24 '18 at 16:35