30

I would like to align the content of each cell (of the first column) to the top. The approaches presented here did not apply. Maybe it has something to do with die tabular-environment inside the tabularx-environment?

How it looks like

i1

How I would like it to look like

i2

MWE

\documentclass{article}
\usepackage{tabularx,booktabs} 

\begin{document}

    \begin{tabularx}{\textwidth}{lX}
        \toprule
        Mittel    & technische Details\\
        \midrule
        %first row of tabularx
        Laptop      &
            \begin{tabular}{ll}
            MacBook Pro & \\
            Baujahr 2010 & \\ 
            Prozessor & 2.4 GHz Intel Core 2 Duo \\
            Speicher & 4 GB 1067 MHz DDR3 \\
            Software & Mac OS X Lion 10.7.5 (11G63)
            \end{tabular}\\[3em]
        %second row of tabularx
        Programmierumgebung          &
            \begin{tabular}{ll}
            Version & 8.0.4.0 \\
            Plattform & Mac OS x86 (32-bit, 64-bit Kernel)
            \end{tabular}\\
        \bottomrule
    \end{tabularx}

\end{document}
Mico
  • 506,678
John
  • 1,953

3 Answers3

19

enter image description here

You can make the inner tabulars top align by using [t] It may just be an artifact of your small example but tabularx can do nothing to help here as the inner tabular is fixed to its natural width so can not reflow to whatever width the X column specifies.

\documentclass{article}
\usepackage{tabularx,booktabs} 

\begin{document}
\noindent
    \begin{tabularx}{\textwidth}{lX}
        \toprule
        Mittel    & technische Details\\
        \midrule
        %first row of tabularx
        Laptop      &
            \begin{tabular}[t]{ll}
            MacBook Pro & \\
            Baujahr 2010 & \\ 
            Prozessor & 2.4 GHz Intel Core 2 Duo \\
            Speicher & 4 GB 1067 MHz DDR3 \\
            Software & Mac OS X Lion 10.7.5 (11G63)
            \end{tabular}\\[3em]
        %second row of tabularx
        Programmierumgebung          &
            \begin{tabular}[t]{ll}
            Version & 8.0.4.0 \\
            Plattform & Mac OS x86 (32-bit, 64-bit Kernel)
            \end{tabular}\\
        \bottomrule
    \end{tabularx}

\end{document}
David Carlisle
  • 757,742
  • I've noticed there's a significant difference between the X column type and the "older" l and p{<width>} column types: If there's a tabular environment in a row and X column, it's necessary to suppress the whitespace at the left edge of the environment explicitly, by inserting @{}, in order to get its contents to line up with other cells in the same column that are not tabulars. (See also the output of your MWE.) Inserting @{} isn't needed if the tabular environment occurs in a column of type p or l. Is this a "feature" of the tabularx environment? – Mico Mar 21 '13 at 13:41
  • @Mico Not really any different: X is p by the time that the table is typeset it just calculates a width and then literally generates a p{...} column specification and calls a normal tabular. – David Carlisle Mar 21 '13 at 13:45
  • If a tabularx is like a tabular, why not have the vertical alignment options of the array package? Wouldn't it be more natural to type \begin{tabularx}{\textwidth}[t]{@{}XX@{}} than to insert tabulars inside? – yannis Mar 24 '23 at 16:44
  • @yannis a tabularx is a tabular* just with some p column widths calculated. A [t] on the outer tabualar only has much affect if the table is inline with other text or image on the same baseline. It never affects the alignment between column cells so here it would do nothing – David Carlisle Mar 24 '23 at 16:54
9

Why not use a single tabularx environment instead of one inside a tabular?

\begin{tabularx}{\textwidth}{llX}
    \toprule
    Mittel              & technische Details & \\
    \midrule
    Laptop              & MacBook Pro  & \\
                        & Baujahr 2010 & \\ 
                        & Prozessor    & 2.4 GHz Intel Core 2 Duo \\
                        & Speicher     & 4 GB 1067 MHz DDR3 \\
                        & Software     & Mac OS X Lion 10.7.5 (11G63) \\[3em]
    Programmierumgebung & Version      & 8.0.4.0 \\
                        & Plattform    & Mac OS x86 (32-bit, 64-bit Kernel)\\
    \bottomrule
\end{tabularx}

sample output

Mico
  • 506,678
Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195
8

One can specify the vertical alignment -- top, middle (default), and bottom -- of a tabular environment via an optional argument. In the code below, I use [t] (for "top" alignment). Note that it's also necessary now to suppress explicitly, via a @{} directive, the whitespace that's otherwise inserted at the left edge of the cell when using the X column type for a cell that contains a tabular environment.

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs} 
\begin{document}
\noindent
  \begin{tabularx}{\textwidth}{lX}
    \toprule
    Mittel  & technische Details\\
    \midrule
    %first row of tabularx
    Laptop    &
      \begin{tabular}[t]{@{}ll} 
         % use [t] alignment specifier, and @{} to 
         % suppress extra whitespace at left edge
      MacBook Pro & \\
      Baujahr 2010 & \\ 
      Prozessor & 2.4 GHz Intel Core 2 Duo \\
      Speicher & 4 GB 1067 MHz DDR3 \\
      Software & Mac OS X Lion 10.7.5 (11G63)
      \end{tabular}\\[5em]
    %second row of tabularx
    Programmierumgebung      &
      \begin{tabular}[t]{@{}ll} 
      Version & 8.0.4.0 \\
      Plattform & Mac OS x86 (32-bit, 64-bit Kernel)
      \end{tabular}\\
    \bottomrule
  \end{tabularx}
\end{document}
Mico
  • 506,678