3

My objective was to write latex code for the following table created in Word. enter image description here

However, I am unable to format the first column. I would like to define a certain measurement for the column width and for this command to generate an automatic line wrap if the width is too small and in addition, I would like the text to be centered between the two cells that were merged.

\begin{table}[H]
\centering
\begin{tabular}{p{3cm}cccccc}
\hline
  Variável                                                         & Período & Média    & Desvio Padrão & \begin{tabular}[c]{@{}c@{}}Coeficiente \\ de variação\end{tabular} & \begin{tabular}[c]{@{}c@{}}Valor\\ Mínimo\end{tabular} & \begin{tabular}[c]{@{}c@{}}Valor\\ Máximo\end{tabular} \\ \hline
    \multirow{2}{*}{Leitos de UTI}                                   & 2016    & 1.867    & 2.505         & 134,17\%                                                           & 73                                                     & 11.039                                                 \\
    & 2019    & 2.016    & 2.738         & 135,81\%                                                           & 82                                                     & 12.515                                                 \\ \hline
    \multirow{2}{=}{Doadores Efetivos}                               & 2016    & 141      & 193           & 136,88\%                                                           & 5                                                      & 842                                                    \\
    & 2019    & 178      & 249           & 139,89\%                                                           & 4                                                      & 1.080                                                  \\ \hline
    \multirow{2}{*}{CIHDOTT’s}                                       & 2016    & 31       & 56            & 180,65\%                                                           & 1                                                      & 255                                                    \\
    & 2019    & 25       & 29            & 116,00\%                                                           & 1                                                      & 106                                                    \\ \hline
    \multirow{2}{*}{Equipes de Transplantes Renais}                  & 2016    & 6        & 8             & 133,33\%                                                           & 1                                                      & 35                                                     \\
    & 2019    & 7        & 8             & 114,29\%                                                           & 1                                                      & 31                                                     \\ \hline
    \multirow{2}{*}{Respiradores de emergência}                      & 2016    & 2.594    & 3.697         & 142,52\%                                                           & 121                                                    & 16.692                                                 \\
    & 2019    & 2.981    & 4.115         & 138,04\%                                                           & 152                                                    & 18.807                                                 \\ \hline
    \multirow{2}{*}{Serviços de Neurocirurgia}                       & 2016    & 20       & 26            & 130,00\%                                                           & 3                                                      & 118                                                    \\
    & 2019    & 22       & 28            & 127,27\%                                                           & 4                                                      & 127                                                    \\ \hline
    \multirow{2}{*}{Valores totais pagos com Serviços Hospitalares}  & 2016    & 6.871,29 & 11.235,63     & 163,52\%                                                           & 41,79                                                  & 49.342,72                                              \\
    & 2019    & 8.071,96 & 12.114,82     & 150,08\%                                                           & 85,20                                                  & 54.430,50                                              \\ \hline
    \multirow{2}{*}{Valores totais pagos com Serviços Profissionais} & 2016    & 2.606,59 & 4.332,99      & 166,23\%                                                           & 16,72                                                  & 19.179,92                                              \\
    & 2019    & 3.074,86 & 4.657,82      & 151,48\%                                                           & 35,65                                                  & 20.285,42                                              \\ \hline
    \multirow{2}{*}{Recusa Familiar}                                 & 2016    & 120      & 140           & 116,67\%                                                           & 12                                                     & 656                                                    \\
    & 2019    & 123      & 143           & 116,26\%                                                           & 14                                                     & 678                                                    \\ \hline
    \multirow{2}{*}{Autorização de Internações Hospitalares}         & 2016    & 229      & 363           & 158,51\%                                                           & 2                                                      & 1.605                                                  \\
    & 2019    & 261      & 377           & 144,44\%                                                           & 4                                                      & 1.639                                                  \\ \hline
\end{tabular}\end{table}

enter image description here

As mentioned in the comments, I tried to solve the problem using the command \multirow{2}{=}{...}

but this generated some overlapping of the lines as you can see in the Figure below.

enter image description here

5
  • In the fourth and fifth row, you use \multirow{2}{=}{Doadores Efetivos}, that is, you use = instead of *, which seems to work fine. Why not using it throughout the table? Or maybe this is not the output you want? Commented Feb 6 at 17:31
  • @JasperHabicht I think this is not the ideal solution, I did some tests and this generated some overlapping of the lines.
    – Math
    Commented Feb 6 at 17:35
  • The multirow manual (page 14 or so) suggests for such cases to insert one (or multiple) blank row(s). I don't know if this is a solution for you. Commented Feb 6 at 17:41
  • 1
    @JasperHabicht I added an image showing what the original table looked like, which I'm trying to create in latex.
    – Math
    Commented Feb 6 at 17:46
  • Well, in the image you show, the leftmost column is larger so that the text can easily fit in two lines. You need to adjust the font size or the column widths of your table accordingly. For example, you could change p{3cm} to p{4cm}. But it is difficult to help you without a minimal working example (MWE) that shows the document class and packages you use, so that others can know about how much space you actually have for the table. Maybe also have a look at tha various suggestions in this thread. Commented Feb 6 at 17:56

2 Answers 2

3

Not sure why * isn't working, but adding the width does (my manual doen't list = as an option). The other problem is that 3cm isn't wide enough for the column, but is already too wide to fit the tabular on the page.

\documentclass{article}
\usepackage{multirow}

\begin{document}

\begin{table}[ht]% friends don't let friends use H
\centering
\setlength{\tabcolsep}{1pt}
\begin{tabular}{p{4cm}cccccc}
\hline
  Variável                                                         & Período & Média    & Desvio Padrão & \begin{tabular}[c]{@{}c@{}}Coeficiente \\ de variação\end{tabular} & \begin{tabular}[c]{@{}c@{}}Valor\\ Mínimo\end{tabular} & \begin{tabular}[c]{@{}c@{}}Valor\\ Máximo\end{tabular} \\ \hline
    \multirow{2}{4cm}{Leitos de UTI}                                   & 2016    & 1.867    & 2.505         & 134,17\%                                                           & 73                                                     & 11.039                                                 \\
    & 2019    & 2.016    & 2.738         & 135,81\%                                                           & 82                                                     & 12.515                                                 \\ \hline
    \multirow{2}{4cm}{Doadores Efetivos}                               & 2016    & 141      & 193           & 136,88\%                                                           & 5                                                      & 842                                                    \\
    & 2019    & 178      & 249           & 139,89\%                                                           & 4                                                      & 1.080                                                  \\ \hline
    \multirow{2}{4cm}{CIHDOTT’s}                                       & 2016    & 31       & 56            & 180,65\%                                                           & 1                                                      & 255                                                    \\
    & 2019    & 25       & 29            & 116,00\%                                                           & 1                                                      & 106                                                    \\ \hline
    \multirow{2}{4cm}{Equipes de Transplantes Renais}                  & 2016    & 6        & 8             & 133,33\%                                                           & 1                                                      & 35                                                     \\
    & 2019    & 7        & 8             & 114,29\%                                                           & 1                                                      & 31                                                     \\ \hline
    \multirow{2}{4cm}{Respiradores de emergência}                      & 2016    & 2.594    & 3.697         & 142,52\%                                                           & 121                                                    & 16.692                                                 \\
    & 2019    & 2.981    & 4.115         & 138,04\%                                                           & 152                                                    & 18.807                                                 \\\hline
    \multirow{2}{4cm}{Serviços de Neurocirurgia}                       & 2016    & 20       & 26            & 130,00\%                                                           & 3                                                      & 118                                                    \\
    & 2019    & 22       & 28            & 127,27\%                                                           & 4                                                      & 127                                                    \\ \hline
    \multirow{2}{4cm}{Valores totais pagos com Serviços Hospitalares}  & 2016    & 6.871,29 & 11.235,63     & 163,52\%                                                           & 41,79                                                  & 49.342,72                                              \\
    & 2019    & 8.071,96 & 12.114,82     & 150,08\%                                                           & 85,20                                                  & 54.430,50                                              \\ \hline
    \multirow{2}{4cm}{Valores totais pagos com Serviços Profissionais} & 2016    & 2.606,59 & 4.332,99      & 166,23\%                                                           & 16,72                                                  & 19.179,92                                              \\
    & 2019    & 3.074,86 & 4.657,82      & 151,48\%                                                           & 35,65                                                  & 20.285,42                                              \\ \hline
    \multirow{2}{4cm}{Recusa Familiar}                                 & 2016    & 120      & 140           & 116,67\%                                                           & 12                                                     & 656                                                    \\
    & 2019    & 123      & 143           & 116,26\%                                                           & 14                                                     & 678                                                    \\ \hline
    \multirow{2}{4cm}{Autorização de Internações Hospitalares}         & 2016    & 229      & 363           & 158,51\%                                                           & 2                                                      & 1.605                                                  \\
    & 2019    & 261      & 377           & 144,44\%                                                           & 4                                                      & 1.639                                                  \\ \hline
\end{tabular}\end{table}
\end{document}

demo

3
  • +1. Should friends let friends run \setlength{\tabcolsep}{1pt}? :-) (Hint: force a line break in Desvio Padrão.)
    – Mico
    Commented Feb 6 at 18:29
  • We all know that [H] wastes space. Never heard anyone complaining about their tabulars being too narrow. Commented Feb 6 at 22:00
  • It's not about the overall width, it's about the (lack of) whitespace between columns.
    – Mico
    Commented Feb 7 at 0:15
3

The following solution doesn't employ any \multirow directives. It further aligns the numbers in the data columns on their explicit or implicit decimal markers. Finally, it gives the table a more open and inviting "look" by using far fewer, but well-spaced, horizontal lines.

enter image description here

\documentclass{article}
\usepackage[a4paper,margin=3cm]{geometry} % set page parameters as needed

\usepackage[T1]{fontenc}
\usepackage[portuguese]{babel} % or 'brazilian'?
\usepackage{lmodern}

\usepackage{amsmath,tabularx,booktabs}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcommand\mc[1]{\multicolumn{1}{@{}c@{}}{#1}} % handy shortcut macro
\newcommand\mytab[1]{%
   \begin{tabular}[t]{@{} c @{}} #1 \end{tabular}}
\newcommand\mytabx[1]{\smash[b]{%
   \begin{tabular}[t]{@{} L @{}} #1 \end{tabular}}}

\usepackage{dcolumn} % align numbers on decimal markers
\newcolumntype{d}[1]{D{,}{,}{#1}}

\begin{document}

\begin{table}[ht]

\begin{tabularx}{\textwidth}{@{} % target width: \textwidth
    L l d{4.2} d{5.2} c d{3.2} d{5.2} @{}}
\toprule
  Variável & 
  Período  & 
  \mc{\mytab{Média}} & 
  \mc{\mytab{Desvio\\Padrão}} &   
  \mytab{Coeficiente \\ de variação} & 
  \mc{\mytab{Valor\\ Mínimo}} & 
  \mc{\mytab{Valor\\ Máximo}} \\ 
\midrule
Leitos de UTI     
      & 2016    & 1.867    & 2.505 & 134,17\% & 73 & 11.039 \\
      & 2019    & 2.016    & 2.738 & 135,81\% & 82 & 12.515 \\ 
\addlinespace
Doadores Efetivos 
      & 2016    & 141      & 193   & 136,88\% & 5  & 842    \\
      & 2019    & 178      & 249   & 139,89\% & 4  & 1.080  \\ 
\addlinespace
CIHDOTT’s         
      & 2016    & 31       & 56    & 180,65\% & 1  & 255    \\
      & 2019    & 25       & 29    & 116,00\% & 1  & 106    \\ 
\addlinespace
\mytabx{Equipes de Transplantes Renais} 
      & 2016    & 6        & 8     & 133,33\% & 1  & 35     \\
      & 2019    & 7        & 8     & 114,29\% & 1  & 31     \\ 
\addlinespace
\mytabx{Respiradores de emergência} 
      & 2016    & 2.594    & 3.697 & 142,52\% & 121 & 16.692\\
      & 2019    & 2.981    & 4.115 & 138,04\% & 152 & 18.807\\ 
\addlinespace
\mytabx{Serviços de Neurocirurgia}
      & 2016    & 20       & 26    & 130,00\% & 3   & 118   \\
      & 2019    & 22       & 28    & 127,27\% & 4   & 127   \\ 
\addlinespace
\mytabx{Valores totais pagos com Serviços Hospitalares} 
      & 2016    & 6.871,29 & 11.235,63 & 163,52\% & 41,79 & 49.342,72  \\
      & 2019    & 8.071,96 & 12.114,82 & 150,08\% & 85,20 & 54.430,50  \\ 
\addlinespace
\mytabx{Valores totais pagos com Serviços Profissionais} 
      & 2016    & 2.606,59 & 4.332,99 & 166,23\% & 16,72 & 19.179,92 \\
      & 2019    & 3.074,86 & 4.657,82 & 151,48\% & 35,65 & 20.285,42 \\ 
\addlinespace
Recusa Familiar   
      & 2016    & 120      & 140      & 116,67\% & 12    & 656   \\
      & 2019    & 123      & 143      & 116,26\% & 14    & 678   \\ 
\addlinespace
\mytabx{Autorização de Internações Hospitalares} 
      & 2016    & 229      & 363      & 158,51\% & 2     & 1.605 \\
      & 2019    & 261      & 377      & 144,44\% & 4     & 1.639 \\ 
\bottomrule
\end{tabularx}

\end{table}

\end{document}

You must log in to answer this question.

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