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?
Answer
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
[TO DO: HELP PLEASE!]
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 related to LaTeX, etc. 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?.