4

I am trying to nest a tabularx table into a cell of another tabularx table. I did it according to the Question here. Now my problem is, that they use the \newcolumntype{Y}{>{\centering\arraybackslash}X} column type, which does not allow to specify the width of the columns.

My definitions look like this:

\newcolumntype{R}[1]{>{\raggedleft\hsize=#1\hsize\arraybackslash}X}%
\newcolumntype{L}[1]{>{\raggedright\hsize=#1\hsize\arraybackslash}X}%
\newcolumntype{C}[1]{>{\centering\hsize=#1\hsize\arraybackslash}X}

If I use one of them, the nested table will overflow the cell. Do you have any idea how I could fix this?

MWE:

\documentclass[10pt,notitlepage]{article}%
\usepackage{tabularx}
\usepackage{hyperref}
\usepackage{array}
\usepackage{minted}
\newcolumntype{L}[1]{>{\raggedright\hsize=#1\hsize\arraybackslash}X}
\begin{document}

 \begin{table}[H]
  \centering
  \begin{tabularx}{\linewidth}{L{0.6}L{1.4}}
    \toprule
      \textbf{Command} & \textbf{Description} \\
    \midrule
      \mintinline{console}{pwd} & Print name of current working directory test trest \\
      \mintinline{console}{cd} & {
        Change directory
        \newline
        \begin{tabularx}{\cellwidth}{L{0.8}L{1.2}}
          \toprule
          \mintinline{console}{~} & current users home directory \\
          \mintinline{console}{~bob} & bobs' home direcotry \\
          \mintinline{console}{-} & previous directory \\
          \bottomrule
        \end{tabularx}
        } \\
    \bottomrule
  \end{tabularx}
  \caption{Basics}
\end{table}

\end{document}

Now it looks like this: The table

How can I extend the nested table to the edge of the cell?

7
  • 2
    Beware: the total of the \hizes must equal the number of X columns, so that, for instance, in the nested tabularx, the coefficients should be 0.6 and 1.4.
    – Bernard
    Commented Mar 22, 2019 at 10:54
  • @Bernard ah thanks for the hint. So I did it wrong all those years... The second table is now contained in the cell. But now it does not fill to the edge.
    – Pascal
    Commented Mar 22, 2019 at 10:58
  • I must say the documentation for this point needs to be read carefully. B.t.w., also, in your outer tabularx, L{0.5}L{0.5} is simply XX.
    – Bernard
    Commented Mar 22, 2019 at 11:02
  • How is the \mintinline macro defined?
    – Mico
    Commented Mar 22, 2019 at 11:43
  • its in the \usepackage{minted} package
    – Pascal
    Commented Mar 22, 2019 at 11:50

1 Answer 1

7

Here are three different possibilities how you can achieve the desired layout. Please note that I have removed the \mintinline commands as the issue is reproducible without them.

In the first table, I have replaced the undefined \cellwidth with 1.4\linewidth+\tabcolsep. In this case, 1.4 equals the option of the second column of the surrounding tabularx.

In the second table, I have used just one variable width column per tabularx as the left columns of both the outer and the inner table don't seem to contain content that needs to be automatically broken over several lines.

In the third table, I have completely removed the inner tabularx and used \multicolumn commands instead.

\documentclass[10pt,notitlepage]{article}%
\usepackage{tabularx}
\usepackage{hyperref}
\usepackage{array}
\usepackage{booktabs}

\usepackage{calc}
\newcolumntype{L}[1]{>{\raggedright\hsize=#1\hsize\arraybackslash}X}

\begin{document}

 \begin{table}
  \centering
  \begin{tabularx}{\linewidth}{L{0.6}L{1.4}}
    \toprule
      \textbf{Command} & \textbf{Description} \\
    \midrule
      \texttt{pwd} & Print name of current working directory test trest \\
      \texttt{cd} & {
        Change directory
        \newline
        \begin{tabularx}{1.4\linewidth+\tabcolsep}{L{0.8}L{1.2}}
          \toprule
          \texttt{\~} & current users home directory \\
          \texttt{bob} & bobs' home direcotry \\
          \texttt{-} & previous directory \\
          \bottomrule
        \end{tabularx}
        } \\
    \bottomrule
  \end{tabularx}
  \caption{Basics}
\end{table}

 \begin{table}
  \centering
  \begin{tabularx}{\linewidth}{lX}
    \toprule
      \textbf{Command} & \textbf{Description} \\
    \midrule
      \texttt{pwd} & Print name of current working directory test trest \\
      \texttt{cd} & {
        Change directory
        \newline
        \begin{tabularx}{\linewidth+\tabcolsep}{lX}
          \toprule
          \texttt{\~} & current users home directory \\
          \texttt{bob} & bobs' home direcotry \\
          \texttt{-} & previous directory \\
          \bottomrule
        \end{tabularx}
        } \\
    \bottomrule
  \end{tabularx}
  \caption{Basics}
\end{table}

 \begin{table}
  \centering
  \begin{tabularx}{\linewidth}{llX}
    \toprule
      \textbf{Command} & \multicolumn{2}{X}{\textbf{Description}} \\
    \midrule
      \texttt{pwd} & \multicolumn{2}{X}{Print name of current working directory test trest }\\
      \texttt{cd}  & \multicolumn{2}{X}{Change directory}\\
    \cmidrule{2-3}
                   & \texttt{\~}     & current users home directory \\
                   &  \texttt{bob}   & bobs' home direcotry \\
                   & \texttt{-}      & previous directory \\
    \cmidrule{2-3}
    \bottomrule
  \end{tabularx}
  \caption{Basics}
\end{table}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .