4

I have problems translating this table (from Online table generator) to tabularx environment so it would be coherent with the rest of Tables of my document. Could you kindly help me? Thanks!

\documentclass{article}
\usepackage{multirow}
\usepackage{graphicx}

\begin{document}
\begin{table}[]
\centering
\caption{My caption}
\resizebox{\textwidth}{!}{%
\begin{tabular}{cccccc}
\hline
1                     & 2                    & 3                    & 4                    & 5                    & 6                    \\
\hline
\multirow{2}{*}{Pepe} & \multirow{2}{*}{a}   & \multirow{2}{*}{b}   & \multirow{2}{*}{c}   & \multirow{2}{*}{d}   & e                    \\
                      &                      &                      &                      &                      & f                    \\
\hline
\multicolumn{1}{l}{}  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
\hline
\end{tabular}%
}
\end{table}
\end{document}

That is, the table should start by:

\documentclass{article}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{tabularx}

\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}
\end{tabularx}
BLA, BLA, BLA.
\end{table}
\end{document}
3
  • A tip: If you indent lines by 4 spaces or enclose words in backticks `, they'll be marked as code, as can be seen in my edit. You can also highlight the code and click the "code" button (with "{}" on it).
    – Tiuri
    Commented Jan 28, 2019 at 8:52
  • Remember, tabularx takes two arguments, you only listed one
    – daleif
    Commented Jan 28, 2019 at 9:06
  • 2
    Centering a box of \textwidth? Resize a a small table ? Multicolumns of 1 column? Another suggestion: Do no use table generators, the resulting code is garbage.
    – Fran
    Commented Jan 28, 2019 at 11:24

3 Answers 3

4

Here are two solutions. The first uses a tabularx environment, with all six columns using a centered version of the X column type. The second uses a tabular* environment, with all six columns using the c column type. Both solutions occupy the full width of the textblock.

Since \multicolumn{1}{l}{} does nothing at all, you might as well omit those instructions.

enter image description here

\documentclass{article}
\usepackage{multirow,tabularx,booktabs}
\newcommand{\mr}[1]{\multirow{2}{*}{#1}} % handy shortcut macro
% centered version of X column type:
\newcolumntype{C}{>{\centering\arraybackslash}X} 

\usepackage[skip=0.333\baselineskip]{caption} % optional

\begin{document}
\begin{table}
%%\centering  % <-- redundant
\caption{tabularx}
\begin{tabularx}{\textwidth}{@{} *{6}{C} @{}}
\toprule
1         & 2      & 3      & 4      & 5      & 6 \\
\midrule
\mr{Pepe} & \mr{a} & \mr{b} & \mr{c} & \mr{d} & e \\
          &        &        &        &        & f \\
\midrule
  &  &  &  &  &  \\ % no need for "\multicolumn{1}{l}{}" stuff
\bottomrule
\end{tabularx}

\vspace{1cm} %
\setlength\tabcolsep{0pt}
\caption{tabular*}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} *{6}{c} }
\toprule
1         & 2      & 3      & 4      & 5      & 6 \\
\midrule
\mr{Pepe} & \mr{a} & \mr{b} & \mr{c} & \mr{d} & e \\
          &        &        &        &        & f \\
\midrule
  &  &  &  &  &  \\ % no need for "\multicolumn{1}{l}{}" stuff
\bottomrule
\end{tabular*}
\end{table}
\end{document} 
1
  • Thank you so much for this very useful answer, Best regards.
    – Santi
    Commented Jan 30, 2019 at 15:25
3

You do not need multicolumns in every cell, nor \multirow{2}{*}{...}, nor tabularx or tabular* to extend the text width, nor empty rows. Here you have enough with m columns (of array package) and, of course, with booktabs: :)

mwe

\documentclass{article}
\usepackage{graphicx,array,booktabs}
\begin{document}
\begin{table}
\caption{My caption}
\newcolumntype{C}{>{\centering\arraybackslash}m{\dimexpr\linewidth/6-2\tabcolsep}}
\begin{tabular}{CCCCCC}\toprule
1  & 2 & 3 & 4 & 5 & 6 \\\midrule
Pepe & a & b & c  & d &e\par f\\\bottomrule
\end{tabular}
\end{table}
\end{document}

By the way, I would not use columns of more two-three em here. Widening the table as much as possible instead of a small centered table does not make it more beautiful, but ugly scattered.

3

As @daleif mentioned you forgot to add the second bracket with your columns' specification {cccccc}. Probably there is a reason why you want to use the tabularx environment from the tabularx package.

The pack­age de­fines an en­vi­ron­ment tab­u­larx, an ex­ten­sion of tab­u­lar which has an ad­di­tional col­umn des­ig­na­tor, X, which cre­ates a para­graph-like col­umn whose width au­to­mat­i­cally ex­pands so that the de­clared width of the en­vi­ron­ment is filled. (Two X columns to­gether share out the avail­able space be­tween them, and so on.)

I guess you want to adapt the width of the first column as shown in the following example.

Compiled output (using standalone class)

I give you the according code as a MWE:

\documentclass{article}
\usepackage{tabularx}
\usepackage{multirow}
% The [following] pack­age en­hances the qual­ity of ta­bles [...]
\usepackage{booktabs}
% Macro to simplify the code
\newcommand{\mr}[1]{\multirow{2}{*}{#1}}

\begin{document}
\begin{table}
\caption{My caption}
\centering
\begin{tabularx}{.5\textwidth}{Xccccc}
\toprule
1         & 2      & 3      & 4      & 5      & 6 \\
\midrule
\mr{Pepe} & \mr{a} & \mr{b} & \mr{c} & \mr{d} & e \\
          &        &        &        &        & f \\
\midrule
  &  &  &  &  &  \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

Supplement

Additionally, keep in mind that there is a package called tabu, which is more powerful than tabularx.

2
  • 3
    It does not make any sense to use tabularx with no X columns, then you could just as well have used tabular
    – daleif
    Commented Jan 28, 2019 at 10:18
  • I agree with you, the package aim is to adapt the space between columns, e.g. \begin{tabularx}{250pt}{|c|X|c|X|}... Commented Jan 28, 2019 at 12:14

You must log in to answer this question.

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