Pylatex - Beamer -包xcolor的选项冲突,无法使用python解决

wqsoz72f  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(231)

请在下面找到我的代码

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")

每当我尝试运行我的代码,我得到这个错误! LaTeX Error: Option clash for package xcolor.然而,有趣的是,当我改变文档类文章这个错误消失了.我曾试图直接追加包,我已经传递了一些选项给文档和包添加dvipsnames(即doc.packages.append(Package('xcolor', options=['dvipsnames'])),但可惜没有用...
我真的很想使用我自己的颜色,我已经定义以上。
如有任何建议,我们将不胜感激
编辑:下面是.tex文件

\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}
8dtrkrch

8dtrkrch1#

您可以使用xcolor={dvipsnames,table} class选项来避免此问题:

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")

相关问题