# A consolidated 'recovering material from a PDF' question and answer?

A question which comes up in one form or another reasonable regularly is 'How do I turn a PDF back into .tex?': see for example Can I recover my .tex file?. Some, but not all, of these questions are about 'disaster recovery', but the core issue (recreating a .tex file) does not directly hinge on this.

We had a similar situation for Linux installation, which was hacked out in meta and has now been posted to the main site. Particularly useful there was the fact that as this was a 'set up' question, it's written to be general. I wonder if it would be useful to do the same for the 'recovery' issue? If so, an answer here consisting of a question/answer pair for the main site would be the best approach. We can then post as a CW question on the main site when ready.

Discussion I guess in comments as needed.

-
Looks like a 'yes'. I guess someone will have to write something! –  Joseph Wright Sep 24 '12 at 18:35
I think we should use How to convert PDF to (La)TeX? as the question. It already has 7k views. I would assume altering the question a bit to make it "canonical" will be alright. –  doncherry Sep 28 '12 at 5:57
@doncherry OK provided we are not messing about too much with the question. Probably we would not be: my suggestion below would simply be adding possible additional cases to it. –  Joseph Wright Sep 28 '12 at 8:36

In my opinion, such consolidation will be useful. Today, you find information in different answers to different question. For example, a very useful link was hidden in a closed question (claimed duplicate). Also, we can then have cover more situations, slightly different from the usual ‘How to convert .PDF to .tex’.

Maybe a similar consolidation can be useful for the ‘How to convert .tex to .docx’.

In the consolidation, it is important to make room for new information regarding new programs etc.

-
I've made a start on a generic answer: perhaps you could contribution on the 'meat' as you've written a reasonably long answer to at least one of the individual questions on this. –  Joseph Wright Sep 24 '12 at 19:49
I would love to see this happen, since I'm currently trying to figure out how to write a complex document for my Word loving supervisor in LaTeX, and it seems to be that there are several almost good answers scattered over many questions marked as duplicates. Unfortunately, as far as I can tell, the real answer is "there really isn't a good way". –  craigim Apr 20 '13 at 0:26

A good question and answer here needs a bit of work. Feel free to edit: set as CW to make that easy.

# Question

Title: Conversion/recovery of .tex source from PDF file

Is there a method to convert a PDF file into a .tex file? Does it depend on whether the file was originally created using LaTeX or if additional files (such as .aux, .log or .bbl) are available? If I am recovering from a computer problem, as opposed to converting a PDF I've been given by someone else, does this make a difference?

In the most general terms, conversion of a PDF into a (La)TeX file that would have generated it is not possible in an automated way. There is no 'special' information added to a PDF by LaTeX which would allow this process. As such, converting a PDF from an unknown source is very much the same as converting a PDF you know was generated by LaTeX: not that easy!

Tools

• Since the TeX source is text-based, the go-to conversion of choice might be pdftotext. pdftotext is an open source command-line utility for converting PDF files to plain text files. That is, extracting text data from PDF-encapsulated files. It is freely available and included by default with many Linux distributions, as well as being available on Windows (as part of the Xpdf distribution). Such text extraction is complicated as PDF files are internally built on page drawing primitives, meaning the boundaries between words and paragraphs often must be inferred based on their position on the page. As such, the conversion-accuracy depends on the contents and layout.

• If you're only interested in retrieving graphics, Inkscape provides functionality to export to PStricks or TikZ.

• AbiWord has an import from/export to LaTeX plugin. The command-line usage would be

abiword --to=tex filename.pdf


(This is described in more detail by frabjous)

• Global Blind Spot blog lists a script for rudimentary PDF to LaTeX conversion in Linux

• InftyReader is a paid-for/non-free alternative. InftyReader is an Optical Character Recognition (OCR) application that recognizes and translates scientific documents (including math symbols) into LaTeX, MathML and XHTML. InftyReader can recognize tables including math expressions in the cells so long as the ruled lines are not broken.

Secondary files

It is possible that when trying to 'recover' a file you know was generated by LaTeX that you have various secondary files available. The nature of many of these files is discussed in File extensions of LaTeX-related files. Broadly, these are of limited help in converting a PDF into a .tex file. At best, they contain partial information about some aspect of the original source, but none will contain large pieces of the original file (unless the case is very unusual). Probably the most common secondary files are .aux, .bbl, .blg and .log:

• The .log and .blg files are logs: they tell us what happened in the LaTeX and BibTeX runs, respectively. That will be useful in working out which packages were used in the .tex file, but that alone does not get us very far (no custom settings or actual input).
• The .bbl file may help with the bibliography part of the document. If you did not use biblatex then the .bbl file is a formatted bibliography, but if you used biblatex then it's not. Moreover, it does not help with the citations that link to the bibliography, and most of the time the bibliography will be a relatively small part of the entire document.
• The .aux file tells us about information transferred between LaTeX runs, so for example labels used in the .tex file, but not where they might have been cross-referenced, etc.

As you'll see, the amount of information in the various additional files is at best quite limited, and in most real documents will form only a small part of what's needed to reconstruct the .tex source.

Disaster recovery

Recovering data following computer issues is broadly 'off-topic' for TeX-sx, but some general pointers can be given. If you suspect you have deleted your .tex source and it is not in the 'Recycle Bin' or similar, you should proceed very cautiously. It is usually best to use a separate machine to look for advice, and not to create any new files (for example by downloading recovery tools) onto the disk which contained the lost file(s). It then may be possible to recover your source without 'reconstruction'.

Notes

It is possible to attach .tex source to a PDF so that everything is available 'in one go'. However, this has to be done when the PDF is created, so does not help with conversion of an arbitrary PDF file. See Is there some way to embed LaTeX source code in a PDF file?.

-
I don’t want to edit the article: One could try to import the PDF into LibreOffice/LO or Apache OpenOffice/AOO (extension needed, which in LO is already included, for AOO I don’t know) or in MS Office (with commercial add-on; MSOffice 2013 is to be said, it will include PDF import). With LO or AOO use the extension “Writer2LaTeX” or save all as RTF file and use the program rtf2latex2e. –  Speravir Sep 26 '12 at 1:46
Some links: Oracle PDF Import ExtensionWriter2LaTeX or Writer2LaTeX or documentation & beta version on Sourceforge Writer2LaTeX (also with an extension “Writer2BiBTeX”) BTW not pre-installed on AOO and LO – rtf2latex2e – Also: Graphics need perhaps to be extracted with a graphics editor or screenshot tool. –  Speravir Sep 27 '12 at 0:01
I found some references here: How to convert PDF to (La)TeX? and PDF to LaTeX Linux. –  Werner Sep 28 '12 at 4:38