My problem is that I have many tables in my document with the same layout, and I want to specify the maximum width of a specific column to be 5 cm. I tried with p{5cm} but it make the column fix 5 cm wide and it looks odd with only a short word in it. I want it to be as narrow as possible, and split it into two lines, if it's wider than 5 cm. Is there any solution for my problem?
[Tex/LaTex] How to set a maximum column width
tablesvarwidth
Related Solutions
Note that the word "Computer" takes up almost as much space as does the word "Television". Rather than fiddle with one column width at a time in an effort to make the table look OK, I'd use the tabularx
environment to assure that all five columns have the same width.
\documentclass{beamer}
\usepackage{tabularx} % for 'tabularx' environment
\useapackage{ragged2e} % for \Centering macro
\newcolumntype{C}{>{\Centering\arraybackslash}X}
\begin{document}
\begin{frame}
\begin{tabularx}{\textwidth}{| C | C | C | C | C |}
Television & Radio & Washer & Dryer & Computer \\
\end{tabularx}
\end{frame}
\end{document}
I would do the following changes:
- use
tabularx
with prescribed width of one column for table environment - reduce
\tabcolsep
to half (3pt) - reduce font size to
\small
- use
multirow
for header of second column - define new command for shortness of
\multicolumn
- the first column define as math environment
- use rules from
booktabs
packages - use
threeparttable
for table notes
MWE:
\documentclass[twocolumn]{article}
\usepackage{booktabs, multirow, tabularx, threeparttable}
\newcommand\mcc[1]{\multicolumn{2}{c}{#1}}
\usepackage{amsmath}
\usepackage{lipsum}% for dummy text
\begin{document}
\lipsum*[2]
\begin{table}[htb]
\begin{threeparttable}[htb]
\caption{My caption}
\label{my-label}
\small%footnotesize
\setlength\tabcolsep{3pt}
\begin{tabularx}{\linewidth}{@{}>{$}l<{$} >{\centering}X cc cc@{}}
\toprule
& \multirow{2}{=}{\centering
NV\tnote{a} \\
(LV\tnote{b})}
& \mcc{Calibration} & \mcc{Validation} \\
\cmidrule(lr){3-4}\cmidrule(lr){5-6}
\text{Pre-processing} & & RMSE & R$^2$ & RMSE & R$^2$\\
\midrule
R(\lambda) & X(X) & X & X & X & X \\
D^1(R(\lambda)) & X(X) & X & X & X & X \\
D^2(R(\lambda)) & X(X) & X & X & X & X \\
-log(R(\lambda)) & X(X) & X & X & X & X \\
-log(D^1(R(\lambda)) & X(X) & X & X & X & X \\
-log(D^2(R(\lambda)) & X(X) & X & X & X & X \\
D^1(-log(R(\lambda))) & X(X) & X & X & X & X \\
D^2(-log(R(\lambda))) & X(X) & X & X & X & X \\
MSC(R(\lambda)) & X(X) & X & X & X & X \\
MSC(D^1(R(\lambda))) & X(X) & X & X & X & X \\
MSC(D^2(R(\lambda))) & X(X) & X & X & X & X \\
MSC(-log(R(\lambda))) & X(X) & X & X & X & X \\
MSC(D^1(-log(R(\lambda)))) & X(X) & X & X & X & X \\
MSC(-log(D^1(R(\lambda)))) & X(X) & X & X & X & X \\
MSC(D^2(-log(R(\lambda)))) & X(X) & X & X & X & X \\
MSC(-log(D^2(R(\lambda)))) & X(X) & X & X & X & X \\
SNV(R(\lambda)) & X(X) & X & X & X & X \\
SNV(D^1(R(\lambda))) & X(X) & X & X & X & X \\
SNV(D^2(R(\lambda))) & X(X) & X & X & X & X \\
SNV(-log(R(\lambda))) & X(X) & X & X & X & X \\
SNV(D^1(-log(R(\lambda)))) & X(X) & X & X & X & X \\
SNV(-log(D^1(R(\lambda)))) & X(X) & X & X & X & X \\
SNV(D^2(-log(R(\lambda)))) & X(X) & X & X & X & X \\
SNV(-log(D^2(R(\lambda)))) & X(X) & X & X & X & X \\
\text{Multiple simultaneous}& X(X) & X & X & X & X \\
\bottomrule
\end{tabularx}
\begin{tablenotes}[para, flushleft]\itshape
\item[a] Number of variables active in final variable selection,
\item[b] optimal number of latent variables
in PLS regression model.
\end{tablenotes}
\end{threeparttable}
\end{table}
\lipsum
\end{document}
Best Answer
You can do it with
varwidth
:Which compiles to:
Thank-you for inspiring me to look for this answer. It is a nice question!