2

Code:

\begin{table}[htb]
\begin{flushleft}
\begin{minipage}{.55\textwidth}
{\tiny{table1}\newline\newline}
\input{"/path/to/table1.txt"}
\end{minipage}
\hfill
\begin{minipage}{.4\textwidth}
{\tiny{table2}\newline\newline}
\input{"/path/to/table2.txt"}
\end{minipage}
\end{flushleft}
\end{table}

table1.txt:

{\tiny
\begin{tabularx}{\textwidth}{p{.45\textwidth}|rr}
  \hline
 & B1 & B2 \\ 
  \hline
  A1 & 80 & 12 \\ 
  A2 & 1 & 2 \\ 
  B & 1 & 5 \\ 
  C & 3 & 1 \\ 
  C & 3 & 1 \\ 
  C & 3 & 1 \\ 
  C & 3 & 1 \\ 
   \hline
\end{tabularx}
}

table2.txt:

{\tiny
\begin{tabularx}{\textwidth}{p{.45\textwidth}|rr}
  \hline
 & A1 & A2 \\ 
  \hline
  A & 1 & 2 \\ 
  B & 1 & 5 \\ 
  C & 2 & 5 \\ 
  D & 3 & 8 \\
   \hline
\end{tabularx}
}

Question:

The two tables are not in the same level. The row length of table2 is smaller than the row length of table1. Therefore table2 to is setted centered towards table1.

How to get them in one line if the row lengths of table1 and table2 are not equal?

2 Answers 2

1

Please always make your code runnable (R for example is not a defined column type by default) but you probably want \begin{minipage}[t]{...} and \begin{tabularx}{<width>}[t]{...} in both cases so they align at their tops.

Although simpler would be to have [t] on the tabularx but not have the minipage and flushleft environments at all as they are not doing anything very useful, you can have two tables on the same line without wrapping them in minipage.

5
  • EDITED: R converted to -> r
    – b4154
    Commented Dec 10, 2015 at 10:15
  • @b4154 your example does not make sense after the edit as tabularx has to have a column based on the X column type, presumably R was such a defined column. with just p and r tabularx can not work at all. This is why examples should always be complete documents so that you can test them before posting. Commented Dec 10, 2015 at 10:22
  • @ David Carliste: \begin{tabularx}[t] does not work. \begin{minipage}[t] solves the problem. But if I have no caption, e.g.: {\tiny{table1}\newline\newline}, using \begin{minipage}[t] does not work to get the tables in one line
    – b4154
    Commented Dec 10, 2015 at 10:53
  • @Mico thanks (hard to remember your own syntax:-) Commented Dec 10, 2015 at 11:44
  • Carliste - I used {tabularx}[t] instead of {tabularx}{\\textwidth}[t]. Therefore the error. Thank you
    – b4154
    Commented Dec 10, 2015 at 12:07
1

In addition to adding the [t] positioning specifier to the two minipage declarations, you should use \caption commands and remove some unused code. For the tabularx environments, use X instead of p{0.45\textwidth}.

\documentclass{article}
\usepackage{tabularx,caption}
\captionsetup{skip=0.5\baselineskip} % whitespace below caption
\begin{document}
\begin{table}[htb]
\scriptsize % Don't use "\tiny" unless you have contempt for your readers
\captionsetup{font=scriptsize}
\begin{minipage}[t]{.55\textwidth}
  \caption{table1}
  \begin{tabularx}{\linewidth}{@{}X|rr@{}}
  \hline
  & B1 & B2 \\
  \hline
  A1 & 80 & 12 \\
  A2 & 1 & 2 \\
  B & 1 & 5 \\
  C & 3 & 1 \\
  C & 3 & 1 \\
  C & 3 & 1 \\
  C & 3 & 1 \\
  \hline
  \end{tabularx}
\end{minipage}
\hspace{\fill} % Maximize the horizontal separation
\begin{minipage}[t]{.4\textwidth}
  \caption{table2}
  \begin{tabularx}{\linewidth}{@{}X|rr@{}}
  \hline
  & B1 & B2 \\
  \hline
  A1 & 80 & 12 \\
  A2 & 1 & 2 \\
  B & 1 & 5 \\
  C & 3 & 1 \\
  C & 3 & 1 \\
  \hline
  \end{tabularx}
\end{minipage}
\end{table}

\end{document} 
4
  • Why to use {\linewidth} after {tabularx} and not e.g. {1.0.\textwidth}?
    – b4154
    Commented Dec 10, 2015 at 11:18
  • @b4154 - In this case, \linewidth, 1.0\linewidth, \textwidth, and 1.0\textwidth all work equally well. In contrast, 1.0.\textwidth won't work because of the stray . ("period", "full stop").
    – Mico
    Commented Dec 10, 2015 at 11:24
  • @b4154 - Out of curiosity: Why did you go ahead and "accept" the other answer? Should I delete my answer as it is apparently of no use to you?
    – Mico
    Commented Dec 10, 2015 at 11:27
  • No, do not delete please. But I did not understand your answer 100% and therefore I asked you about \linewidth
    – b4154
    Commented Dec 10, 2015 at 11:36

You must log in to answer this question.

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