7

My question doesn't explicitly refer to LaTeX itself, but the resulting PDF. To make long story short: I want to be able to copy text from PDF to TXT file as a continuous text. I believe this is common problem for many people.

I'm working on LaTeX document that is compiled to the PDF file. My text contains many word breaks at the end of a line. Sometimes I need to copy resulting text from PDF to the plain text file (*.txt). Unfortunately:

PDFs are designed to mimic a printed page, and they are designed only as an output format, not an input format. a PDF is basically a map containing the exact location of characters (individual letters or punctuation, etc.) or images. In most cases, a PDF does not even store information about where one word ends and another begins, much less things like soft breaks vs. hard breaks for paragraph endings.

Therefore I shouldn't be surprised that when I compile the following text to PDF:

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\title{Introduction to \LaTeX{}}
\author{Author's Name}

\maketitle

\begin{abstract}
The abstract text goes here.
\end{abstract}

\section{Introduction}
This is \LaTeX text that will be copied and pasted. Verylongword. Taumatawhakatangi­hangakoauauotamatea­turipukakapikimaunga­horonukupokaiwhen­uakitanatahu is a hill near Porangahau, south of Waipukurau in southern Hawke's Bay, New Zealand.

\subsection{Subsection Heading Here}
This text comes from Wikipedia.: The name "Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu" translates roughly as "The summit where Tamatea, the man with the big knees, the slider, climber of mountains, the land-swallower who travelled about, played his nose flute to his loved one".

\end{document}

which results in:

PDF created from example

and I copy whole text to TXT file, I get:

Introduction to L A TEX
Author’s Name
April 29, 2017
Abstract
The abstract text goes here.
1
Introduction
This is L A TEXtext that will be copied and pasted. Verylongword. Taumatawhakatangi-
hangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu is
a hill near Porangahau, south of Waipukurau in southern Hawke’s Bay, New
Zealand.
1.1
Subsection Heading Here
This text comes from Wikipedia.: The name ”Taumatawhakatangihangakoauauo-
tamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu” translates roughly
as ”The summit where Tamatea, the man with the big knees, the slider, climber
of mountains, the land-swallower who travelled about, played his nose flute to
his loved one”.

The most irritating are the word breaks and line endings that become new lines, so that copied text is not continuous. Is there any trick that can help me with copying text from PDF to TXT file as a continuous text?

Copying text directly from the TEX source is not an option, since the above example is very simplified – typical TEX source contains macros, formatting commands and so on.

7
  • 1
    You can improve the results with \usepackage[T1]{fontenc} or by using lualatex (and you should use correct quotes and not "). But the results depends a lot from the pdfviewer, adobe e.g. is better than sumatra. Apr 29, 2017 at 14:40
  • 3
    Related: stackoverflow.com/questions/530121/…
    – Marijn
    Apr 29, 2017 at 15:04
  • detex seems to be the best solution proposed so far (though it is applied on TEX source, not PDF file as I suggested). Thanks @Marijn! You can post your answer, so that I can accept it. ;-) Apr 29, 2017 at 15:17
  • 2
    not really related but you want LaTeX\ or you lose the space after. Apr 29, 2017 at 21:18
  • 1
    I believe that when TeX breaks a word with a hyphen, it uses the ordinary hyphen character. Then, text extracted from a PDF, by any software, cannot distingush between inserted hyphens, and natural hyphens that happen to be at the end of a line.
    – user103221
    May 29, 2017 at 21:14

2 Answers 2

3

Posting (as community wiki) the comment by Marijn about which the OP said “ Thanks @Marijn! You can post your answer, so that I can accept it.”

There is a question about this on Stack Overflow, and of the answers there, the one the OP finds working best is detex (opendetex). This is run on the source TeX file rather than the PDF file.

Just for completeness, other options mentioned on the Stack Overflow answer are:

  • catdvi which is run on the DVI file
  • Converting to HTML (with htlatex / tex4t / hyperlatex / hevea), then extracting text from the HTML file
  • Pandoc, a versatile converter between many formats
  • LaTeX2RTF to convert to RTF, then extract the text somehow
  • untex

See more at the TeX FAQ: Conversion from (La)TeX to plain text

Note: This is community wiki so please edit the answer rather than leaving comments, if possible.

3

Since you already mentioned, that this is not a LaTeX-specific question, you shouldn't be surprised, that my answer is also not LaTeX-related :).

One way is probably to use a so-called OCR (Optical Character Recognition) software.

  • OCR software is able to understand pictures and convert it to editable text.
  • OCR software even can understand multi-column layout and so on.
  • I am a reader of a famous German computer magazine called c't.
  • In issue 6/2017 there was a good review of the software Abbyy FineReader 14 (which has a 30 days test trial).
  • Or try another OCR software like FreeOCR or just google for other options :).
3
  • 2
    I think that OCR is overkill. OCR is intended to work primarily on images which, by their nature, don't include any useful metadata that can be useful to convert into plain text (as opposed to PDF). Apr 29, 2017 at 15:08
  • @patryk.beza If it works than I wouldn't care if it's an overkill. It also depends if it's a regulatory task or seldom. Apr 29, 2017 at 16:36
  • 3
    In Linux, pdftotext produce a better result without word breaks. For the line breaks, maybe pandoc --wrap=preserve could help, but I am not tested that.
    – Fran
    Apr 30, 2017 at 5:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .