Skip to content

Commit 899d416

Browse files
authored
Merge pull request #483 from cobordism/refactor-tex-templates
Simplifying TexTemplates and Creating Template Library
2 parents 2304505 + a6b553c commit 899d416

File tree

12 files changed

+1581
-323
lines changed

12 files changed

+1581
-323
lines changed

docs/source/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Examples
77
examples/annotations
88
examples/plots
99
examples/text
10+
examples/tex
1011
examples/formulas
1112
examples/3d
1213
examples/camera_settings

docs/source/examples/tex.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
LaTeX
2+
=================================
3+
4+
.. manim:: Example1LaTeX
5+
:save_last_frame:
6+
7+
class Example1LaTeX(Scene):
8+
def construct(self):
9+
tex = Tex(r'$\xrightarrow{Hello}$ \LaTeX').scale(3)
10+
self.add(tex)
11+
12+
.. manim:: Example2LaTeX
13+
:save_last_frame:
14+
15+
class Example2LaTeX(Scene):
16+
def construct(self):
17+
tex = Tex(r'$\mathtt{Hello}$ \LaTeX', color=BLUE).scale(3)
18+
self.add(tex)
19+
20+
21+
.. manim:: Example3LaTeX
22+
:save_last_frame:
23+
24+
class Example3LaTeX(Scene):
25+
def construct(self):
26+
myTemplate = TexTemplate()
27+
myTemplate.add_to_preamble(r"\usepackage{mathrsfs}")
28+
tex = Tex(r'$\mathscr{H}ello$ \LaTeX}', tex_template=myTemplate).scale(3)
29+
self.add(tex)
30+
31+
32+
.. manim:: Example4LaTeX
33+
:save_last_frame:
34+
35+
class Example4LaTeX(Scene):
36+
def construct(self):
37+
tex = Tex('Hello', '$\\bigstar$', '\LaTeX').scale(3)
38+
tex.set_color_by_tex('igsta', RED)
39+
self.add(tex)
40+
41+
.. manim:: Example5LaTeX
42+
:save_last_frame:
43+
44+
class Example5LaTeX(Scene):
45+
def construct(self):
46+
tex = Tex('Hello \LaTeX', tex_template=TexFontTemplates.french_cursive).scale(3)
47+
self.add(tex)
48+
49+
.. manim:: Example5bLaTeX
50+
:save_last_frame:
51+
52+
class Example5bLaTeX(Scene):
53+
def construct(self):
54+
templateForFrenchCursive = TexTemplate(
55+
preamble=r"""
56+
\usepackage[english]{babel}
57+
\usepackage{amsmath}
58+
\usepackage{amssymb}
59+
\usepackage[T1]{fontenc}
60+
\usepackage[default]{frcursive}
61+
\usepackage[eulergreek,noplusnominus,noequal,nohbar,nolessnomore,noasterisk]{mathastext}
62+
""")
63+
tex = Tex('Hello \LaTeX', tex_template=templateForFrenchCursive).scale(3)
64+
self.add(tex)
65+
66+
.. manim:: Example6LaTeX
67+
:save_last_frame:
68+
69+
class Example6LaTeX(Scene):
70+
def construct(self):
71+
tex = Tex('Hello 你好 \LaTeX', tex_template=TexTemplateLibrary.ctex).scale(3)
72+
self.add(tex)
73+
74+

docs/source/reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Utilities
118118
~utils.space_ops
119119
~utils.strings
120120
~utils.tex
121+
~utils.tex_templates
121122
~utils.tex_file_writing
122123

123124

example_scenes/advanced_tex_fonts.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from manim import *
2+
3+
# French Cursive LaTeX font example from http://jf.burnol.free.fr/showcase.html
4+
5+
# Example 1 Manually creating a Template
6+
7+
TemplateForFrenchCursive = TexTemplate(
8+
preamble=r"""
9+
\usepackage[english]{babel}
10+
\usepackage{amsmath}
11+
\usepackage{amssymb}
12+
\usepackage[T1]{fontenc}
13+
\usepackage[default]{frcursive}
14+
\usepackage[eulergreek,noplusnominus,noequal,nohbar,%
15+
nolessnomore,noasterisk]{mathastext}
16+
"""
17+
)
18+
19+
20+
def FrenchCursive(*tex_strings, **kwargs):
21+
return Tex(*tex_strings, tex_template=TemplateForFrenchCursive, **kwargs)
22+
23+
24+
class TexFontTemplateManual(Scene):
25+
"""An example scene that uses a manually defined TexTemplate() object to create
26+
LaTeX output in French Cursive font"""
27+
28+
def construct(self):
29+
self.add(Tex("Tex Font Example").to_edge(UL))
30+
self.play(ShowCreation(FrenchCursive("$f: A \\longrightarrow B$").shift(UP)))
31+
self.play(
32+
ShowCreation(FrenchCursive("Behold! We can write math in French Cursive"))
33+
)
34+
self.wait(1)
35+
self.play(
36+
ShowCreation(
37+
Tex(
38+
"See more font templates at \\\\ http://jf.burnol.free.fr/showcase.html"
39+
).shift(2 * DOWN)
40+
)
41+
)
42+
self.wait(2)
43+
44+
45+
# Example 2, using a Template from the collection
46+
47+
48+
class TexFontTemplateLibrary(Scene):
49+
"""An example scene that uses TexTemplate objects from the TexFontTemplates collection
50+
to create sample LaTeX output in every font that will compile on the local system.
51+
52+
Please Note:
53+
Many of the in the TexFontTemplates collection require that specific fonts
54+
are installed on your local machine.
55+
For example, choosing the template TexFontTemplates.comic_sans will
56+
not compile if the Comic Sans Micrososft font is not installed.
57+
58+
This scene will only render those Templates that do not cause a TeX
59+
compilation error on your system. Furthermore, some of the ones that do render,
60+
may still render incorrectly. This is beyond the scope of manim.
61+
Feel free to experiment.
62+
"""
63+
64+
def construct(self):
65+
def write_one_line(template):
66+
x = Tex(template.description, tex_template=template).shift(UP)
67+
self.play(ShowCreation(x))
68+
self.wait(1)
69+
self.play(FadeOut(x))
70+
71+
examples = [
72+
TexFontTemplates.american_typewriter, # "American Typewriter"
73+
TexFontTemplates.antykwa, # "Antykwa Półtawskiego (TX Fonts for Greek and math symbols)"
74+
TexFontTemplates.apple_chancery, # "Apple Chancery"
75+
TexFontTemplates.auriocus_kalligraphicus, # "Auriocus Kalligraphicus (Symbol Greek)"
76+
TexFontTemplates.baskervald_adf_fourier, # "Baskervald ADF with Fourier"
77+
TexFontTemplates.baskerville_it, # "Baskerville (Italic)"
78+
TexFontTemplates.biolinum, # "Biolinum"
79+
TexFontTemplates.brushscriptx, # "BrushScriptX-Italic (PX math and Greek)"
80+
TexFontTemplates.chalkboard_se, # "Chalkboard SE"
81+
TexFontTemplates.chalkduster, # "Chalkduster"
82+
TexFontTemplates.comfortaa, # "Comfortaa"
83+
TexFontTemplates.comic_sans, # "Comic Sans MS"
84+
TexFontTemplates.droid_sans, # "Droid Sans"
85+
TexFontTemplates.droid_sans_it, # "Droid Sans (Italic)"
86+
TexFontTemplates.droid_serif, # "Droid Serif"
87+
TexFontTemplates.droid_serif_px_it, # "Droid Serif (PX math symbols) (Italic)"
88+
TexFontTemplates.ecf_augie, # "ECF Augie (Euler Greek)"
89+
TexFontTemplates.ecf_jd, # "ECF JD (with TX fonts)"
90+
TexFontTemplates.ecf_skeetch, # "ECF Skeetch (CM Greek)"
91+
TexFontTemplates.ecf_tall_paul, # "ECF Tall Paul (with Symbol font)"
92+
TexFontTemplates.ecf_webster, # "ECF Webster (with TX fonts)"
93+
TexFontTemplates.electrum_adf, # "Electrum ADF (CM Greek)"
94+
TexFontTemplates.epigrafica, # Epigrafica
95+
TexFontTemplates.fourier_utopia, # "Fourier Utopia (Fourier upright Greek)"
96+
TexFontTemplates.french_cursive, # "French Cursive (Euler Greek)"
97+
TexFontTemplates.gfs_bodoni, # "GFS Bodoni"
98+
TexFontTemplates.gfs_didot, # "GFS Didot (Italic)"
99+
TexFontTemplates.gfs_neoHellenic, # "GFS NeoHellenic"
100+
TexFontTemplates.gnu_freesans_tx, # "GNU FreeSerif (and TX fonts symbols)"
101+
TexFontTemplates.gnu_freeserif_freesans, # "GNU FreeSerif and FreeSans"
102+
TexFontTemplates.helvetica_fourier_it, # "Helvetica with Fourier (Italic)"
103+
TexFontTemplates.latin_modern_tw_it, # "Latin Modern Typewriter Proportional (CM Greek) (Italic)"
104+
TexFontTemplates.latin_modern_tw, # "Latin Modern Typewriter Proportional"
105+
TexFontTemplates.libertine, # "Libertine"
106+
TexFontTemplates.libris_adf_fourier, # "Libris ADF with Fourier"
107+
TexFontTemplates.minion_pro_myriad_pro, # "Minion Pro and Myriad Pro (and TX fonts symbols)"
108+
TexFontTemplates.minion_pro_tx, # "Minion Pro (and TX fonts symbols)"
109+
TexFontTemplates.new_century_schoolbook, # "New Century Schoolbook (Symbol Greek)"
110+
TexFontTemplates.new_century_schoolbook_px, # "New Century Schoolbook (Symbol Greek, PX math symbols)"
111+
TexFontTemplates.noteworthy_light, # "Noteworthy Light"
112+
TexFontTemplates.palatino, # "Palatino (Symbol Greek)"
113+
TexFontTemplates.papyrus, # "Papyrus"
114+
TexFontTemplates.romande_adf_fourier_it, # "Romande ADF with Fourier (Italic)"
115+
TexFontTemplates.slitex, # "SliTeX (Euler Greek)"
116+
TexFontTemplates.times_fourier_it, # "Times with Fourier (Italic)"
117+
TexFontTemplates.urw_avant_garde, # "URW Avant Garde (Symbol Greek)"
118+
TexFontTemplates.urw_zapf_chancery, # "URW Zapf Chancery (CM Greek)"
119+
TexFontTemplates.venturis_adf_fourier_it, # "Venturis ADF with Fourier (Italic)"
120+
TexFontTemplates.verdana_it, # "Verdana (Italic)"
121+
TexFontTemplates.vollkorn_fourier_it, # "Vollkorn with Fourier (Italic)"
122+
TexFontTemplates.vollkorn, # "Vollkorn (TX fonts for Greek and math symbols)"
123+
TexFontTemplates.zapf_chancery, # "Zapf Chancery"
124+
]
125+
126+
self.add(Tex("Tex Font Template Example").to_edge(UL))
127+
128+
for font in examples:
129+
try:
130+
write_one_line(font)
131+
except:
132+
print("FAILURE on ", font.description, " - skipping.")
133+
134+
self.play(
135+
ShowCreation(
136+
Tex(
137+
"See more font templates at \\\\ http://jf.burnol.free.fr/showcase.html"
138+
).shift(2 * DOWN)
139+
)
140+
)
141+
self.wait(2)

example_scenes/custom_template.tex

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@
44
\usepackage{amsmath}
55
\usepackage{amssymb}
66
\usepackage[f]{esvect}
7-
\usepackage{dsfont}
8-
\usepackage{setspace}
9-
\usepackage{tipa}
10-
\usepackage{relsize}
11-
\usepackage{textcomp}
12-
\usepackage{mathrsfs}
13-
\usepackage{calligra}
14-
\usepackage{wasysym}
15-
\usepackage{ragged2e}
16-
\usepackage{physics}
17-
\usepackage{xcolor}
18-
\usepackage{microtype}
19-
\DisableLigatures{encoding = *, family = * }
20-
%\usepackage[UTF8]{ctex}
21-
\linespread{1}
227

238
\begin{document}
249

example_scenes/customtex.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class TexTemplateFromCLI(Scene):
1212

1313
def construct(self):
1414
text = MathTex(r"\vv{vb}")
15-
# text=Tex(r"$\vv{vb}$")
1615
self.play(Write(text))
16+
self.wait(1)
1717

1818

1919
class InCodeTexTemplate(Scene):
@@ -22,10 +22,20 @@ class InCodeTexTemplate(Scene):
2222
"""
2323

2424
def construct(self):
25-
tpl = TexTemplate()
26-
tpl.append_package(["esvect", ["f"]])
27-
config["tex_template"] = tpl
25+
# Create a new template
26+
myTemplate = TexTemplate()
2827

29-
# text=Tex(r"$\vv{vb}$")
30-
text = MathTex(r"\vv{vb}")
28+
# Add packages to the template
29+
myTemplate.add_to_preamble(r"\usepackage{esvect}")
30+
31+
# Set the compiler and output format (default: latex and .dvi)
32+
# possible tex compilers: "latex", "pdflatex", "xelatex", "lualatex", "luatex"
33+
# possible output formats: ".dvi", ".pdf", and ".xdv"
34+
myTemplate.tex_compiler = "pdflatex"
35+
myTemplate.output_format = ".pdf"
36+
37+
# To use this template in a Tex() or MathTex() object
38+
# use the keyword argument tex_template
39+
text = MathTex(r"\vv{vb}", tex_template=myTemplate)
3140
self.play(Write(text))
41+
self.wait(1)

manim/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@
7979
from .utils.space_ops import *
8080
from .utils.strings import *
8181
from .utils.tex import *
82+
from .utils.tex_templates import *

manim/mobject/svg/tex_mobject.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ...utils.strings import split_string_list_to_isolate_substrings
2828
from ...utils.tex_file_writing import tex_to_svg_file
2929
from ...utils.color import BLACK
30+
from ...utils.tex import TexTemplate
3031

3132
TEX_MOB_SCALE_FACTOR = 0.05
3233

@@ -57,20 +58,21 @@ class SingleStringMathTex(SVGMobject):
5758
"height": None,
5859
"organize_left_to_right": False,
5960
"alignment": "",
60-
"type": "tex",
61-
"template": None,
61+
"tex_environment": "align*",
62+
"tex_template": None,
6263
}
6364

6465
def __init__(self, tex_string, **kwargs):
6566
digest_config(self, kwargs)
66-
if self.template is None:
67-
self.template = kwargs.get("tex_template", config["tex_template"])
67+
if self.tex_template is None:
68+
self.tex_template = kwargs.get("tex_template", config["tex_template"])
69+
6870
assert isinstance(tex_string, str)
6971
self.tex_string = tex_string
7072
file_name = tex_to_svg_file(
7173
self.get_modified_expression(tex_string),
72-
self.type,
73-
tex_template=self.template,
74+
environment=self.tex_environment,
75+
tex_template=self.tex_template,
7476
)
7577
SVGMobject.__init__(self, file_name=file_name, **kwargs)
7678
if self.height is None:
@@ -190,6 +192,7 @@ def construct(self):
190192
"arg_separator": " ",
191193
"substrings_to_isolate": [],
192194
"tex_to_color_map": {},
195+
"tex_environment": "align*",
193196
}
194197

195198
def __init__(self, *tex_strings, **kwargs):
@@ -310,7 +313,7 @@ class Tex(MathTex):
310313
CONFIG = {
311314
"alignment": "\\centering",
312315
"arg_separator": "",
313-
"type": "text",
316+
"tex_environment": None,
314317
}
315318

316319

manim/utils/module_ops.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sys
88
import types
9+
import re
910

1011

1112
def get_module(file_name):

0 commit comments

Comments
 (0)