Please find my code below
from pylatex import Tabular, Document, Package, MultiColumn, SmallText, Command, MiniPage, Center, NoEscape
import pandas as pd
import numpy as np
#region Create document with the necessary packages
# Create a document with the necessary packages
doc = Document(documentclass='beamer')
doc.packages.append(Package('booktabs'))
doc.packages.append(Package('fontsize'))
doc.packages.append(Package('geometry'))
doc.packages.append(Package('xcolor'))
#endregion
#region Adding colors
doc.append(Command('definecolor', arguments=['Crayola', 'HTML', '2172F2']))
doc.append(Command('definecolor', arguments=['Sea Green', 'HTML', '2CB96D']))
doc.append(Command('definecolor', arguments=['primary', 'HTML', '0065f2']))
doc.append(Command('definecolor', arguments=['success', 'HTML', '28b440']))
doc.append(Command('definecolor', arguments=['warning', 'HTML', 'e4b80b']))
doc.append(Command('definecolor', arguments=['error', 'HTML', 'f43319']))
#endregion
#region Dummy Data
df = pd.DataFrame()
df["name"] = ["Nick","Gaetano", "George", "Jason","Death", "God", 'Stan', "Satan","Beel","Nick","Gaetano", "George", "Jason","Death", "God", 'Stan', "Satan","Beel"]
df["Restaurant Manager"] = ["Sam","Mason", "Sam", "Mason","Mason","Sam","Sam","Sam","Mason","Sam","Mason", "Sam", "Mason","Mason","Sam","Sam","Sam","Mason"]
df["Score"] = np.random.randint(5,30,size=18)
df['Percentile Rank'] = np.random.randint(5,101,size=18)
df["Restaurant Name"] = ["Elise","Bob","Death","Elise","Bob","Death","Elise","Bob","Death","Elise","Bob","Death","Elise","Bob","Death","Elise","Bob","Death"]
#endregion
#endregion
#region Group the data frame by the Restaurant Name column
df_grouped = df.groupby('Restaurant Name')
#endregion
# Create a minipage environment
minipage = MiniPage(width=NoEscape(r'\linewidth'))
#region Iterate through the groups and create a tabular environment for each restaurant
for restaurant, df_group in df_grouped:
# Create a tabular environment
tabular = LongTable('|cp{3cm}cc|') # l, c, and r for left, center, and right alignment, respectively; p for a paragraph column; and | for vertical lines.
# Add the table header row, using the defined color
tabular.add_hline()
tabular.add_row((MultiColumn(4, align='|c|', data=restaurant),), color="blue")
tabular.add_hline()
# Group the data frame by the Restaurant Manager column
df_manager_grouped = df_group.groupby('Restaurant Manager')
# Iterate through the groups and add the rows to the tabular environment
rank = 1
for group, df_manager_group in df_manager_grouped:
tabular.add_hline()
tabular.add_row((MultiColumn(4, align='c', data=NoEscape("\\vspace{-2ex}\\fontsize{1}{1}\selectfont")),))
tabular.add_hline()
tabular.add_row((MultiColumn(4, align='|c|', data=group),))
tabular.add_hline()
tabular.add_row(('Rank', 'Name', 'Score', '%-ile Rank'))
rank = 1
for _, row in df_manager_group.iterrows():
tabular.add_row((str(rank), NoEscape(r'\raggedright ' + row['name']), str(row['Score']), str(row['Percentile Rank'])))
rank += 1
num_rows = len(df_manager_group)
for i in range(3 - num_rows):
tabular.add_row((str(rank), 'N/A', 'N/A', 'N/A'))
# Add the final hline and add the tabular environment to the minipage
tabular.add_hline()
minipage.append(tabular)
minipage.append(Command('hspace', '2mm'))
#endregion
doc.append(minipage)
# Generate the PDF
doc.generate_pdf("Report/", clean_tex= True, compiler="pdflatex")
Whenever I try to run my code, I get this error ! LaTeX Error: Option clash for package xcolor.
However, what is interesting is that when I change the document class to article this error disappears. I have tried to append the package directly, I have passed some options to both the document and the package to add the dvipsnames
(i.e. doc.packages.append(Package('xcolor', options=['dvipsnames']))
but alas to no avail...
I would really like to use my own colors that I have defined above.
Any suggestions would be much appreciated
Edit: Here is the .tex file
\documentclass{beamer}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{booktabs}%
\usepackage{fontsize}%
\usepackage{geometry}%
\usepackage[dvipsnames]{xcolor}%
\usepackage{ragged2e}%
\usepackage{longtable}%
\usepackage[table]{xcolor}%
%
%
%
\begin{document}%
\normalsize%
\definecolor{Crayola}{HTML}{2172F2}%
\definecolor{Sea Green}{HTML}{2CB96D}%
\definecolor{primary}{HTML}{0065f2}%
\definecolor{success}{HTML}{28b440}%
\definecolor{warning}{HTML}{e4b80b}%
\definecolor{error}{HTML}{f43319}%
\begin{minipage}{\linewidth}%
\begin{longtable}{|cp{3cm}cc|}%
\hline%
\rowcolor{Red}%
\multicolumn{4}{|c|}{Bob}\\%
\hline%
\hline%
\multicolumn{4}{c}{\vspace{-2ex}\fontsize{1}{1}\selectfont}\\%
\hline%
\multicolumn{4}{|c|}{Mason}\\%
\hline%
Rank&Name&Score&\%{-}ile Rank\\%
1&\raggedright Gaetano&5&17\\%
2&\raggedright Death&8&74\\%
3&\raggedright Gaetano&19&32\\%
4&\raggedright Death&16&48\\%
\hline%
\multicolumn{4}{c}{\vspace{-2ex}\fontsize{1}{1}\selectfont}\\%
\hline%
\multicolumn{4}{|c|}{Sam}\\%
\hline%
Rank&Name&Score&\%{-}ile Rank\\%
1&\raggedright Satan&14&20\\%
2&\raggedright Satan&19&18\\%
3&N/A&N/A&N/A\\%
\hline%
\end{longtable}%
\hspace{2mm}%
\begin{longtable}{|cp{3cm}cc|}%
\hline%
\rowcolor{Red}%
\multicolumn{4}{|c|}{Death}\\%
\hline%
\hline%
\multicolumn{4}{c}{\vspace{-2ex}\fontsize{1}{1}\selectfont}\\%
\hline%
\multicolumn{4}{|c|}{Mason}\\%
\hline%
Rank&Name&Score&\%{-}ile Rank\\%
1&\raggedright Beel&11&94\\%
2&\raggedright Beel&8&89\\%
3&N/A&N/A&N/A\\%
\hline%
\multicolumn{4}{c}{\vspace{-2ex}\fontsize{1}{1}\selectfont}\\%
\hline%
\multicolumn{4}{|c|}{Sam}\\%
\hline%
Rank&Name&Score&\%{-}ile Rank\\%
1&\raggedright George&20&43\\%
2&\raggedright God&19&72\\%
3&\raggedright George&16&23\\%
4&\raggedright God&13&94\\%
\hline%
\end{longtable}%
\hspace{2mm}%
\begin{longtable}{|cp{3cm}cc|}%
\hline%
\rowcolor{Red}%
\multicolumn{4}{|c|}{Elise}\\%
\hline%
\hline%
\multicolumn{4}{c}{\vspace{-2ex}\fontsize{1}{1}\selectfont}\\%
\hline%
\multicolumn{4}{|c|}{Mason}\\%
\hline%
Rank&Name&Score&\%{-}ile Rank\\%
1&\raggedright Jason&7&73\\%
2&\raggedright Jason&27&93\\%
3&N/A&N/A&N/A\\%
\hline%
\multicolumn{4}{c}{\vspace{-2ex}\fontsize{1}{1}\selectfont}\\%
\hline%
\multicolumn{4}{|c|}{Sam}\\%
\hline%
Rank&Name&Score&\%{-}ile Rank\\%
1&\raggedright Nick&14&34\\%
2&\raggedright Stan&26&34\\%
3&\raggedright Nick&14&99\\%
4&\raggedright Stan&15&28\\%
\hline%
\end{longtable}%
\hspace{2mm}%
\end{minipage}%
\end{document}
You can avoid the problem by using the xcolor={dvipsnames,table}
class option:
from pylatex import Tabular, Document, Package, MultiColumn, SmallText, Command, MiniPage, Center, NoEscape, LongTable, utils
import pandas as pd
import numpy as np
#region Create document with the necessary packages
# Create a document with the necessary packages
from pylatex import Document
doc = Document(documentclass='beamer', document_options=NoEscape(r'xcolor={dvipsnames,table}'))
doc.packages.append(Package('booktabs'))
doc.packages.append(Package('fontsize'))
doc.packages.append(Package('geometry'))
doc.packages.append(Package('xcolor'))
#endregion
#region Adding colors
doc.append(Command('definecolor', arguments=['Crayola', 'HTML', '2172F2']))
doc.append(Command('definecolor', arguments=['Sea Green', 'HTML', '2CB96D']))
doc.append(Command('definecolor', arguments=['primary', 'HTML', '0065f2']))
doc.append(Command('definecolor', arguments=['success', 'HTML', '28b440']))
doc.append(Command('definecolor', arguments=['warning', 'HTML', 'e4b80b']))
doc.append(Command('definecolor', arguments=['error', 'HTML', 'f43319']))
#endregion
#region Dummy Data
df = pd.DataFrame()
df["name"] = ["Nick","Gaetano", "George", "Jason","Death", "God", 'Stan', "Satan","Beel","Nick","Gaetano", "George", "Jason","Death", "God", 'Stan', "Satan","Beel"]
df["Restaurant Manager"] = ["Sam","Mason", "Sam", "Mason","Mason","Sam","Sam","Sam","Mason","Sam","Mason", "Sam", "Mason","Mason","Sam","Sam","Sam","Mason"]
df["Score"] = np.random.randint(5,30,size=18)
df['Percentile Rank'] = np.random.randint(5,101,size=18)
df["Restaurant Name"] = ["Elise","Bob","Death","Elise","Bob","Death","Elise","Bob","Death","Elise","Bob","Death","Elise","Bob","Death","Elise","Bob","Death"]
#endregion
#endregion
#region Group the data frame by the Restaurant Name column
df_grouped = df.groupby('Restaurant Name')
#endregion
# Create a minipage environment
minipage = MiniPage(width=NoEscape(r'\linewidth'))
#region Iterate through the groups and create a tabular environment for each restaurant
for restaurant, df_group in df_grouped:
# Create a tabular environment
tabular = LongTable(table_spec='|cp{3cm}cc|') # l, c, and r for left, center, and right alignment, respectively; p for a paragraph column; and | for vertical lines.
# Add the table header row, using the defined color
tabular.add_hline()
tabular.add_row((MultiColumn(4, align='|c|', data=restaurant),), color="blue")
tabular.add_hline()
# Group the data frame by the Restaurant Manager column
df_manager_grouped = df_group.groupby('Restaurant Manager')
# Iterate through the groups and add the rows to the tabular environment
rank = 1
for group, df_manager_group in df_manager_grouped:
tabular.add_hline()
tabular.add_row((MultiColumn(4, align='c', data=NoEscape("\\vspace{-2ex}\\fontsize{1}{1}\selectfont")),))
tabular.add_hline()
tabular.add_row((MultiColumn(4, align='|c|', data=group),))
tabular.add_hline()
tabular.add_row(('Rank', 'Name', 'Score', '%-ile Rank'))
rank = 1
for _, row in df_manager_group.iterrows():
tabular.add_row((str(rank), NoEscape(r'\raggedright ' + row['name']), str(row['Score']), str(row['Percentile Rank'])))
rank += 1
num_rows = len(df_manager_group)
for i in range(3 - num_rows):
tabular.add_row((str(rank), 'N/A', 'N/A', 'N/A'))
# Add the final hline and add the tabular environment to the minipage
tabular.add_hline()
minipage.append(tabular)
minipage.append(Command('hspace', '2mm'))
#endregion
doc.append(minipage)
# Generate the PDF
doc.generate_pdf("./", compiler="pdflatex")