3

I have a tabularx table with:

  • a variable width column X on the left
  • a cell with a nested tabular table on the right

Like so:

\begin{tabularx}{\textwidth}{ | X | c | }
\lipsum[1-1]
&
\begin{tabular}{rl}
test&test\\
test&test\\
test&test\\
test&test\\
test&test\\
test&test\\
\end{tabular}
\end{tabularx}

Now the problem I have is that for some reason it looks like this:

enter image description here

And I want it to look like this:

right

How do I go about this?

5

You are aligning the top line of the paragraph with the centre of the tabular.

Use

\begin{tabular}[t] 

so that the top line of the tabular is taken as the alignment point, which will then align with the top line of the paragraph in the first column.

3

You can do that with a simpler syntax if you use the makecellpackage, which allows for a common formatting, vertical/horizontal alignment and line breaks in cells introduced by the \makecell command. A small patch allows to have the vertical and horizontal alignment of the cell as an optional argument (default is cc – vertically and horizontally centred). In addition it can provide some vertical padding of cells:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array, tabularx}
\usepackage{makecell}
\setcellgapes{3pt}
\let\oldmakecell\makecell
\renewcommand\makecell[2][cc]{\renewcommand\cellalign{#1}\oldmakecell{#2}}
\usepackage{lipsum} 

\begin{document}

\begin{table}[h]
\makegapedcells
\begin{tabularx}{\textwidth}{ | X | rl | }
\lipsum[1-1]\leavevmode\vspace*{-\baselineskip}
& \makecell[tr]{test\\test\\test\\test\\test\\tests}
& \makecell[tl]{test\\test\\test\\test\\test\\tests}\\
\end{tabularx}
\end{table}

\end{document} 

enter image description here

2

I think you need to make two adjustments to the code:

  • First, as already noted by David Carlisle, you need to add the [t] position specifier to the inner tabular environment.

  • Second, you need to remove the extra whitespace that's surrounding the inner tabular -- remember that the c column type of the outer tabularx environment already provides whitespace (in the amount of \tabcolsep). No need to duplicate this. I suggest you insert @{} directives to prevent the insertion of extra whitespace.

To summarize, the inner tabular environment should be set up as follows:

\begin{tabular}[t]{@{} rl @{}}

enter image description here

\documentclass{article}
\usepackage{tabularx,lipsum}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{ | X | c | }
\lipsum*[1]
&
\begin{tabular}[t]{@{} rl @{}}
test&test\\
test&test\\
test&test\\
test&test\\
test&test\\
test&test\\
\end{tabular}
\end{tabularx}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.