manim icon indicating copy to clipboard operation
manim copied to clipboard

Manim is not correctly drawing tikzpictures

Open rdownieMath opened this issue 5 years ago • 4 comments

I'm very very new to coding and am mostly trying to learn on my own. I'm starting to pick up the basics of manim, but am still having difficulties with troubleshooting. I've been trying to get manim to draw tikz diagrams and I've spent hours digging through what seemed like similar issues. I finally got it to draw the diagram, but it's all messed up and I can't find any solutions online.

I'm trying to do a tree diagram. It should look like this (from LaTeX):

Screen Shot 2020-08-19 at 9 44 20 PM

Here's the code I used in manim:

from manimlib.imports import * 

class TikzMobject(TextMobject):
	CONFIG = {
	"stroke_width": 3,
	"fill_opacity": 0,
	"stroke_opacity": 1,
	}

class Tree(Scene):
	def construct(self):
	
		
		TreeDiagram = TikzMobject(r"""
		\begin{tikzpicture}[level distance=5em,level 1/.style={sibling distance=5cm},level 2/.style={sibling distance=2.5cm},level 3/.style={sibling distance=1cm},every node/.style={shape=rectangle,rounded corners,draw,align=center}]]
		\node {Start}
		child { node {$s_1$} 
		child { node {$p_1$}
		child { node{$f_1$}}
		child { node{$f_2$}}}
		child { node {$p_2$}
		child { node{$f_1$}}
		child { node{$f_2$}}}}
		child { node {$s_2$} 
		child { node {$p_1$}
		child { node {$f_1$}}
		child { node {$f_2$}}}
		child { node {$p_2$}
		child { node {$f_1$}}
		child { node {$f_2$}}}}
		child { node {$s_3$}
		child { node {$p_1$}
		child { node {$f_1$}}
		child { node {$f_2$}}}
		child { node {$p_2$}
		child { node {$f_1$}}
		child { node {$f_2$}}}};
		\end{tikzpicture}
		""")
		TreeDiagram.scale(0.3)
		TreeDiagram.rotate(TAU/2)

		self.play(FadeIn(TreeDiagram))

But what it produces is this:

Screen Shot 2020-08-19 at 9 48 02 PM

I would greatly appreciate any help!

rdownieMath avatar Aug 20 '20 05:08 rdownieMath

That's because Manim removes styles from SVGs (manim compiles the .tex file into dvi, then transforms the dvi to svg), so you can't depend on LaTeX styles for Manim, you'll have to do it by hand or create a class on your own.

Elteoremadebeethoven avatar Aug 20 '20 05:08 Elteoremadebeethoven

I encountered a similar issue once. Did you add dvisvgm to the document class of the tex_template.tex file? If yes then just remove it. It should be

\documentclass[preview]{standalone}

shailvaidya avatar Sep 23 '20 08:09 shailvaidya

Is there any update on this case? I want to generate tikzcd diagrams, but all the arrows collapse to the corner

Functor_ManimCE_v0 14 0

nosarthur avatar Jan 23 '22 19:01 nosarthur

I have the same issue of wanting to use manim to display lots of commutative diagrams (generated by tikzcd) and other TikZ drawn diagrams.

I'm completely naïve to manim's internals, but I am a capable programmer. I'm willing to put in some time to help if someone has time give me direction.

timtro avatar Mar 28 '22 16:03 timtro