5

I'm kind of new to Latex and I'm trying to write a table like this one:

enter image description here

After hours of searching, this is the best I could achieve:

enter image description here

With this code:

\documentclass{report}
\usepackage{graphicx}

\begin{document}
    \begin{table}[!ht]
        \def\arraystretch{1.5}
        \centering
        \resizebox{\textwidth}{!}{%
            \begin{tabular}{|l|c|l|l|l|l|l|l|l|}
                \hline
                Bit & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 \\ \hline
                byte 1 & \multicolumn{4}{c|}{Tipo de Paquete de Control} & \multicolumn{4}{c|}{Flags específicos de cada Paquete de Control} \\ \hline
                byte 2 & \multicolumn{8}{c|}{Longitud restante} \\ \hline
            \end{tabular}%
        }
        \caption{Formato del Header Fijo}
    \end{table}
\end{document}

Which is pretty good for me except for the fact that '4' and '0' columns are really big compared to the other ones, the columns are not well distributed.

I have played with 'X' type columns but the table always ends up going outside the page. This must be pretty straightforward but I'm having a hard time creating tables (yet) coming from a Microsoft Word environment.

Any help or guidance is greatly appreciated!

2
  • texts in multicolumn cells are to wide. can they be split into two lines?
    – Zarko
    May 28, 2018 at 23:07
  • @Zarko yes, if it's automatic that would be event better!
    – Andres
    May 28, 2018 at 23:09

1 Answer 1

4

like this?

enter image description here

(red lines indicate page layout)

With the package tabularx, you can obtain equal-width cells 7 thru `0˙, if one properly calculates the usable width of the four-cell grouped cells

\documentclass{report}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X} % usable width: '\hsize'
\newcolumntype{Y}{>{\hsize=\dimexpr4\hsize+6\tabcolsep+3\arrayrulewidth}C}


%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[!ht]
    \def\arraystretch{1.5}
    %\centering % no longer needed
    \begin{tabularx}{\linewidth}{|l|*{8}{C|}}
    \hline
    Bit & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 \\ 
    \hline
    byte 1 & \multicolumn{4}{Y|}{Tipo de Paquete de Control}
           & \multicolumn{4}{Y|}{Flags específicos de cada Paquete de Control} \\ 
   \hline
   byte 2  & \multicolumn{8}{c|}{Longitud restante} \\ 
   \hline
   \end{tabularx}%
   \caption{Formato del Header Fijo}
   \end{table}
\end{document}
3
  • @Mico, thank you very much for correcting my answer.
    – Zarko
    May 29, 2018 at 5:20
  • Not a correction (there were no mistakes!), just some streamlining...
    – Mico
    May 29, 2018 at 5:25
  • oh, my midnight English was terrible ... and adding new column type Y significantly improve my answer. so i thing that you correct it :-)
    – Zarko
    May 29, 2018 at 5:38

You must log in to answer this question.

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