Skip to content

Commit d3ff3fe

Browse files
cobordismleotrsbehackl
authored
added some words to documentation LaTeX examples (#558)
* added some words to documentation LaTeX examples * fixes * Update docs/source/examples/tex.rst Co-authored-by: Leo Torres <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Leo Torres <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Leo Torres <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Leo Torres <[email protected]> * backticks * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * double backticks for rst literals * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Update docs/source/examples/tex.rst Co-authored-by: Benjamin Hackl <[email protected]> * Merge Text and LaTeX examples Co-authored-by: Aron Fischer <co-bordism on gitlab> Co-authored-by: Leo Torres <[email protected]> Co-authored-by: Benjamin Hackl <[email protected]>
1 parent 3b6519a commit d3ff3fe

File tree

3 files changed

+114
-37
lines changed

3 files changed

+114
-37
lines changed

docs/source/examples.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Examples
66
examples/shapes_images_positions
77
examples/annotations
88
examples/plots
9-
examples/text
109
examples/tex
1110
examples/formulas
1211
examples/3d

docs/source/examples/tex.rst

Lines changed: 114 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
1+
Text and LaTeX
2+
===============
3+
4+
5+
Text
6+
--------------
7+
The simplest way to add text to you animation is to use the :class:`~.Text` class. It uses the Cairo library to render text.
8+
A newer addition to manim is the :class:`~.PangoText` class, which uses the Pango library.
9+
10+
The Text() mobject
11+
+++++++++++++++++++
12+
13+
.. manim:: Example1Text
14+
:save_last_frame:
15+
16+
class Example1Text(Scene):
17+
def construct(self):
18+
text = Text('Hello world').scale(3)
19+
self.add(text)
20+
21+
For more examples, see: :class:`~.Text`.
22+
23+
The PangoText() mobject
24+
+++++++++++++++++++++++
25+
26+
The :class:`~.PangoText` mobject uses the Pango library to render text. Use this whenever you want to use non-English alphabets like `你好` or `こんにちは` or `안녕하세요` or `مرحبا بالعالم`.
27+
28+
129
LaTeX
2-
=================================
30+
-------------------
31+
32+
The Tex() mobject
33+
+++++++++++++++++++
34+
Just as you can use :class:`~.Text` to add text to your videos, you can use :class:`~.Tex` to insert LaTeX.
35+
36+
.. manim:: ExampleLaTeX
37+
:save_last_frame:
38+
39+
class ExampleLaTeX(Scene):
40+
def construct(self):
41+
tex = Tex(r'\LaTeX').scale(3)
42+
self.add(tex)
43+
44+
Note that we are using a raw string (``r'---'``) instead of a regular string (``'---'``).
45+
This is because TeX code uses a lot of special characters - like ``\`` for example -
46+
that have special meaning within a regular python string. An alternative would have
47+
been to write ``\\`` as in ``Tex('\\LaTeX')``.
48+
49+
The MathTex() mobject
50+
++++++++++++++++++++++
51+
Anything enclosed in ``$`` signs is interpreted as maths-mode:
352

453
.. manim:: Example1LaTeX
554
:save_last_frame:
@@ -9,14 +58,45 @@ LaTeX
958
tex = Tex(r'$\xrightarrow{Hello}$ \LaTeX').scale(3)
1059
self.add(tex)
1160

61+
Whereas in a :class:`~.MathTex` mobject everything is math-mode by default and you would use ``\text{}`` to
62+
insert regular text:
63+
64+
.. manim:: Example1bLaTeX
65+
:save_last_frame:
66+
67+
class Example1bLaTeX(Scene):
68+
def construct(self):
69+
tex = MathTex(r'\xrightarrow{Hello}\text{ \LaTeX}').scale(3)
70+
self.add(tex)
71+
72+
LaTeX commands and keyword arguments
73+
+++++++++++++++++++++++++++++++++++++
74+
We can use any standard LaTeX commands in the AMS maths packages. For example the ``mathtt`` text type.
75+
1276
.. manim:: Example2LaTeX
1377
:save_last_frame:
1478

1579
class Example2LaTeX(Scene):
1680
def construct(self):
17-
tex = Tex(r'$\mathtt{Hello}$ \LaTeX', color=BLUE).scale(3)
81+
tex = Tex(r'$\mathtt{Hello}$ \LaTeX').scale(3)
82+
self.add(tex)
83+
84+
On the manim side, the :class:`~.Tex` class also accepts attributes to change the appearance of the output.
85+
This is very similar to the :class:`~.Text` class. For example, the ``color`` keyword changes the color of the TeX mobject:
86+
87+
.. manim:: Example2bLaTeX
88+
:save_last_frame:
89+
90+
class Example2bLaTeX(Scene):
91+
def construct(self):
92+
tex = Tex(r'Hello \LaTeX', color=BLUE).scale(3)
1893
self.add(tex)
1994

95+
Extra LaTeX Packages
96+
+++++++++++++++++++++
97+
Some commands require special packages to be loaded into the TeX template. For example,
98+
to use the ``mathscr`` script, we need to add the ``mathrsfs`` package. Since this package isn't loaded
99+
into manim's tex template by default, we add it manually:
20100

21101
.. manim:: Example3LaTeX
22102
:save_last_frame:
@@ -25,50 +105,62 @@ LaTeX
25105
def construct(self):
26106
myTemplate = TexTemplate()
27107
myTemplate.add_to_preamble(r"\usepackage{mathrsfs}")
28-
tex = Tex(r'$\mathscr{H}ello$ \LaTeX}', tex_template=myTemplate).scale(3)
108+
tex = Tex(r'$\mathscr{H} \rightarrow \mathbb{H}$}', tex_template=myTemplate).scale(3)
29109
self.add(tex)
30110

111+
Substrings and parts
112+
+++++++++++++++++++++
113+
The TeX mobject can accept multiple strings as arguments. Afterwards you can refer to the individual
114+
parts either by their index (like ``tex[1]``), or you can look them up by (parts of) the tex code like
115+
in this example where we set the color of the ``\bigstar`` using :func:`~.set_color_by_tex`:
31116

32117
.. manim:: Example4LaTeX
33118
:save_last_frame:
34119

35120
class Example4LaTeX(Scene):
36121
def construct(self):
37-
tex = Tex('Hello', '$\\bigstar$', '\LaTeX').scale(3)
122+
tex = Tex('Hello', r'$\bigstar$', r'\LaTeX').scale(3)
38123
tex.set_color_by_tex('igsta', RED)
39124
self.add(tex)
40125

126+
LaTeX Maths Fonts - The Template Library
127+
++++++++++++++++++++++++++++++++++++++++++++
128+
Changing fonts in LaTeX when typesetting mathematical formulae is a little bit more tricky than
129+
with regular text. It requires changing the template that is used to compile the tex code.
130+
Manim comes with a collection of :class:`~.TexFontTemplates` ready for you to use. These templates will all work
131+
in maths mode:
132+
41133
.. manim:: Example5LaTeX
42134
:save_last_frame:
43135

44136
class Example5LaTeX(Scene):
45137
def construct(self):
46-
tex = Tex('Hello \LaTeX', tex_template=TexFontTemplates.french_cursive).scale(3)
138+
tex = Tex(r'$f: A \rightarrow B$', tex_template=TexFontTemplates.french_cursive).scale(3)
47139
self.add(tex)
48140

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)
141+
Manim also has a :class:`~.TexTemplateLibrary` containing the TeX templates used by 3Blue1Brown. One example
142+
is the ctex template, used for typesetting Chinese. For this to work, the ctex LaTeX package
143+
must be installed on your system. Furthermore, if you are only typesetting Text, you probably do not
144+
need :class:`~.Tex` at all, and should use :class:`~.Text` or :class:`~.PangoText` instead.
65145

66146
.. manim:: Example6LaTeX
67147
:save_last_frame:
68148

69149
class Example6LaTeX(Scene):
70150
def construct(self):
71-
tex = Tex('Hello 你好 \LaTeX', tex_template=TexTemplateLibrary.ctex).scale(3)
151+
tex = Tex('Hello 你好 \\LaTeX', tex_template=TexTemplateLibrary.ctex).scale(3)
72152
self.add(tex)
73153

74154

155+
Aligning formulae
156+
++++++++++++++++++
157+
A :class:`~.MathTex` mobject is typeset in the LaTeX ``align*`` environment. This means you can use the ``&`` alignment
158+
character when typesetting multiline formulae:
159+
160+
.. manim:: Example7LaTeX
161+
:save_last_frame:
162+
163+
class Example7LaTeX(Scene):
164+
def construct(self):
165+
tex = MathTex(r'f(x) &= 3 + 2 + 1\\ &= 5 + 1 \\ &= 6').scale(2)
166+
self.add(tex)

docs/source/examples/text.rst

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)