Search code examples
latexpositioningcentering

How can I center the head of a table in LateX?


I obtain this:

enter image description here

I cannot figure out how can I center the first column name vertically. Below is my current code:

\\documentclass{article}
\\usepackage\[utf8\]{inputenc}
\\usepackage{longtable}
\\usepackage{multirow}
\\usepackage{array}
\\title{teste}
\\begin{document}
\\setlength{\\tabcolsep}{9pt}
\\renewcommand{\\arraystretch}{1.1}
\\newcolumntype{M}\[1\]{\>{\\centering\\arraybackslash}m{#1}}
\\begin{longtable}
{|M{2,5cm}||M{1,3cm}|M{2,1cm}|M{3,1cm}|M{2,4cm}| }
\\hline
\\multicolumn{5}{|c|}{MODELS} \\
\\hline
\\vspace{1.9cm}
Nome& Tipo &Estimativa de Custo Anual&Requisitos Cumpridos& Último Update\\
\\hline
\\endhead
ABC   & C& 9.999&Sim &Não\\
\\hline
\\end{longtable}
\\end{document}

Solution

  • Please always provide a minimum working example (MWE). This is code we can compile and test. You should also clean your code before posting. Otherwise, you ask potential contributor for the extra unnecessary work to help you.

    I suggest another long-table related environment xltabular. It is similar to longtable but accepts the X column type, which adapts width to the rest of the table; it's a convenient way to avoid unnecessary calculations to get a proper dimension of your table, which does not exceed page dimensions.

    With X which uses p{} under the hood, you get the correct alignment of the first cell. I also recommend reducing avoiding vertical to a minimum. In your case, I left the one between the first column and "the rest" of the table. I also demonstrated how you could add bars and rules of different thicknesses.

    Finally, I added \endfoot so the table is correctly closed on subsequent pages.

    Note. On one hand, you are using , as a decimal marker in a column definition and . in the content of the table. I was surprised the code actually get compiled.

    EDIT. I use a \xmathstrut macro from mathtools to add some vertical spacing in your header. Lower the value from 2 to reduce the spacing.

    Here's the code:

    \documentclass{article}
    \usepackage{xltabular}
    \usepackage{mathtools}
    
    % \title{teste}
    
    
    \begin{document}
    % --- BEGIN GROUP -------------------------
    \begingroup
    \renewcommand{\arraystretch}{1.5}
    \newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
    \renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}p{#1}}
    \setlength{\tabcolsep}{3pt}
    \begin{xltabular}{\linewidth}{
      X !{\vrule width 0.2pt}
      M{1,3cm}
      M{2,1cm}
      M{3,1cm}
      M{2,4cm}
    }
      \noalign{\hrule height 0.8pt}
      \multicolumn{5}{c}{MODELS} \\
      \noalign{\hrule height 0.8pt}
      $\xmathstrut{2}$Nome
      & Tipo
      & Estimativa de Custo Anual
      & Requisitos Cumpridos
      & Último Update \\
      \noalign{\hrule height 0.5pt} \endhead
      \noalign{\hrule height 0.8pt} \endfoot
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
      ABC & C & 9.999 & Sim & Não\\
    \end{xltabular}
    \endgroup
    % --- END GROUP ---------------------------
    \end{document}
    

    enter image description here