I have a bunch of one-line LaTeX strings which I would like to compile into unicode strings (preferably using Python, but any solution will do). Specifically achieve

in: '\"o{A}c'
out: 'öAc'

(I realize there is latexcodec, but it I found it unreliable so I'm looking for an alternative approach.)

I figure the only reliable way to do so is to fall back to a system LaTeX compiler, specifically

  • to compile the string into a PDF, and
  • to read the output from the PDF file.

Is there a more streamlined approach to this? For example: Is is possible to compile LaTeX into a unicode string in memory rather than a PDF?

  • 1
    Can you add an example of what you mean by “compiling into unicode strings”? What's the input and the expected output? – egreg Oct 25 '15 at 11:29
  • 1
    biber can do this for you. – Johannes_B Oct 25 '15 at 11:42
  • Questions about Python are off topic here. However, stripping off braces shouldn't be difficult, while decoding from LICR to Unicode can be done by looking at the <enc>enc.dfu files, for instance t1enc.dfu. – egreg Oct 25 '15 at 11:50
  • I don't really care so much about Python, if there is any solution I'll see how I can adapt it to my use case. – Nico Schlömer Oct 25 '15 at 11:59
  • 1
    i can give you a small latex example, if you want. – Johannes_B Nov 24 '15 at 17:36

Pandoc does a decent job for not too complicated documents. Try

echo "\\\"o{A}c" | pandoc -f latex -t plain

or, in Python,

def latex_to_unicode(latex_string):
    '''Convert a LaTeX string to unicode.
    # Use pandoc for the job
        # This works in Python 3.4+
        return subprocess.check_output(
            ['pandoc', '-f', 'latex', '-t', 'plain'],
    except TypeError:  # unexpected keyword 'input'
        p = subprocess.Popen(
            ['pandoc', '-f', 'latex', '-t', 'plain'],
        stdout, stderr = p.communicate(latex_string)
        return stdout.replace('\n', ' ').strip().decode('utf-8')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.