Is there any solution in LaTeX tables that implements similar functionality to MSWord's AutoFit?
[Tex/LaTex] Auto-fit table column widths based on content
automationcolumnstableswidth
Related Solutions
You don't need tabularx
if you don't use its X
columns which adjust their width according to the available space.
You probably just need a new column type M
defined in this way:
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
You don't also need to use different rows for long cells, since they are wrapped into new lines automatically.
MWE:
\documentclass[twoside]{report}
\usepackage{amsfonts}
\usepackage{tabularx, booktabs}
\usepackage{siunitx}
\usepackage{array}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\begin{table}[h!]
\centering
\begin{tabular}{M{1.7cm}M{1.5cm}M{1.5cm}M{2.5cm}M{2cm}}
\toprule
Modelling Noise RMS & Reference Input Noise & $\mu_{s}$ & Modelling Time Duration & Outcome \\
\midrule\\ [0.5ex]
Optimal (0.3)& & 0.5 & Stopped once converged & Converged \\ [2ex]
0.05 & & 0.25 & $n=10000$ & Failed to converge \\ [2ex]
0.1 & & 0.25 & $n=10000$ & Destabilised \\ [2ex]
0.15 & & 0.25 & $n=10000$ & Destabilised \\ [2ex]
0.2 & & 0.25 & $n=10000$ & Destabilised \\ [2ex]
0.25 & & 0.25 & $n=10000$ & Destabilised \\ [2ex]
0.3 & & 0.25 & $n=10000$ & Destabilised \\ [2ex]
0.35 & & 0.25 & $n=10000$ & Destabilised \\ [2ex]
0.4 & & 0.25 & $n=10000$ & Destabilised \\ [2ex]
0.45 & & 0.25 & $n=10000$ & Destabilised \\ [2ex]
0.5 & & 0.25 & $n=10000$ & Destabilised \\ [0.5ex]\\ \bottomrule
\end{tabular}
\end{table}
\end{document}
Output
As a side note, 15pt
is not a valid option for the report
class.
The original problem: The long entry of the first column was split in two lines in two table rows. The entry of the second column was then put in the second row. Instead, you want it in the first row:
Start of long text ... & Reference \\
long text continued & \\
However, this can be solved in a more elegant way with an X
column of package tabularx
, then the first column gets all the available space and the entry is broken across lines automatically, see the answer of Zarko. The following example does not use \raggedright
, because it seems that the column width is so large, that justified text should work without much trouble. Also I have indented the following lines to make it easier for the reader to find the next table row. Alternatively some vertical space can be added to separate the multi-line entries.
The next problem are the lines, which do not look too good; therefore the nicer lines of package booktabs
are used.
\documentclass[a4paper,11pt]{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
\begin{table}
\centering
\footnotesize
\begin{tabularx}{\linewidth}{>{\hangindent=1em\hangafter=1 }Xl}
\toprule
Example & Reference \\
\midrule
Northern Wheatears had higher reproductive success in higher vegetation
height & ArltPart2007 \\
Mallards that avoided wetlands with large expanses of open water had
higher reproductive success & BloomEtAl2013 \\
American Redstarts that occupied wet forest habitat in the winter raised
more offspring than conspecifics occupying other habitats in the winter &
NorrisEtAl2003 \\
Great Tits nesting in mature woodland produced larger broods than
conspecifics nesting in gardens and hedgerows & RiddingtonGosler1995 \\
Reduction in winter stubble has led to a reduction in Reed Bunting
survival rate & PeachEtAl1999 \\
Fledgling Ovenbird survival increased with vegetation structure &
KingEtAl2006 \\
Daily nest survival of eight forest species was positively related to
nest distance from the forest edge and nest height & NewmarkEtAl2011 \\
\bottomrule
\end{tabularx}
\caption{Examples of the effects on habitat on individual fitness.}
\label{table_fitness}
\end{table}
\begin{table}
\centering
\footnotesize
\begin{tabularx}{\linewidth}{Xl}
\toprule
Example & Reference \\
\midrule
Northern Wheatears had higher reproductive success in higher vegetation
height & ArltPart2007 \\
\addlinespace
Mallards that avoided wetlands with large expanses of open water had
higher reproductive success & BloomEtAl2013 \\
\addlinespace
American Redstarts that occupied wet forest habitat in the winter raised
more offspring than conspecifics occupying other habitats in the winter &
NorrisEtAl2003 \\
\addlinespace
Great Tits nesting in mature woodland produced larger broods than
conspecifics nesting in gardens and hedgerows & RiddingtonGosler1995 \\
\addlinespace
Reduction in winter stubble has led to a reduction in Reed Bunting
survival rate & PeachEtAl1999 \\
\addlinespace
Fledgling Ovenbird survival increased with vegetation structure &
KingEtAl2006 \\
\addlinespace
Daily nest survival of eight forest species was positively related to
nest distance from the forest edge and nest height & NewmarkEtAl2011 \\
\bottomrule
\end{tabularx}
\caption{Examples of the effects on habitat on individual fitness.}
\label{table_fitness_addlinespace}
\end{table}
\end{document}
Best Answer
Have a look at the
tabulary
package. Its description reads:In my example, I also use the
ragged2e
package to allow for hyphenation within the table cells.(Fixed a bug in an earlier version of this post that had a missing '\'.)