Create a latex table where odd rows are in olive!8 color and others in white
- User
Create a latex table where odd rows are in olive!8 color and others in white
- Assistant
Here is an example of a LaTeX table with odd rows colored in olive!8 and even rows in white: \documentclass{article} \usepackage{colortbl} \usepackage{xcolor}
\definecolor{olive}{HTML}{#808000}
\begin{document}
\begin{tabular}{|l|l|} \hline \rowcolor{olive!8} This is & the first row \ \rowcolor{white} This is & the second row \ \rowcolor{olive!8} This is & the third row \ \rowcolor{white} This is & the fourth row \ \rowcolor{olive!8} This is & the fifth row \ \hline \end{tabular}
\end{document} CopiedIn this example, the colortbl package is used to color the rows, and the xcolor package is used to define the olive color. The \rowcolor command is used to set the color of each row. You can adjust the shade of olive by changing the number after the ! symbol. For example, olive!20 would produce a darker shade of olive. If you want to automate the coloring of the rows, you can use the tabular environment with the rowcolor option. Here is an example: \documentclass{article} \usepackage{colortbl} \usepackage{xcolor}
- User
rowcolors command should be out of the table otherwise it doesn't work for me
- Assistant
You are correct that the \rowcolors command should be used outside of the tabular environment. Here is the corrected code: \documentclass{article} \usepackage{colortbl} \usepackage{xcolor}
\definecolor{olive}{HTML}{#808000}
\begin{document}
\rowcolors{2}{olive!8}{white} \begin{tabular}{|l|l|} \hline This is & the first row \ This is & the second row \ This is & the third row \ This is & the fourth row \ This is & the fifth row \ \hline \end{tabular}
\end{document} CopiedBy placing the \rowcolors command outside of the tabular environment, it will correctly apply the alternating row colors to the table. Alternatively, you can use the tabular environment with the rowcolor option, like this: \documentclass{article} \usepackage{colortbl} \usepackage{xcolor}
\definecolor{olive}{HTML}{#808000}
\begin{document}
\begin{tabular}{|l|l|} \hline \rowcolor{olive!8} This is & the first row \ \rowcolor{white} This is & the second row \ \rowcolor{olive!8} This is & the third row \ \rowcolor{white} This is & the fourth row \ \rowcolor{olive!8} This is & the fifth row \ \hline \end{tabular}
\end{document} CopiedBut using \rowcolors is often more convenient and flexible, especially for larger tables.