Skip to content

Conversation

@PhilippImhof
Copy link
Member

@PhilippImhof PhilippImhof commented Jun 3, 2020

I have completely rewritten the code for managing the TeX template. This is a new PR basically bringing the same changes as the old one, but without the mess...

  • Full backwards compatibility: if you do nothing, everything will work as before, the old default template will be used for LaTeX or XeLaTeX
  • New command line option --tex_template allows for a custom tex file
  • New class TexTemplate starting with the current default values for maximum backwards compatibility.
  • New class has methods to append or prepend custom packages and/or custom preamble commands.
  • Added method register_tex_template allows to register a new or modified template

Example (included)

from manim import *

# Test cases:
# 1. manim customtex.py ExampleFileScene -pl
#       --> should fail, because \vv is not defined
#
# 2. manim customtex.py ExampleFileScene --tex_template custom_template.tex -pl
#       --> should succeed as custom template includes package esvect (which defines \vv)
#
# 3. manim customtex.py ExampleClassScene -pl
#       --> should succeed as the package esvect is included in template object

class ExampleFileScene(Scene):
    def construct(self):
        text=TexMobject(r"\vv{vb}")
        #text=TextMobject(r"$\vv{vb}$")
        self.play(Write(text))

class ExampleClassScene(Scene):
    def construct(self):
        tpl=TexTemplate()
        tpl.append_package(["esvect",["f"]])
        config.register_tex_template(tpl)

        #text=TextMobject(r"$\vv{vb}$")
        text=TexMobject(r"\vv{vb}")
        self.play(Write(text))

As you may have guessed, the syntax for packages is

  • append_package("package") if you do not have any options
  • append_package(["package",["option1","option2=value2"]]) if you do have options to give

When using the command line approach with a custom template file, the simpler class TexTemplateFromFile is used. It does not allow adding/removing packages from within the scene. However, I do not think there is a need for it, because if one specifies a custom template file, we can surely assume they will not need to dynamically adapt it later on.

@PhilippImhof PhilippImhof marked this pull request as draft June 3, 2020 12:03
@PhilippImhof PhilippImhof marked this pull request as ready for review June 3, 2020 12:09
@PgBiel PgBiel requested a review from leotrs June 3, 2020 14:34
@PgBiel PgBiel added the new feature Enhancement specifically adding a new feature (feature request should be used for issues instead) label Jun 3, 2020
@leotrs
Copy link
Contributor

leotrs commented Jun 4, 2020

In the future, it will be interesting to monitor whether TexTemplate is actually being used to change the tex defaults during a scene. I can see how this could be used, but it's somewhat of a niche use-case, no? I think similar/equivalent functionality could be used by having multiple TexTemplateFromFile objects, each initialized with a different template file. So instead of building a tex file from within the scene, a user could just have two different TexTemplateFromFile objects. I think this is cleaner so that we are not put in a position of continuing to add more and more functionality to TexTemplate above and beyond adding/removing packages.

@leotrs
Copy link
Contributor

leotrs commented Jun 4, 2020

I'm merging this for now as it's the same as a PR that was already merged and it looks a lot cleaner in terms of commits/git/history etc. Thanks @PhilippImhof ! Hopefully we did it right this time ;)

@leotrs leotrs merged commit 0261b83 into ManimCommunity:master Jun 4, 2020
@PhilippImhof
Copy link
Member Author

PhilippImhof commented Jun 4, 2020

@leotrs I fully agree. Honestly, I cannot imagine a single case where one would want to add packages or define macros in the middle of a scene. When I started writing it, the only way to add packages was to change a file that was buried somewhere in the manim tree and was likely to be overwritten with the next update. That part was resolved with the --tex_template parameter.

I then thought it would be nice to be able to define the LaTeX template at the same place where you create your scene, because that way there would be no need for a separate template that might get lost later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature Enhancement specifically adding a new feature (feature request should be used for issues instead)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants