LaTeXExporter._escape_latex mangles its own replacements: a backslash in a title renders as literal \{}

Open Beginner friendly
#5,703 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
latex, python
Domain
backend

Research direction

Start in citation_formatter.py at LaTeXExporter._escape_latex and compare it with _escape_bibtex and the _BIBTEX_ESCAPES map around line 94. Trace export_to_latex for the title path, then add coverage for a backslash-bearing title and confirm the generated LaTeX preserves the intended backslash without literal escaped braces.

Written by the indexing model from the issue text.

Description

Summary

LaTeXExporter._escape_latex escapes by running a sequential str.replace chain. The first rule turns \ into \textbackslash{}, and the later {\{ and }\} rules then re-escape the braces that rule just introduced. A title containing a backslash is emitted as \textbackslash\{\}, which LaTeX renders as the literal text \{} rather than a backslash.

Reproduction
formatter = LaTeXExporter()          # citation_formatter.py
formatter._escape_latex(r"a\b")
# -> 'a\\textbackslash\\{\\}b'       # wrong; should be 'a\\textbackslash{}b'

~ and ^ escape correctly only by accident — their rules run after the brace rules, so the {} they emit is never revisited. Reorder the list and they break too, which is why this wants a structural fix rather than a reordering.

Fix

Use a single-pass character map instead of a replace chain, exactly as _escape_bibtex in the same module already does:

_BIBTEX_ESCAPES = {...}   # citation_formatter.py, ~line 94

That helper was written this way because of this bug — its comment names _escape_latex as the existing instance:

Single-pass character map. A sequential replace() chain would re-escape the braces its own replacements introduce — the bug already present in _escape_latex.

So the fix is to give _escape_latex the same treatment, plus a test that pins a backslash-bearing title through export_to_latex.

Impact

Output corruption in LaTeX export, not a security issue: the mangled form is inert text, and it cannot break out of a field the way the BibTeX brace bug could (fixed in #5685). Reachable with any title containing a backslash.

Context

Found by AI review of #5685 and confirmed by execution. Deliberately not fixed there — #5685 is already large, and this predates it and is not made more likely by it. Sibling of #5687 and #5689, which track the other export-path gaps found in that review.

Dominant language
Python
Stars
9.1k
Forks
824
Avg merge
3d 4h
Merged PRs (30d)
289

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from LearningCircuit/local-deep-research

All issues in LearningCircuit/local-deep-research

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.