kerrickstaley / genanki

A Python 3 library for generating Anki decks
MIT License
1.99k stars 150 forks source link

Is it possible to change the LaTex premable on a Model? #65

Closed LoganLim2271 closed 3 years ago

LoganLim2271 commented 3 years ago

Hi there, thank you for this great package. When I was looking into the code for Model.to_json in model.py I noticed that the LaTex preamble for the Note type is constant.

return {
      "css": self.css,
      "did": deck_id,
      "flds": self.fields,
      "id": str(self.model_id),
      "latexPost": "\\end{document}",
      "latexPre": "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage{amssymb,amsmath}\n"
                  "\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
      "mod": now_ts,
      "name": self.name,
      "req": self._req,
      "sortf": 0,
      "tags": [],
      "tmpls": self.templates,
      "type": self.model_type,
      "usn": -1,
      "vers": []
    }

Is there some way to change the LaTex preamble for a Model object to use more LaTex packages & define environments?

bary12 commented 3 years ago

Can be worked around by extending genanki.Model and overriding to_json. Here is an example:


class CustomLatexModel(genanki.Model):
    def __init__(self, latex_pre = None, latex_post = None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.latex_pre = latex_pre
        self.latex_post = latex_post

    def to_json(self, *args, **kwargs):
        json = super().to_json(*args, **kwargs)
        if self.latex_pre is not None:
            json['latexPre'] = self.latex_pre
        if self.latex_post is not None:
            json['latexPost'] = self.latex_post

        return json
kerrickstaley commented 3 years ago

In genanki 0.11.0 you can pass latex_pre and latex_post to Model.__init__.