3

Before ask for help I search here but, with no luck.

I have a long table latex (I'm using longtable environment). I want to color some specific rows in gray (lines: 2, 4, 5, 6, 11, 12, 13, 15, 17, 21, 29 and 32). Can you guys help me to achieve this? Thank you in advance. Here's my code:

    \documentclass{article}

\usepackage{longtable}
\usepackage{rotating,tabularx,booktabs,siunitx,caption}
\usepackage{colortbl, xcolor}
\usepackage{adjustbox}
\usepackage{makecell}

\title{A longtable example}

\begin{document}

\begin{center}
    \small
    \setlength\LTleft{0pt}           
    \setlength\LTright{0pt}
    \setlength\tabcolsep{1.5pt} 
        \begin{longtable}{ccccc}
        \caption{Tabela Nome dos Candidatos. Elaborado por Autor. 2020.} \label{tab:tabelaNomeCandidatosColigacao} \\
        
        \toprule
        \multicolumn{1}{c}{\textbf{\makecell{Número \\ do Candidato}}} & 
        \multicolumn{1}{l}{\textbf{\makecell{Nome do Candidato \\ (urna)}}}  &
        \multicolumn{1}{c}{\textbf{Partido}} &
        \multicolumn{1}{c}{\textbf{\makecell{Votos Nominais \\ do Candidato}}} &
        \multicolumn{1}{c}{\textbf{\makecell{\% Votos \\ Válidos}}}  \\
        \midrule
        \endfirsthead
        
        \toprule
        \multicolumn{1}{c}{\textbf{Número do Candidato}} & 
        \multicolumn{1}{l}{\textbf{\makecell{Nome do Candidato \\ (urna)}}} & 
        \multicolumn{1}{c}{\textbf{Partido}} &
        \multicolumn{1}{c}{\textbf{\makecell{Votos Nominais \\ do Candidato}}} &
        \multicolumn{1}{c}{\textbf{\makecell{\% Votos \\ Válidos}}} \\
        \midrule
        \endhead
        
        \midrule
        \endfoot
        
        \bottomrule
        \endlastfoot
        
        44444 & JORGE KAJURU  & PRP & 37.796 & 5,65 \\ 
        \rowcolor{gray!40}
        45233 & DRA. CRISTINA  & PSDB & 9.114 & 1,36 \\
        \makecell{90100} & \makecell{VINICIUS CIRQUEIRA}  & \makecell{PROS} & 8.582 & 1,28 \\
        \rowcolor{gray!40!}
        10123 & ROGERIO CRUZ  & PRB & 8.312 & 1,24 \\
        \rowcolor{gray!40!}
        45602 & ANSELMO PEREIRA  & PSDB & 7.504 & 1,12 \\
        \rowcolor{gray!40!}
        40001 & ELIAS VAZ  & PSB & 7.487 & 1,12 \\
        23456 & CARLIN CAFÉ  & PPS & 7.392 & 1,1 \\
        \makecell{10010} & \makecell{ALYSSON LIMA}  & \makecell{PRB} & 7.257 & 1,08 \\
        \makecell{35789} & \makecell{SABRINA GARCEZ}  & \makecell{PMB} & 7.233 & 1,08 \\
        \makecell{22222} & \makecell{FELISBERTO TAVARES}  & \makecell{PR} & 6.670 & 1 \\
        \rowcolor{gray!40!}
        15700 & WELINGTON PEIXOTO  & PMDB & 6.513 & 0,97 \\
        \rowcolor{gray!40!}
        12345 & PAULINHO GRAUS  & PDT & 6.452 & 0,96 \\
        \rowcolor{gray!40!}
        40321 & OSÉIAS VARÃO  & PSB & 6.171 & 0,92 \\
        \makecell{22123} & \makecell{IZIDIO ALVES}  & \makecell{PR} & 5.889 & 0,88 \\
        \rowcolor{gray!40!}
        27500 & KLEYBE MORAIS  & PSDC & 5.818 & 0,87 \\
        \makecell{15015} & \makecell{CLECIO ALVES}  & \makecell{PRP} & 5.415 & 5,65 \\
        \rowcolor{gray!40!}
        90900 & TIAOZINHO PORTO  & PROS & 4.878 & 0,73 \\
        \makecell{55555} & \makecell{PRISCILLA TEJOTA}  & \makecell{PSD} & 4.807 & 0,72 \\
        \makecell{17000} & \makecell{LUCAS KITÃO}  & \makecell{PSL} & 4.499 & 0,67 \\
        \makecell{55015} & \makecell{PAULO MAGALHÃES}  & \makecell{PSD} & 4.482 & 0,67 \\
        \rowcolor{gray!40!}
        65123 & TATIANA LEMOS  & PC do B & 4.418 & 0,66 \\
        \makecell{43444} & \makecell{DELEGADO EDUARDO PRADO}  & \makecell{PV} & 4.237 & 0,63 \\
        \makecell{15678} & \makecell{ANDREY AZEREDO}  & \makecell{PMDB} & 4.073 & 0,61 \\
        \makecell{43400} & \makecell{GUSTAVO CRUVINEL}  & \makecell{PV} & 4.066 & 0,61 \\
        \makecell{27900} & \makecell{JAIR DIAMANTINO}  & \makecell{PSDC} & 3.889 & 0,58 \\
        \makecell{28123} & \makecell{JUAREZ LOPES}  & \makecell{PRTB} & 3.753 & 0,56 \\
        \makecell{27100} & \makecell{ANDERSON SALES BOKÃO}  & \makecell{PSDC} & 3.479 & 0,52 \\
        \rowcolor{gray!40!}
        51800 & ZANDER  & PEN & 3.501 & 0,52 \\
        20500 & LEIA KLEBIA  & PSC & 3.367 & 0,5 \\
        \makecell{36153} & \makecell{GCM ROMÁRIO POLICARPO}  & \makecell{PTC} & 3.185 & 0,48 \\
        \makecell{44321} & \makecell{MILTON MERCEZ}  & \makecell{PRP} & 2.886 & 0,43 \\
        \makecell{44190} & \makecell{CABO SENNA}  & \makecell{PRP} & 2.795 & 0,42 \\
        19123 & SARGENTO NOVANDIR  & PTN & 2.713 & 0,41 \\
        \rowcolor{gray!40!}
        25555 & DR. PAULO DAHER  & DEM & 2.511 & 0,38 \\
        \makecell{19123} & \makecell{EMILSON PEREIRA}  & \makecell{PTN} & 2.429 & 0,36 \\
        
        \end{longtable}
\end{center}

\end{document}
13
  • What is the reason for all the \makecell commands?
    – leandriis
    Commented Jun 19, 2020 at 18:35
  • Without the \makecell commands, the \rowcolor command works perfectly fine: \rowcolor{gray!80!} 45233 & DRA. CRISTINA & PSDB & 9.114 & 1,36 \\
    – leandriis
    Commented Jun 19, 2020 at 18:37
  • I will remove them. Not using it for anything on this table. Just want to color those specific rows with gray and I don't know how to do that. Commented Jun 19, 2020 at 18:38
  • Why did you decide to use a \tabcolsep of just 1pt? As you can see from the table headers of the two last columns, with such a small tabcolsep, the text in the two columns will almost touch.
    – leandriis
    Commented Jun 19, 2020 at 18:39
  • No way @leandriis, I just tested here and worked like a charm. Thanks Commented Jun 19, 2020 at 18:39

1 Answer 1

2

The following might serve as a point to start from. Since there was no documentclass of other information about the size of the margins given in the original code, I guessed:

\documentclass{article}
\usepackage{longtable}
\usepackage{makecell}
\usepackage{booktabs}

\usepackage[table]{xcolor}

\begin{document}

{
\small
\setlength{\tabcolsep}{5pt} 
  \begin{longtable}{cllcc}
  \caption{Tabela Nome dos Candidatos. Elaborado por Autor. 2020.}
  \label{tab:tabelaNomeCandidatosColigacao} \\
  
  \toprule
  \thead{Número \\ do\\ Candidato} & 
  \thead{Nome do Candidato \\ (urna)}  &
  \thead{Partido} &
  \thead{Votos\\ Nominais  do\\ Candidato} &
  \thead{\% Votos \\ Válidos}  \\
  \midrule
  \endfirsthead
  
  \toprule
  \thead{Número \\ do\\ Candidato} & 
  \thead{Nome do Candidato \\ (urna)}  &
  \thead{Partido} &
  \thead{Votos\\ Nominais  do\\ Candidato} &
  \thead{\% Votos \\ Válidos}  \\
  \midrule
  \endhead
  
  \midrule
  \endfoot
  
  \bottomrule
  \endlastfoot
  
                       44444 & JORGE KAJURU            & PRP & 37.796 & 5,65 \\ 
  \rowcolor{lightgray} 45233 & DRA. CRISTINA           & PSDB    & 9.114 & 1,36 \\
                       90100 & VINICIUS CIRQUEIRA      & PROS    & 8.582 & 1,28 \\
  \rowcolor{lightgray} 10123 & ROGERIO CRUZ            & PRB     & 8.312 & 1,24 \\
  \rowcolor{lightgray} 45602 & ANSELMO PEREIRA         & PSDB    & 7.504 & 1,12 \\
  \rowcolor{lightgray} 40001 & ELIAS VAZ               & PSB     & 7.487 & 1,12 \\
                       23456 & CARLIN CAFÉ             & PPS     & 7.392 & 1,1  \\
                       10010 & ALYSSON LIMA            & PRB     & 7.257 & 1,08 \\
                       35789 & SABRINA GARCEZ          & PMB     & 7.233 & 1,08 \\
                       22222 & FELISBERTO TAVARES      & PR      & 6.670 & 1    \\
                       15700 & WELINGTON PEIXOTO       & PMDB    & 6.513 & 0,97 \\
                       12345 & PAULINHO GRAUS          & PDT     & 6.452 & 0,96 \\
                       40321 & OSÉIAS VARÃO            & PSB     & 6.171 & 0,92 \\
                       22123 & IZIDIO ALVES            & PR      & 5.889 & 0,88 \\
                       27500 & KLEYBE MORAIS           & PSDC    & 5.818 & 0,87 \\
                       15015 & CLECIO ALVES            & PRP     & 5.415 & 5,65 \\
                       90900 & TIAOZINHO PORTO         & PROS    & 4.878 & 0,73 \\
                       55555 & PRISCILLA TEJOTA        & PSD     & 4.807 & 0,72 \\
                       17000 & LUCAS KITÃO             & PSL     & 4.499 & 0,67 \\
                       55015 & PAULO MAGALHÃES         & PSD     & 4.482 & 0,67 \\
                       65123 & TATIANA LEMOS           & PC do B & 4.418 & 0,66 \\
                       43444 & DELEGADO EDUARDO PRADO  & PV      & 4.237 & 0,63 \\
                       15678 & ANDREY AZEREDO          & PMDB    & 4.073 & 0,61 \\
                       43400 & GUSTAVO CRUVINEL        & PV      & 4.066 & 0,61 \\
                       27900 & JAIR DIAMANTINO         & PSDC    & 3.889 & 0,58 \\
                       28123 & JUAREZ LOPES            & PRTB    & 3.753 & 0,56 \\
                       27100 & ANDERSON SALES BOKÃO    & PSDC    & 3.479 & 0,52 \\
                       51800 & ZANDER                  & PEN     & 3.501 & 0,52 \\
                       20500 & LEIA KLEBIA             & PSC     & 3.367 & 0,5  \\
                       36153 & GCM ROMÁRIO POLICARPO   & PTC     & 3.185 & 0,48 \\
                       44321 & MILTON MERCEZ           & PRP     & 2.886 & 0,43 \\
                       44190 & CABO SENNA              & PRP     & 2.795 & 0,42 \\
                       19123 & SARGENTO NOVANDIR       & PTN     & 2.713 & 0,41 \\
                       25555 & DR. PAULO DAHER         & DEM     & 2.511 & 0,38 \\
                       19123 & EMILSON PEREIRA         & PTN     & 2.429 & 0,36 \\
  
  \end{longtable}
}

\end{document}
1
  • Thank you for your help !!!!! It really did the trick for me. Also, now I know how to post any question with MWE. Commented Jun 20, 2020 at 14:46

You must log in to answer this question.

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