Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(latex): don't cite single [N] missing from Sources (#7604)

Vladimir Babincac190207f59
diff --git a/changelog.d/7529.bugfix.md b/changelog.d/7529.bugfix.md
new file mode 100644
index 0000000000..33d550c48c
--- /dev/null
+++ b/changelog.d/7529.bugfix.md
@@ -0,0 +1 @@
+LaTeX export now leaves a single `[N]` as literal text when the Sources section has no entry `N`, instead of emitting `\cite{N}` for prose like `[2020]` or a stray `[0]` and producing undefined-citation warnings.
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index 6418af87ad..8dfc2ba604 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -3563,8 +3563,14 @@ class LaTeXExporter:
return "\\cite{" + ",".join(nums) + "}"
return match.group(0)
+ def cite_single(match: re.Match[str]) -> str:
+ num = match.group(1)
+ if num in entries:
+ return "\\cite{" + num + "}"
+ return match.group(0)
+
body_content = self.comma_citation_pattern.sub(cite_group, body_content)
- body_content = self.citation_pattern.sub(r"\\cite{\1}", body_content)
+ body_content = self.citation_pattern.sub(cite_single, body_content)
# Convert lists
body_content = self._convert_lists(body_content)
@@ -3621,6 +3627,8 @@ class LaTeXExporter:
continue
label = match.group(1)
if label.isdecimal():
+ if label not in entries:
+ continue
index = label
elif _CITATION_GROUP_LABEL_RE.fullmatch(label):
members = [member.strip() for member in label.split(",")]
diff --git a/tests/text_optimization/test_citation_formatter.py b/tests/text_optimization/test_citation_formatter.py
index f56455ee19..fea75c1952 100644
--- a/tests/text_optimization/test_citation_formatter.py
+++ b/tests/text_optimization/test_citation_formatter.py
@@ -1281,6 +1281,26 @@ Higher numbers 【10】 and 【25】 work too.
assert r"\cite{10}" in result
assert r"\cite{25}" in result
+ def test_latex_single_citation_not_in_sources_left_as_text(self):
+ """A single [N] with no matching Sources entry stays literal text."""
+ content = """# Report
+
+Prose mentioning a year [2020] and a stray [0] index, plus real [1].
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+"""
+ exporter = LaTeXExporter()
+ result = exporter.export_to_latex(content)
+
+ assert r"\cite{1}" in result
+ assert r"\cite{2020}" not in result
+ assert r"\cite{0}" not in result
+ assert "[2020]" in result
+ assert "[0]" in result
+
class TestRISExporter:
"""Test cases for RIS export functionality."""
diff --git a/tests/text_optimization/test_citation_formatter_behavior.py b/tests/text_optimization/test_citation_formatter_behavior.py
index fa50dedc2e..0cf5ccbbbf 100644
--- a/tests/text_optimization/test_citation_formatter_behavior.py
+++ b/tests/text_optimization/test_citation_formatter_behavior.py
@@ -1173,7 +1173,9 @@ class TestLaTeXExporterExportToLatex:
)
exporter = LaTeXExporter()
- result = exporter.export_to_latex("See [1] for details.")
+ result = exporter.export_to_latex(
+ "See [1] for details.\n\n## Sources\n\n[1] A Source\n"
+ )
assert "\\cite{1}" in result
def test_adds_bibliography(self):
diff --git a/tests/text_optimization/test_latex_body_escaping.py b/tests/text_optimization/test_latex_body_escaping.py
index e1d59c4475..152049f859 100644
--- a/tests/text_optimization/test_latex_body_escaping.py
+++ b/tests/text_optimization/test_latex_body_escaping.py
@@ -61,7 +61,10 @@ class TestMarkdownConversionSurvivesEscaping:
def test_emphasis_and_citations_still_convert(self):
exporter = LaTeXExporter()
- content = "# Title\n\n**bold{x}** and *italic* cite [1].\n"
+ content = (
+ "# Title\n\n**bold{x}** and *italic* cite [1].\n\n"
+ "## Sources\n\n[1] A Source\n"
+ )
result = exporter.export_to_latex(content)
assert "\\textbf{bold\\{x\\}}" in result
diff --git a/tests/text_optimization/test_latex_citation_links.py b/tests/text_optimization/test_latex_citation_links.py
index e350862352..cdf35f65ca 100644
--- a/tests/text_optimization/test_latex_citation_links.py
+++ b/tests/text_optimization/test_latex_citation_links.py
@@ -64,9 +64,19 @@ class TestSavedReportCitations:
"Wikipedia agrees \\cite{4}."
)
- def test_a_number_label_needs_no_sources_block(self):
+ def test_a_number_label_missing_from_sources_stays_literal(self):
markdown = "# Research Report\n\nSee [[7]](https://seven.example/a).\n"
+ assert exported_answer(markdown) == (
+ "See [[7]](https://seven.example/a)."
+ )
+
+ def test_a_number_label_declared_in_sources_is_cited(self):
+ markdown = report(
+ "See [[7]](https://seven.example/a).",
+ "[7] Seven Paper\nURL: https://seven.example/a\n",
+ )
+
assert exported_answer(markdown) == "See \\cite{7}."
def test_two_sources_sharing_a_url_cite_the_lower_index(self):
@@ -121,10 +131,10 @@ class TestSavedReportCitations:
def test_brackets_before_parenthesised_prose_are_not_a_link(self):
# A destination cannot contain a space, so this is text, not a link.
- markdown = report("Table [[7]](see below) lists them.")
+ markdown = report("Table [[3]](see below) lists them.")
assert exported_answer(markdown) == (
- "Table [\\cite{7}](see below) lists them."
+ "Table [\\cite{3}](see below) lists them."
)
def test_a_prose_link_is_left_as_written(self):