19

If I compile a .tex file to a PDF, how will the PDF encode a mathematical formula like f.ex. the integral below? Is it a bitmap? Or how else is it encoded? And how can I extract the formula from a PDF file? enter image description here

Some more context: My goal is to train a neural net to output the latex code when I give it a mathematical symbol like the integral above as an input. The first step for this would be to find out how the symbol is represented in the PDF file so I can extract this part and use it as a label for the training data.

Thanks

  • Sees like a duplicate (or related to) How to convert PDF to Latex – StephenG Apr 25 '18 at 14:12
  • Just FYI, there is a commercial program (not free) called "PDFtoMusic" which does this kind of thing, for musical notation. It can actually play the music (and sing with synth voice, too). But the PDF must be generated by a music notation program, so that PDFtoMusic knows how to decode it. Scanned sheet music does not work (that is, no bitmaps). I mention this, merely to show that your goal is not futile. – user139954 Apr 25 '18 at 15:37
  • Something that already does this: mathpix.com – Dougal Apr 25 '18 at 17:18
29

Essentially in pdf every letter (or run of letters) is positioned by coordinates so even a normal word might be encoded as individual letters positioned to "look" like text, so as to take account of inter-letter kerns etc.

Math is no different: the characters are just normal font characters positioned on the page at locations that TeX has determined.

PostScript uses the same rendering model as PDF but is a bit easier to read by eye, Taking Henri's example and using latex and dvips

\documentclass{article}

\pagestyle{empty}

\begin{document}
$\int_0^2 x^2 dx$
\end{document}

Produces the following PostScript

%%Page: 1 1
TeXDict begin 1 0 bop 639 457 a Fc(R)695 477 y Fb(2)678
553 y(0)746 524 y Fa(x)793 494 y Fb(2)830 524 y Fa(dx)p
eop end
%%Trailer

where you can see the structure: strings are encoded as for example (dx) for dx and but apart from that 2 letter example all other character runs are single characters with the font and coordinates specified separately for each letter.

  • Add a colon after Math is no different. – TRiG Apr 25 '18 at 15:21
  • @TRiG done :-). – David Carlisle Apr 25 '18 at 15:34
9

If you use a Unicode math font, then all of the glyphs are just Unicode symbols in the resulting PDF.

\documentclass{article}
\pagestyle{empty}
\usepackage{unicode-math}
\begin{document}
$\int_0^2 x^2 dx$
\end{document}
$ pdftotext test.pdf -
2

∫ 𝑥2 𝑑𝑥
0
  • I did not understand the question, did you? :-( I'm upvoting your answer. But why I have not understood the questions? – Sebastiano Apr 24 '18 at 21:31
  • @Sebastiano »find out how the symbol is represented in the PDF file« is the key point here (I presume). – Henri Menke Apr 24 '18 at 21:31
  • Boh! I have seen the tag bitmap :-( – Sebastiano Apr 24 '18 at 21:35
  • 3
    @Dr.ManuelKuehner PDF was originally intended as "digital paper" in which the characters had no semantic meaning. Over the years, many features have been added, with the emphasis on electronic files rather than pre-print. As far as I know (which is very little), it is possible to add semantic meaning using accessibility tags. However, the TeX capability (accsup package) is very limited, and the job is very labor-intensive with professional software (Adobe Acrobat Pro). – user139954 Apr 25 '18 at 17:09
  • 2
    @Dr.ManuelKuehner For the above example, I doubt if the PDF reader chooses font based on semantic meaning, because I doubt that the original PDF had such meaning encoded. But as long as Unicode is used, there is a specific character for (say) the integral sign. There are also specific characters for superscripts and subscripts, which are different from the same characters merely moved up or down. An equation using Unicode-specific characters has some built-in semantic meaning, for simple equations. But that does not work for matrices, or other situations specifically using position. – user139954 Apr 25 '18 at 18:58
8

For my opinion (I hope to have understand well) you could use a special tool called MaxTract. It can be found at the link http://www.cs.bham.ac.uk/research/groupings/reasoning/sdag/maxtract.php.

Maxtract is a tool for converting PDF into formats such as LaTeX, MathML and text. enter image description here

ADDENDUM: Could this program PDF to LaTeX converter also be useful?

enter image description here

  • 2
    You already know this, but for the benefit of others who find this thread: One of the principles of Unicode is that each character has a semantic meaning. This, the summation symbol us U+2201, but the Greek capital Sigma is U+03A3, even though they may look the same. This semantic difference is one way that automation can discern the meaning. – user139954 Apr 25 '18 at 15:44

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.