Update tex_mobject.py

This commit is contained in:
Grant Sanderson 2018-05-09 14:01:38 -07:00 committed by GitHub
parent 6fa024b773
commit d97b994876
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,14 +4,13 @@ from svg_mobject import SVGMobject
from svg_mobject import VMobjectFromSVGPathstring from svg_mobject import VMobjectFromSVGPathstring
from utils.config_ops import digest_config from utils.config_ops import digest_config
from utils.strings import split_string_list_to_isolate_substring from utils.strings import split_string_list_to_isolate_substring
from utils.tex_file_writing import tex_to_svg_file
from mobject.geometry import Line
from mobject.types.vectorized_mobject import VGroup from mobject.types.vectorized_mobject import VGroup
from mobject.types.vectorized_mobject import VectorizedPoint from mobject.types.vectorized_mobject import VectorizedPoint
import operator as op import operator as op
# TODO list
# - Make sure if "color" is passed into TexMobject, it behaves as expected
TEX_MOB_SCALE_FACTOR = 0.05 TEX_MOB_SCALE_FACTOR = 0.05
@ -67,11 +66,20 @@ class SingleStringTexMobject(SVGMobject):
def modify_special_strings(self, tex): def modify_special_strings(self, tex):
tex = self.remove_stray_braces(tex) tex = self.remove_stray_braces(tex)
if tex in ["\\over", "\\overline"]: should_add_filler = reduce(op.or_, [
# fraction line needs something to be over # Fraction line needs something to be over
tex += "\\," tex == "\\over",
if tex == "\\sqrt": tex == "\\overline",
tex += "{\\quad}" # Makesure sqrt has overbar
tex == "\\sqrt",
# Need to add blank subscript or superscript
tex.endswith("_"),
tex.endswith("^"),
])
if should_add_filler:
filler = "{\\quad}"
tex += filler
if tex == "\\substack": if tex == "\\substack":
tex = "" tex = ""
for t1, t2 in ("\\left", "\\right"), ("\\right", "\\left"): for t1, t2 in ("\\left", "\\right"), ("\\right", "\\left"):
@ -95,10 +103,9 @@ class SingleStringTexMobject(SVGMobject):
for char in "{}" for char in "{}"
] ]
if num_rights > num_lefts: if num_rights > num_lefts:
backwards = tex[::-1].replace("}", "", num_rights - num_lefts) tex = "{" + tex
tex = backwards[::-1]
elif num_lefts > num_rights: elif num_lefts > num_rights:
tex = tex.replace("{", "", num_lefts - num_rights) tex = tex + "}"
return tex return tex
def get_tex_string(self): def get_tex_string(self):
@ -273,98 +280,38 @@ class BulletedList(TextMobject):
class TexMobjectFromPresetString(TexMobject): class TexMobjectFromPresetString(TexMobject):
CONFIG = {
# To be filled by subclasses
"tex": None,
"color": None,
}
def __init__(self, **kwargs): def __init__(self, **kwargs):
digest_config(self, kwargs) digest_config(self, kwargs)
TexMobject.__init__(self, self.tex, **kwargs) TexMobject.__init__(self, self.tex, **kwargs)
self.set_color(self.color) self.set_color(self.color)
##########
class Title(TextMobject):
CONFIG = {
"scale_factor": 1,
"include_underline": True,
"underline_width": FRAME_WIDTH - 2,
# This will override underline_width
"match_underline_width_to_text": False,
"underline_buff": MED_SMALL_BUFF,
}
def tex_hash(expression, template_tex_file): def __init__(self, text, **kwargs):
return str(hash(expression + template_tex_file)) TextMobject.__init__(self, text, **kwargs)
self.scale(self.scale_factor)
self.to_edge(UP)
def tex_to_svg_file(expression, template_tex_file): if self.include_underline:
image_dir = os.path.join( underline = Line(LEFT, RIGHT)
TEX_IMAGE_DIR, underline.next_to(self, DOWN, buff=self.underline_buff)
tex_hash(expression, template_tex_file) if self.match_underline_width_to_text:
) underline.match_width(self)
if os.path.exists(image_dir): else:
return get_sorted_image_list(image_dir) underline.scale_to_fit_width(self.underline_width)
tex_file = generate_tex_file(expression, template_tex_file) self.add(underline)
dvi_file = tex_to_dvi(tex_file) self.underline = underline
return dvi_to_svg(dvi_file)
def generate_tex_file(expression, template_tex_file):
result = os.path.join(
TEX_DIR,
tex_hash(expression, template_tex_file)
) + ".tex"
if not os.path.exists(result):
print("Writing \"%s\" to %s" % (
"".join(expression), result
))
with open(template_tex_file, "r") as infile:
body = infile.read()
body = body.replace(TEX_TEXT_TO_REPLACE, expression)
with open(result, "w") as outfile:
outfile.write(body)
return result
def get_null():
if os.name == "nt":
return "NUL"
return "/dev/null"
def tex_to_dvi(tex_file):
result = tex_file.replace(".tex", ".dvi")
if not os.path.exists(result):
commands = [
"latex",
"-interaction=batchmode",
"-halt-on-error",
"-output-directory=" + TEX_DIR,
tex_file,
">",
get_null()
]
exit_code = os.system(" ".join(commands))
if exit_code != 0:
latex_output = ''
log_file = tex_file.replace(".tex", ".log")
if os.path.exists(log_file):
with open(log_file, 'r') as f:
latex_output = f.read()
raise Exception(
"Latex error converting to dvi. "
"See log output above or the log file: %s" % log_file)
return result
def dvi_to_svg(dvi_file, regen_if_exists=False):
"""
Converts a dvi, which potentially has multiple slides, into a
directory full of enumerated pngs corresponding with these slides.
Returns a list of PIL Image objects for these images sorted as they
where in the dvi
"""
result = dvi_file.replace(".dvi", ".svg")
if not os.path.exists(result):
commands = [
"dvisvgm",
dvi_file,
"-n",
"-v",
"0",
"-o",
result,
">",
get_null()
]
os.system(" ".join(commands))
return result