How to convert simple string to LaTeX string programmatically ?
Closed this issue · 1 comments
NavpreetDevpuri commented
if we try TextMobject(" \# \{ \} ")
it works fine.
but if we try following code TextMobject(" # { } ")
it will result into following error
File "manimlib\utils\tex_file_writing.py", line 67, in tex_to_dvi
"See log output above or the log file: %s" % log_file)
Exception: Latex error converting to dvi. See log output above or the log file: ./media\Tex\c2fe0a3b9407ff29.log
is there any way to convert simple string " # { } " to LaTex string " \# \{ \} " ?
something like print(StringToLaTexString(" # { } "))
Output should be \# \{ \}
So that we can use something like TextMobject(StringToLaTexString(" # { } "))
to avoid error.
NavpreetDevpuri commented
i created following function
def latex_escape(str):
# Replace a \ with $\backslash$
# This is made more complicated because the dollars will be escaped
# by the subsequent replacement. Easiest to add \backslash
# now and then add the dollars
# Must be done after escape of \ since this command adds latex escapes
# Replace characters that can be escaped
# Replace ^ characters with \^{} so that $^F works okay
# Replace tilde (~) with \texttt{\~{}} # Replace tilde (~) with \texttt{\~{}}
list = ["\\", "^", "~", '&', '%', '$', '#', '_', '{', '}']
change_to = ["$\\backslash$", "\\^{}", "\\texttt{\\~{}}", '\\&', '\\%', '\\$', '\\#', '\\_', '\\{', '\\}']
for i in list:
if i in str:
str = str.replace(i, change_to[list.index(i)])
break
return str
and remember that you can't create a single curly brackets '{' or '}' in LaTeX
means you can't create TextMobject(" \{")
or TextMobject(" \}")
or TextMobject(" \{aaa")
so you can try following methods
3b1b/manim#941 (comment)