public function generate()
    {
        $tex2png = $this;

        $generate = function($target) use ($tex2png) {
            $tex2png->actualFile = $target;

            try {
                // Generates the LaTeX file
                $tex2png->createFile();
           
                // Compile the latexFile     
                $tex2png->latexFile();

                // Converts the DVI file to PNG
                $tex2png->dvi2png();
            } catch (\Exception $e) {
                $tex2png->error = $e;
            }

            $tex2png->clean();
        };

        if ($this->actualFile === null) {
            $target = $this->hash . '.png';
            $this->cache->getOrCreate($target, array(), $generate);

            $this->file = $this->cache->getCacheFile($target);
            $this->actualFile = $this->cache->getCacheFile($target, true);
        } else {
            $generate($this->actualFile);
        }

        return $this;
    }