0

Is there a tool to extract only text from a pdf file? The tool should completely ignore images. Extra points for (i) option to ignore headers and footers, (ii) converting each inline equation to a contiguous block of text.

I have a pdf file made with latex. The file contains some images. Some of the images contain labels which can be selected (the images are made in tikz/pstricks). I have tried to convert the pdf file to plain text with a number of tools (eg.: online tools, calibre), but all of them picked up labels in the images and treated them as text. I am okay with the tool picking up the image caption (either way fine), but picking up labels in images makes the text file less useful.
Why is it less useful? (i) grammar check, (ii) diff between such text files gives useless output if image positions are changed between versions of the pdf file (the latter happens since latex treat image as float).

If we have the source tex file, then we can suppress images, but is there a solution in cases where we only have the pdf file?

6
  • On bonus point (ii): An inline equation may be converted to an incomprehensible chunk of text, I am not worried about this. But, many tools have trouble with subscripts and superscripts that they misplace subscript/supersript text to some other line. This messes up text around the equations also, I would like to avoid this. Commented Feb 4, 2021 at 10:36
  • If you are generating the pdf from latex why not invert the process by keeping the source as markdown or restructured text and using that for version control, grammar checks, etc. and generate the latex & pdfs from that? Commented Feb 5, 2021 at 19:27
  • @SteveBarnes That is a reasonable idea. But, for comparing two versions in a discussion between co-authors, it is uncomfortable esp. when co-authors get confused by latex code. Commented Feb 6, 2021 at 6:31
  • Cyriac One of the points of both markdown and reStructured text is that it is human read & writable and focused on the sense while latex is focused on output control and for most people not very readable. Commented Feb 6, 2021 at 10:10
  • @SteveBarnes Sorry I am not familiar with markdown nor restructures text (for latex files). Is it siimilar to xml or csv? Anyway, it sounds like an idea with a general scope. Please consider posting your solution as an answer. Commented Feb 8, 2021 at 6:49

1 Answer 1

1

As from the question it seems that you have access to the original LaTeX information I would recommend converting from the LaTeX rather than the pdf format. This can be done with a number of tools, one of which is the python pylatexenc library which includes a LaTeX to text conversion tool that is usable both programmatically and from the command line.

An example from the documentation:

$ echo '\textit{italic} \`acc\^ented text' | latex2text

Gives: italic àccênted text

It is highly customisable as well as free & cross platform.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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