manim icon indicating copy to clipboard operation
manim copied to clipboard

Add ``TikzTex`` MObject

Open christopher-besch opened this issue 2 years ago • 7 comments

As discussed here you need to add fill_opacity=0, stroke_width=3 to the Tex constructor for tikz to look like this: image and not like this: image

This should be put in a new MObject.

christopher-besch avatar Nov 13 '21 12:11 christopher-besch

Just to add, there was some discussion of implementing these as plugins rather than in the main library, which allows for further flexibility of the mobject. But I'm not for nor against anyhow.

icedcoffeeee avatar Nov 13 '21 12:11 icedcoffeeee

Isn't this sort of implementation just a quick fix? It doesn't really solve the underlying problem of Tikz not being rendered properly.

Darylgolden avatar Nov 13 '21 12:11 Darylgolden

Isn't this sort of implementation just a quick fix? It doesn't really solve the underlying problem of Tikz not being rendered properly.

That is a very good point! But we should at least add the workaround to the documentation while this hasn't been fixed.

christopher-besch avatar Nov 13 '21 12:11 christopher-besch

Isn't this sort of implementation just a quick fix? It doesn't really solve the underlying problem of Tikz not being rendered properly.

That is a very good point! But we should at least add the workaround to the documentation while this hasn't been fixed.

Might be a good idea to add this to the documentation for newcomers

MrDiver avatar Jun 16 '22 10:06 MrDiver

As discussed here you need to add fill_opacity=0, stroke_width=3 to the Tex constructor for tikz to look like this: image and not like this: image

This should be put in a new MObject.

It seems that the tex mobject will generate the svg of Tex in \media\Tex file. So I copy the temp file and use SVGMobject to load it. Might not be a good solution, but it works.

LingrenKong avatar Jul 12 '22 13:07 LingrenKong

class Test(Scene):
    def construct(self):
        latex_temp = TexTemplate()
        latex_temp.add_to_preamble(r"\usepackage{circuitikz}")
        circuit = Tex(r"""
        \begin{circuitikz}
            \draw (0,0)
            to[V,v=$U_q$] (0,1.5);
            \draw (0,1.5)
            to[nos] (2,1.5)
            to[C=$C$] (2,0)
            to[short] (0,0);
            \draw (2,1.5)
            to[short] (4,1.5)
            to[R=$R$] (4,0)
            to[short] (2,0);
            \draw (4,1.5)
            to[short] (6,1.5)
            to[voltmeter] (6,0)
            to[short] (4,0);
        \end{circuitikz}
        """, tex_template=latex_temp, color=BLACK)
        self.add(circuit)
        self.wait(2)
        self.remove(circuit)
        circuit_reload = SVGMobject('test.svg') #copy and rename after first time Tex generation
        self.play(Create(circuit_reload),run_time=2)
        self.wait(2)

https://user-images.githubusercontent.com/47297555/178508762-2f195673-b2cd-4d24-8c28-1bd10ecf272e.mp4

LingrenKong avatar Jul 12 '22 14:07 LingrenKong

Another example: svg mobject can hold the color of tikz drawing

class Test2(Scene):
    def construct(self):
        latex_temp = TexTemplate()
        latex_temp.add_to_preamble(r"\usepackage{tikz}")
        #tikz_color= Tex(r"""
#\begin{tikzpicture}[line width=1ex]
#  \draw (0,0) -- (3,1);
#  \filldraw [fill=yellow!80!black,draw opacity=0.5] (1,0) rectangle (2,1);
#\end{tikzpicture}
#        """, tex_template=latex_temp)
#        self.add(tikz_color)
#        self.wait(2)
#        self.remove(tikz_color)
        reload = SVGMobject('test2.svg')
        self.play(Create(reload),run_time=2)
        self.wait(2)

https://user-images.githubusercontent.com/47297555/178511259-80ef9d6c-fddb-40cc-84a3-81b9001a0630.mp4

@MrDiver Would it be possible to extract the tex building pipeline as a function? It seems that, the compile and convert to svg process works fine for non-text tex content like tikz, but Tex mobject is not designed for plotting. Maybe we can use tex and tikz this way in the future version:

tikz_draw = SVGMobject(Tex('...').to_svg())

LingrenKong avatar Jul 12 '22 14:07 LingrenKong

Any update on this? For me it is super important to have the CircuitTikz and Tikz soon in manim...

myzinsky avatar Nov 10 '22 16:11 myzinsky

With the current development version (plus one commit, will PR that change soon) I can render this scene:

class OpeningCircuit(Scene):
    def construct(self):
        myTemplate = TexTemplate(documentclass=r"\documentclass[tikz,dvisvgm]{standalone}")
        myTemplate.add_to_preamble(r"\usepackage{circuitikz}")
        circuit = Tex(r"""
            \draw (0,0)
            to[V,v=$U_q$] (0,1.5);
            \draw (0,1.5)
            to[nos] (2,1.5)
            to[C=$C$] (2,0)
            to[short] (0,0);
            \draw (2,1.5)
            to[short] (4,1.5)
            to[R=$R$] (4,0)
            to[short] (2,0);
            \draw (4,1.5)
            to[short] (6,1.5)
            to[voltmeter] (6,0)
            to[short] (4,0);
            """,
            tex_template = myTemplate,
            tex_environment="circuitikz",
            stroke_width=2
        )
        self.add(circuit)

resulting in this output:

image

As long as a stroke_width is set, Tex can deal with tikz images fairly well. We are working on a November release to ship all of this new functionality too.

behackl avatar Nov 10 '22 19:11 behackl

When will be the November-Relase?

myzinsky avatar Nov 12 '22 22:11 myzinsky

When will be the November-Relase?

There are still a few open PRs that should be included in the release, feel free to lend a hand by reviewing some of the PRs labeled with documentation, or #3016. Perhaps towards the end of this week, and later otherwise. :-)

behackl avatar Nov 13 '22 12:11 behackl

Hey, has this been resolved yet? Is there anything I can do to help?

madt2709 avatar Apr 26 '23 23:04 madt2709