1

I want to nest 2 tabularx tables: the columns of the "inside" table belong to a column of the "outside" table and therefore shall be indented and have a vertical line on their side.

I tried to do that with the following MWE, but I wish that the second column of the inserted table would be aligned with the "outside" table, so h and j shall be horizontally aligned with b and d.

\documentclass[11pt, a4paper]{scrartcl}
\usepackage{tabularx}
\begin{document}

\begin{tabularx}{\textwidth}{p{3cm} X}
\hline
a & b\\
c & d\\
\multicolumn{2}{@{\quad}|l}{
\begin{minipage}{\textwidth}
\begin{tabularx}{\textwidth}{p{3cm} X}
g & h \\
i & j \\
\end{tabularx}
\end{minipage}
}\\
e & f \\
\hline
\end{tabularx}

\end{document}

enter image description here

2 Answers 2

3

There's probably more than one way to achieve what you're trying to get, but I think the easiest solution is to reduce the width of the p column in the "inner" tabularx group by the combined widths of \quad (1 em) and \tabcolsep. The following MWE does this:

\documentclass[11pt,a4paper]{scrartcl}
\usepackage{tabularx,calc}
\begin{document}

\begin{tabularx}{\textwidth}{p{3cm} X}
\hline
a & b\\    c & d\\    \multicolumn{2}{@{\quad}|l}{
   \begin{minipage}{\textwidth}
      \begin{tabularx}{\textwidth}{p{3cm-1em-\tabcolsep} X}
         g & h \\             i & j \\          \end{tabularx}
   \end{minipage} }\\    e & f \\    \hline
\end{tabularx}
\end{document}

enter image description here

4

You need to modify the width of the first column in your "inner tabularx" to accommodate for the tabular column separation as well as your indentation using \quad. This length arithmetic is made possible by the calc package:

enter image description here

\documentclass{article}
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\usepackage{calc}% http://ctan.org/pkg/calc
\begin{document}
\noindent\begin{tabularx}{\textwidth}{p{3cm} X}
  \hline
  a & b\\
  c & d\\
  \multicolumn{2}{@{\quad}|l}{
    \begin{minipage}{\linewidth}
      \begin{tabularx}{\linewidth}{p{3cm-\widthof{\quad}-\tabcolsep} X}
        g & h \\
        i & j \\
      \end{tabularx}
    \end{minipage}
  }\\
  e & f \\
  \hline
\end{tabularx}
\end{document}

However, you'll notice that LaTeX gives some "Overfull hbox" warnings. Since you only have 2 columns in your tabularx, I would suggest calculating the X column instead - using a similar method as above - and making it a paragraph p{<len>} column to fix exactly within the \textwidth (or \linewidth).

You must log in to answer this question.

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