Clean text from multi-column research papers¶
Two columns in. One scrambled chunk out. A text extractor that reads across the page gives you line 1 of the left column, then line 1 of the right column, then line 2 of the left. Every sentence is valid English and every chunk mixes two topics, so the embeddings are wrong and nothing looks broken.
Here is how to check for it, fix it with plain PyMuPDF, and when that stops being enough.
Check whether you have the problem¶
Print the start of a page and read it. If the second line continues a different sentence from the first, the columns are braided.
# pip install pymupdf
import pymupdf
page = pymupdf.open("paper.pdf")[0]
print(page.get_text(sort=True)[:600])
sort=True sorts by vertical position, which is exactly what braids two columns: on our test page it returned
"Left column text ... Right column text ..." on the same line.
Fix 1: read by column with PyMuPDF (free, digital PDFs)¶
Group words into lines, decide for each line whether it sits in the left column, the right column or across both, and read left then right.
import pymupdf
def page_text_by_column(page):
"""Read a two-column page: full-width lines in place, then left column, then right column."""
mid = page.rect.width / 2
lines = {} # (block, line) -> words
for x0, y0, x1, y1, word, b, l, _ in page.get_text("words"):
lines.setdefault((b, l), []).append((x0, y0, x1, word))
full, left, right = [], [], []
for words in lines.values():
x0 = min(w[0] for w in words); x1 = max(w[2] for w in words); y = min(w[1] for w in words)
text = " ".join(w[3] for w in sorted(words))
if x0 < mid - 20 and x1 > mid + 20: # the line crosses the gutter: title, abstract, wide caption
full.append((y, text))
elif (x0 + x1) / 2 < mid:
left.append((y, text))
else:
right.append((y, text))
first_col_y = min([y for y, _ in left + right], default=0)
head = [t for y, t in sorted(full) if y < first_col_y]
tail = [t for y, t in sorted(full) if y >= first_col_y]
return "\n".join(head + [t for _, t in sorted(left)] + [t for _, t in sorted(right)] + tail)
doc = pymupdf.open("paper.pdf")
text = "\n\n".join(page_text_by_column(p) for p in doc)
We ran this on a two-column page with a full-width title: title first, the whole left column, then the right.
Where it breaks:
- Scanned papers. No text layer, so there are no words to sort.
- Mixed layouts. A page that is one column at the top and two below, three-column pages, a table or figure spanning both columns in the middle of the page.
- Tables and formulas. Words come back in order, but a table is still a run of numbers and a formula is noise.
- Footnotes, headers and page numbers land in the text and in your chunks.
Fix 2: a layout-aware parser¶
These find the columns, tables, figures and headers on each page before reading it. The public benchmark that tests
exactly this is
olmOCR-bench's multi-column set (reading order checked by unit tests on real pages):
| Parser | Multi-column score | Licence | Runs on | Score measured by |
|---|---|---|---|---|
| ReadAnyDoc | 80.9 | Apache-2.0 | no GPU; a vision model you have an account with reads the page | us; per-page results public |
| Marker (1.10.1) | 80.0 | Apache-2.0 code; weights free under $5M funding or revenue | local models | the benchmark's authors |
| MinerU (2.5.4) | 78.2 | Apache-2.0 with extra terms | local models, GPU recommended | the benchmark's authors |
| 65.6 | closed, hosted | Reducto's servers | us | |
| not published | MIT | CPU | - |
Scores from different sources are not a ranking: versions and benchmark revisions differ, and Marker and MinerU have newer releases than the ones scored. olmOCR-bench
With ReadAnyDoc (open source, your own model key):
import readanydoc
result = readanydoc.parse("paper.pdf")
markdown = result.markdown # columns in reading order, tables as tables, headers and footers labelled
Each block also carries its type and position, so you can drop page headers, footers and page numbers before chunking:
Where ReadAnyDoc is weak here: it takes 20-50 seconds a page and costs about $0.003-0.005 a page in model usage, where Marker, MinerU and Docling are free on your own hardware. It is behind Reducto on formulas (OmniDocBench formula error 0.152 against 0.133), which matters for maths-heavy papers. And pages go to the model provider you choose unless you host the model.
Which to use¶
| Your papers | Use |
|---|---|
| Born-digital, plain two-column, few tables | Fix 1: PyMuPDF by column |
| Thousands of arXiv PDFs, GPUs available | Marker or MinerU |
| Scanned papers, old journals, mixed layouts, no GPU | ReadAnyDoc |
| Maths-heavy, formulas must be exact | test Marker, MinerU and Reducto on your own pages; we are behind on formulas |
| You need references and metadata as structured fields | GROBID, built for scholarly papers |
Before you embed¶
- Chunk by section, not by character count. A parser that returns headings lets you split on them.
- Keep tables whole. One table per chunk, with its caption.
- Drop the reference list unless you search citations; it matches every query a little.
- Keep the page number with each chunk so answers can point back to the source.
Questions¶
Why does my PDF text come out with columns mixed together? The extractor orders text by vertical position across the whole page. In a two-column layout that alternates between the columns line by line.
Does sort=True in PyMuPDF fix multi-column PDFs?
No. It sorts top to bottom, then left to right, which interleaves the columns. Sort within each column instead, as
in the function above.
What is the best parser for two-column research papers? On olmOCR-bench's multi-column tests the published scores are ReadAnyDoc 80.9 (measured by us), Marker 80.0 and MinerU 78.2 (the benchmark's authors), Reducto 65.6 (measured by us). They are close at the top: pick on GPU access, cost and whether your papers are scanned.
Do I need OCR for research papers? Only for scanned ones. Most arXiv PDFs have a text layer; older journal scans do not.