Question

I know how make a PDF from LaTeX. Is there a way to extract the LaTeX-code from a PDF I created earlier? How about if someone sends me a PDF and I like the formatting. Can I extract the LaTeX from it?

Was it helpful?

Solution

LaTeX does not have a one-to-one conversion to PDF. With regards to your first question, I believe such a conversion may be technically possible, but I do not believe an application to do so yet exists. Similar to the way assembler can be decompiled back into high level language, there is probably a way to do it. However -- a pdf is allowed to contain all matter of kinds of data -- AutoCAD drawings, JPEG graphics, font files, forms, digital signatures, etc. LaTeX has no idea what these things are. So in answer to the second question is no -- there's not a way to extract equivalent LaTeX from any PDF document.

OTHER TIPS

There is a Tool that reads PDF-Files like an OCR and tries to recreate the Latex-Code. It's nearly perfect and called "Infty Reader"! Because Latex is quite extensible I don't think it get's all the neat formats right.

It's only possible if you embed the source of the document into the PDF file. See the attachfile package for doing this.

Short version: No.

Long version: It's a lot like decompiling: You technically could, but it would involve lots of guessing and heuristics.

I'm not familiar with the PDF innards, but it will likely set fonts/sizes/position directly, instead of defining a format and applying it to headers and such, like in LaTeX.

It is possible to convert your PDF to HTML and your HTML to TEX using pdftohtml and gnuhtml2latex.

In effect, you are doing PDF to LaTeX conversion in 2 steps. The result still is like "making a cow out of a hamburger", but in combination with some cleanup scripts the result can be pretty decent.

The blog post "Rudimentary PDF to LaTeX conversion in Linux" on GlobalBlindSpot has an example Bash script that converts a .pdf to a .tex file and that one to a .pdf file again.

See my answer on related question (how to turn a DVI to tex?)

To amplify - there is no requirement for characters to be in reading order (I have found PDFs where part of the sdrawkcab sdaer txet (and relies on the coordinates). That is very difficult to reconstruct as it can depend on Font metrics. Which can use the appalling ASCII86 protocol.

The best way for data mining from pdf files (due to its complicated format) is to open them with adobe illustrator. Then convert the pdf file to svg file and use a svg parser library writing some tricky code on yourself.

One efficient svg parser lib is batik

(For Linux it is quite a bit complex for converting pdf to svg: calcmaster.net/personal_projects/pdf2svg/)

PS I've been trying since a lot to find a solution to your second part of your question but I've figured out in books such "Visualizing Data, Ben Fry, O’Reilly" that pdf especially Adobe pdf is to complex to parse, so instead use a svg parser lib.

Inkscape can import PDFs and then save as "LaTeX with PSTricks macros" which essentially works by embedding PostScript into the LaTeX source. It's more trouble than its worth, and the resulting Latex source has to be preprocessed before it can be output as a PDF again.

Anyway, even with some hypothetical PDF to LaTeX compiler, at best you'd get something where the position of and size each character or word is separately specified -- the opposite of what you want, which I'm guessing is for a denominator to be one half of a fraction, rather than some number below a horizontal line.

It may work with texmacs, which includes an import of pdf files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top