[Tex/LaTex] Subtable inside a table / Adjusting table column width on the fly

horizontal alignmentmultirowtables

I've been fiddling with the following table for a while, and have some minor issues.

\documentclass{article}
\usepackage{threeparttable}
\usepackage{multirow}
\begin{document}

\begin{table}[!ht]
  \caption{
  \textbf{Pheno file format}}
  \begin{threeparttable}
  \begin{tabular}{|l|l|c|}\hline\hline
\textbf{Pheno file headers} &   \multicolumn{2}{|c|}{\textbf{Notes}} \\ \hline\hline
sex                &   \multicolumn{2}{|l|}{The sex column is restricted to the following values.} \\ \hline\hline
                   &   \multicolumn{1}{|l|}{Sex}    & \multicolumn{1}{p{1cm}|}{Value} \\ \hline\hline
                   &   Male                         & \multicolumn{1}{p{1cm}|}{1}                               \\ \hline
                   &   \multicolumn{1}{|l|}{Female} & \multicolumn{1}{p{1cm}|}{2}    \\ \hline
                   &   \multicolumn{1}{|l|}{Missing}& \multicolumn{1}{|p{1cm}|}{-1}   \\ \hline
studyid            &   \multicolumn{2}{|l|}{\multirow{1}{*}{Description of the study the data belongs to - eg. 'Hapmap6.0'.}} \\
                   &   \multicolumn{2}{|l|}{If studyid is not given, 'unknown' is used.} \\ \hline
studyid            &   \multicolumn{2}{p{\dimexpr\textwidth-2\tabcolsep\relax}|}{Description of the study the data belongs to - eg. 'Hapmap6.0'.
                                       If studyid is not given, 'unknown' is used.} \\ \hline
%\cline{3-4}
  \end{tabular}
  \end{threeparttable}
\end{table}

\end{document}

Questions:

  1. So, I'm sort of embedding a subtable inside a table. I first had
    it as a separate table, but then I stuck it inside the main table.
    It is the table with headers Sex and Value. It would be nice if
    I could adjust the width of the third margin so that it was only as
    wide as necessary, and also if all the lines to the left of that
    table, starting with the double line under Sex/Value were not
    present. The other problem is that the numbers in the third column
    are not left justified. So I'd like it to look something like

     -----------------------------------------------------------------------------
    | Pheno file headers  |                    Notes                             |
    ==============================================================================
    | sex                 | The sex column is restricted to the following values |
    ==============================================================================
                          | Sex    |  Value |
                          | Male   |    1   |
                          | Female |    2   |
                          | Missing|   -1   |
    ------------------------------------------------------------------------------
    | Studyid             | Some stuff                                           |
    ------------------------------------------------------------------------------
    

    Can this be done without lots of complexity?

  2. I've got two examples of a cell (starting with studyid) across two rows and two columns. I think I prefer the one at the bottom. Is that the best way to do it? It certainly seems simpler than the one above it.

Best Answer

Perhaps a suggestion, but here is an alternative to your current set up. I've used tabularx to allow for variable-width tabular columns (via column type X), filling up to some fixed with using the tabularx environment. Additionally, booktabs provide a cleaner spread of tables vertically, while virtually restricting the use of vertical rules (due to the rule definitions). Either way, the tabular layout helps columnar alignment, which removes the requirement for using vertical rules anyway.

The interior tabular has been adjusted to provide a natural-width horizontal alignment, as suggested in your ASCII-art interpretation.

enter image description here

\documentclass{article}
\usepackage{threeparttable}% http://ctan.org/pkg/threeparttable
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\usepackage[labelfont=normal,font=bf]{caption}%http://ctan.org/pkg/caption
\captionsetup[table]{skip=0pt}

\begin{document}

\begin{table}[!ht]
  \caption{Pheno file format}
  \begin{threeparttable}
    \begin{tabularx}{\textwidth}{lX}
      \toprule
      \textbf{Pheno file headers} & \multicolumn{1}{c}{\textbf{Notes}} \\
      \midrule
      sex                         & The sex column is restricted to the following values: \\[\jot]
                                  & \begin{tabular}{lc}
                                      Sex     & Value \\ \midrule
                                      Male    & $\phantom{-}1$ \\
                                      Female  & $\phantom{-}2$ \\
                                      Missing & $-1$
                                    \end{tabular} \\[\jot]
      \midrule
      studyid                     & Description of the study the data belongs to - eg. \texttt{Hapmap6.0}. \\
                                  & If studyid is not given, \texttt{unknown} is used. \\
      \midrule
      studyid                     & Description of the study the data belongs to - eg. \texttt{Hapmap6.0}. \\
                                  & If studyid is not given, \texttt{unknown} is used. \\
      \bottomrule
    \end{tabularx}
  \end{threeparttable}
\end{table}

\end{document}

Using \phantom{-} leaves the correct amount of horizontal (and vertical) space for - without actually typesetting it. That allows for aligning the entries within the nested tabular while also centering it.

\jot is a length of 3pt. Using it together with \\, as in \\[\jot], pushes the tabular line break down vertically by 3pt. Using a length register (like \jot) is preferred over explicitly using \\[3pt], since you can modify \jot (using, say \setlength{\jot}{5pt}) in one location and it would take effect wherever it is used after the redefinition, rather than having to manually modify the 3pt vertical skip everywhere.

Layout changes to the nested/interior tabular is possible (for example, if you want it horizontally centered within the outer tabularx, although the existing alignment seems sufficient).

Finally, there's no need to use multirow anymore, more obvious reasons. In my opinion, the input code is simpler and easier to follow.