# how to set the width of a custom column type and make the first row / title of a threeparttable fit that particular width

I have the following table

I would like :

1. to fix a row to a certain width of the custom \newcolumntype{L}{D{.}{.}{2,5}} (which allows to align the decimal of the item of the column instead of the first character (which prevents the item of the column to be shifted to the right when there is a minus sign in front))
2. that the first row / title of a threeparttable fits that particular width. I.e. it means to be on several lines

In my example the title very long title first col and very long title second col title are not readable

I guess that the following line has to be changed: \newcolumntype{L}{D{.}{.}{2,5}}

Here is the code:

\documentclass{article}
\usepackage{caption,booktabs,array}
\usepackage[flushleft]{threeparttable}
\newcommand{\rowgroup}[1]{\hspace{-1em}#1}
\usepackage{dcolumn}
\usepackage{geometry}
\geometry{
a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\newcolumntype{L}{D{.}{.}{2,5}}
\begin{document}
\pagenumbering{gobble}
\begin{table}
\centering
\begin{threeparttable}
\caption{Results of the test}
% \toprule
%& \multicolumn{1}{c}{} \\
%\toprule
\textbf{Variable}&\textbf{very long title first col}&\textbf{very long title second col}\\
\midrule
\rowgroup{\textbf{Tests}}\\
var1&0.014\pm0.00001&1.285\pm0.00001\\
var2&-26.0\pm0.000014&64.33\pm0.00001\\
\bottomrule
\end{tabular}
\begin{tablenotes}
\small
\item blabla
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}


EDIT

In order to try to answer the second question, I inserted a tabular inside a tabular

\documentclass{article}
\usepackage{caption,booktabs,array}
\usepackage[flushleft]{threeparttable}
\newcommand{\rowgroup}[1]{\hspace{-1em}#1}
\usepackage{dcolumn}
\usepackage{geometry}
\geometry{
a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\newcolumntype{L}{D{.}{.}{2,5}}
\begin{document}
\pagenumbering{gobble}
\begin{table}
\centering
\begin{threeparttable}
\caption{Results of the test}
% \toprule
%& \multicolumn{1}{c}{} \\
%\toprule
\textbf{Variable}& \begin{tabular}{cc}\textbf{very long } \\ \textbf{title} \\ \textbf{first col}\end{tabular}
& \begin{tabular}{cc}\textbf{very long } \\ \textbf{title} \\ \textbf{second col}\end{tabular}\\
\midrule
\rowgroup{\textbf{Tests}}\\
var1&0.014\pm0.00001&1.285\pm0.00001\\
var2&-26.0\pm0.000014&64.33\pm0.00001\\
\bottomrule
\end{tabular}
\begin{tablenotes}
\small
\item blabla
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}

• Just for curiosity - why do you have such long table headings? Would it be possible to write an abbreviation and give a long version of the abbreviation in the description? – Lars Halbauer Jul 16 at 13:54
• @LarsHalbauer: Indeed an Abbreviation would be an alternative. But, for the article I'm writing, I decided so far to go for the long heading version. As Latex highly customable is, it should be possible no? – ecjb Jul 16 at 13:59
• If you need a left aligned column, why don’t you use a p{<width>} column instead? – Lars Halbauer Jul 16 at 14:03
• because the command \newcolumntype{L}{D{.}{.}{2,5}} allows to align the decimal of the item of the column instead of the first character (which prevents the item of the column to be shifted to the right when there is a minus sign in front)) – ecjb Jul 16 at 14:06
• In addition to your question about the column headers, your columns themselves are not aligned properly as you specified 2,5 as the number format but enter numbers with their uncertainties into the same column. You might also want to correct that first. – leandriis Jul 16 at 16:21

Maybe the following can serve as a starting point:

\documentclass{article}
\usepackage{caption,booktabs,array}
\usepackage[flushleft]{threeparttable}
\newcommand{\rowgroup}[1]{\hspace{-1em}#1}
\usepackage{dcolumn}
\usepackage{geometry}
\geometry{
a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\newcolumntype{L}{D{.}{.}{1,6}}
\newcolumntype{A}{D{.}{.}{2,3}}
\begin{document}
\pagenumbering{gobble}
\begin{table}
\centering
\begin{threeparttable}
\caption{Results of the test}
\begin{tabular}{>{\quad}m{12em}A@{\,$$\pm$$\,}LA@{\,$$\pm$$\,}L}
\toprule
\textbf{Variable}& \multicolumn{2}{>{\centering\arraybackslash}p{2.25cm}}{\textbf{very long title first col}}
& \multicolumn{2}{>{\centering\arraybackslash}p{2.25cm}}{\textbf{very long title second col}}\\
\midrule
\rowgroup{\textbf{Tests}}\\
var1&0.014 & 0.00001 & 1.285 & 0.00001\\
var2&-26.0 & 0.000014 & 64.33 &  0.00001\\
\bottomrule
\end{tabular}
\begin{tablenotes}
\small
\item blabla
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}


Some more explanation as requested in the comments:

In order to align the numbers with respect to their decimal separator and the \pm symbol, I have split the values and their uncertainties into two columns that are both derived from the d type column.

Accoding to the dcolumn manual, the D type column accepts the following three arguments: D{<sep.tex>}{<sep.dvi>}{<decimal places>} with <sep.tex> being the separator used in the tex file, <sep.dvi> being the separator used in the output and <decimal places> being the maximum number of decimal places in the corresponding column.

As an example: 64.33 has two places before and after the separator. This would correspond to {2,2}. 0.000014 on the other hand has 1 place before and 6 places after the separator, which would correspond to {1,6}. In order to determine the correct value for <decimal places>, therefore search for the largest number of places before and after the decimal separator. If both of the bove mentioned numbers would be part of the same column, the correct value would thuse be {2,6}.

@{\,$$\pm$$\,}, that is used between the value and the uncertainty column specifier, automatically inserts a \pm inbetween the two adjacent columns. (\, is a small horizontal space)

• Many thanks for your answer @leandriis. That helped a lot. Do you know a good reference to start with in order to try to understand the rather (for me at least) cryptic {>{\quad}m{12em}A@{\,$$\pm$$\,}LA@{\,$$\pm$$\,}L. Moreover, regarding the \newcolumn command I looked at tex.stackexchange.com/questions/257128/… but couldn't find the understanding of the structure your wrote: \newcolumntype{L}{D{.}{.}{1,6}}. Do you know a good other reference for this \newcolumntype. Many thanks again – ecjb Jul 17 at 6:48
• One last point @leandriis: in the code you posted, is it possible to set manually the width of the column of the type \newcolumntype{L}{D{.}{.}{2,5}} \newcolumntype{L}{D{.}{.}{1,6}} \newcolumntype{A}{D{.}{.}{2,3}} ? That would be very helpful! – ecjb Jul 17 at 7:03
• @ecjb: I have added some explanation on the usage of the d type colum as well as on @{}`. I hope I have answered your questions with that. If something remains unclear, do not hesitate to ask. – leandriis Jul 17 at 17:27