class TexTable:
    def __init__(
        self,
        df,
        filepath,
        table_string=None,
        booktabs=True,
        logging=True,
        options_packages=[
            ("locale=DE", "siunitx"),
            ("", "booktabs"),
            ("ngerman", "babel"),
        ],
    ):
        r"""Fill TexTable with DataFrame.

        Parameters
        ----------
        df : pandas.DataFrame
            Data.
        filepath : str
            Complete filepath without extensions to name PDF and TEX files.
        table_string: str, None
            String in \tabular{table_string}.
            If None, default to 'S' for number of fields.
        booktabs : bool, True
            Using booktabs style horizontal lines.
        logging : bool, True
            Log if compilation occurs.
        options_packages : list[tuple]
            Additional packages with options to include in header of TEX file.
        """
        self.df = df
        self.filepath = filepath
        self.logging = logging

        if table_string is None:
            table_string = "S " * df.shape[1]

        self.table = Tabular(table_string,
                             booktabs=booktabs,
                             width=df.shape[1])
        self.table.escape = False

        self.document = Document(self.filepath,
                                 documentclass="standalone",
                                 document_options="preview")
        for opt_pack in options_packages:
            self.document.packages.append(
                NoEscape(r"\usepackage[{}]".format(opt_pack[0]) + r"{" +
                         str(opt_pack[1]) + r"}"))

        self.table.add_row(self.df.columns)

        self.table.add_hline()  # \midrule

        for i in range(self.df.shape[0]):
            self.table.add_row(self.df.loc[i].values)

        self.document.append(NoEscape(self.table.dumps()))

    def generate_tex(self):
        """Generate tex file."""
        self.document.generate_tex(self.filepath)

    def generate_pdf(self, clean_tex=False, silent=True):
        """Compile PDF document if content is new and keep tex file.

        Parameters
        ----------
        clean_tex : bool, False
            Remove TEX file after PDF comilation.
        silent : bool, False
            Dispaly LaTeX compilation output.
        """
        try:
            with open(self.filepath + ".tex", "r") as texfile:
                if texfile.read() == self.document.dumps():
                    if (os.stat(self.filepath + ".tex").st_mtime >
                            os.stat(self.filepath + ".pdf").st_mtime):
                        if self.logging:
                            print("Compile PDF ({})".format(
                                "TEX is newer than PDF."))
                        self.document.generate_pdf(clean_tex=clean_tex,
                                                   silent=silent)
                    else:  # PDF is newer than TEX
                        if self.logging:
                            print("Don't compile PDF ({})".format(
                                "No new content and PDF is newer than TEX."))
                        pass
                else:  # TEX will be overwritten with new content
                    if self.logging:
                        print("Compile PDF ({})".format("New content in TEX."))
                    self.document.generate_pdf(clean_tex=clean_tex,
                                               silent=silent)
        except FileNotFoundError:
            if self.logging:
                print("Compile PDF ({})".format("TEX or PDF does not exist."))
            self.document.generate_pdf(clean_tex=clean_tex, silent=silent)