Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 503583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:24:32+00:00 2026-05-13T06:24:32+00:00

I’m looking for a library (or a cleverer solution) in C or C++ that

  • 0

I’m looking for a library (or a cleverer solution) in C or C++ that would make an image file (PNG|jpeg) from LaTeX code. The use of packages is a prerequisite.

For now I’m thinking of compiling a .tex file into a .dvi and using dvipng to get a .PNG.

There’s also the possibility of compiling a .tex file into a .ps file and then converting it into a .PNG by the mean of extern utilities like pstopng or pstoedit.

But these solutions are cumbersome and not always portable. I would like to integrate this conversion in my program transparently.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:24:32+00:00Added an answer on May 13, 2026 at 6:24 am

    I’ve used the dvipng route several times before, but in python. It’s a common path, that lots of people have taken. Here’s the code, to give you something to get started, and in case anyone wants Python code. I do realise you asked for C/C++; this is for a starter, or for others. This is for generating equations, but it would be trivial to adapt it for more general structures. It does support packages.

    In terms of integrating it transparently, I feel your pain. Not everyone has tex / latex of course, and if they don’t, it’s often a pain to get. The best way to do it, I think, is to provide that functionality as a web service – but of course that’s not always an option.

    Finally, note all the options for dvipng. They control the appearance, via various anti-alisaing options etc. I tuned them extensively to get what I thought looked good.

    def geneq(f, eq, dpi, wl, outname, packages):
        # First check if there is an existing file.
        eqname = os.path.join(f.eqdir, outname + '.png')
    
        # Open tex file.
        tempdir = tempfile.gettempdir()
        fd, texfile = tempfile.mkstemp('.tex', '', tempdir, True)
        basefile = texfile[:-4]
        g = os.fdopen(fd, 'w')
    
        preamble = '\documentclass{article}\n'
        for p in packages:
            preamble += '\usepackage{%s}\n' % p
    
        preamble += '\pagestyle{empty}\n\\begin{document}\n'
        g.write(preamble)
    
        # Write the equation itself.
        if wl:
            g.write('\\[%s\\]' % eq)
        else:
            g.write('$%s$' % eq)
    
        # Finish off the tex file.
        g.write('\n\\newpage\n\end{document}')
        g.close()
    
        exts = ['.tex', '.aux', '.dvi', '.log']
        try:
            # Generate the DVI file
            latexcmd = 'latex -file-line-error-style -interaction=nonstopmode ' + \
                   '-output-directory %s %s' % (tempdir, texfile)
            p = Popen(latexcmd, shell=True, stdout=PIPE)
            rc = p.wait()
            if rc != 0:
                for l in p.stdout.readlines():
                    print '  ' + l.rstrip()
                exts.remove('.tex')
                raise Exception('latex error')
    
            dvifile = basefile + '.dvi'
            dvicmd = 'dvipng --freetype0 -Q 9 -z 3 --depth -q -T tight -D %i -bg Transparent -o %s %s' % (dpi, eqname, dvifile)
            # discard warnings, as well.
            p = Popen(dvicmd, shell=True, stdout=PIPE, stderr=PIPE)
            rc = p.wait()
            if rc != 0:
                print p.stderr.readlines()
                raise Exception('dvipng error')
            depth = int(p.stdout.readlines()[-1].split('=')[-1])
        finally:
            # Clean up.
            for ext in exts:
                g = basefile + ext
                if os.path.exists(g):
                    os.remove(g)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.