~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(latex): label each bibitem with its citation index (#6832)
Lukasd6daf0b242a7
diff --git a/changelog.d/6832.bugfix.md b/changelog.d/6832.bugfix.mdnew file mode 100644index 0000000000..f16fba92e3--- /dev/null+++ b/changelog.d/6832.bugfix.md@@ -0,0 +1 @@+LaTeX export labels each bibliography entry with its citation number, so a report whose sources are numbered with gaps (for example [1] and [5]) no longer prints them as [1] and [2] in the text and the bibliography.diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.pyindex ccadc017f8..c4274681a6 100644--- a/src/local_deep_research/text_optimization/citation_formatter.py+++ b/src/local_deep_research/text_optimization/citation_formatter.py@@ -2163,19 +2163,21 @@ class LaTeXExporter:sources_content = content[sources_start:]entries = _collect_bibliography_entries(sources_content)- bibliography = "\n\\begin{thebibliography}{99}\n"+ # The argument is the widest label, which sets the hanging indent. It+ # is never narrower than the two digits it has always been.+ widest = "9" * max([2, *(len(key) for key in entries)])+ bibliography = f"\n\\begin{{thebibliography}}{{{widest}}}\n"for citation_num in sorted(entries, key=_sort_key):title, url = entries[citation_num]escaped_title = self._escape_latex(title)- # NOTE: thebibliography numbers entries by POSITION, so a gap- # in the index sequence makes \\cite{5} print [3]. That is- # pre-existing and unchanged here — an explicit- # ``\\bibitem[label]{key}`` would fix it but changes an output- # format existing tests pin, so it belongs in its own PR.- # Sorting at least keeps the common contiguous case correct.- item = f"\\bibitem{{{citation_num}}}"+ # thebibliography numbers entries by POSITION unless a label+ # is given, so without one a gap in the index sequence makes+ # \\cite{5} print [3]. The label is the index the report shows,+ # as written: 007 stays 007, and a non-digit key such as a 7a+ # sub-index would be printed as it is too.+ item = f"\\bibitem[{citation_num}]{{{citation_num}}}"safe_url = _safe_bibtex_url(url)if safe_url:bibliography += f"{item} {escaped_title}. \\url{{{safe_url}}}\n"diff --git a/tests/citation_bugs/test_citation_export_formats.py b/tests/citation_bugs/test_citation_export_formats.pyindex a5a482aa58..c740a322db 100644--- a/tests/citation_bugs/test_citation_export_formats.py+++ b/tests/citation_bugs/test_citation_export_formats.py@@ -85,8 +85,8 @@ URL: https://math.edu/integrals"""assert r"$$\int_0^\infty" in latex_output# Should have proper bibliography- assert r"\bibitem{1}" in latex_output- assert r"\bibitem{2}" in latex_output+ assert r"\bibitem[1]{1}" in latex_output+ assert r"\bibitem[2]{2}" in latex_outputdef test_latex_export_special_characters_escaping(self):"""Test LaTeX export properly escapes special characters."""@@ -186,8 +186,8 @@ URL: https://same.url/paper"""# LaTeX should have separate bibitemslatex_exporter = LaTeXExporter()latex_output = latex_exporter.export_to_latex(content)- assert r"\bibitem{1}" in latex_output- assert r"\bibitem{2}" in latex_output+ assert r"\bibitem[1]{1}" in latex_output+ assert r"\bibitem[2]{2}" in latex_outputdef test_export_preserves_markdown_structure(self):"""Test that exports preserve document structure."""diff --git a/tests/text_optimization/test_citation_formatter.py b/tests/text_optimization/test_citation_formatter.pyindex e5c2696159..373a4c2c48 100644--- a/tests/text_optimization/test_citation_formatter.py+++ b/tests/text_optimization/test_citation_formatter.py@@ -1077,7 +1077,7 @@ Text with citations [1] and [2].# Check bibliographyassert r"\begin{thebibliography}" in result- assert r"\bibitem{1}" in result+ assert r"\bibitem[1]{1}" in resultdef test_latex_list_conversion(self, sample_markdown):"""Test list conversion to LaTeX."""diff --git a/tests/text_optimization/test_citation_formatter_behavior.py b/tests/text_optimization/test_citation_formatter_behavior.pyindex 58c47043f3..870c495a4d 100644--- a/tests/text_optimization/test_citation_formatter_behavior.py+++ b/tests/text_optimization/test_citation_formatter_behavior.py@@ -1257,7 +1257,7 @@ class TestLaTeXExporterCreateBibliography:exporter = LaTeXExporter()content = "## Sources\n\n[1] Test Source Title"result = exporter._create_bibliography(content)- assert "\\bibitem{1}" in result+ assert "\\bibitem[1]{1}" in resultdef test_includes_url_in_bibitem(self):"""Includes URL in bibitem entry."""diff --git a/tests/text_optimization/test_citation_formatter_bibliography.py b/tests/text_optimization/test_citation_formatter_bibliography.pyindex 968fac9268..a7bf2bbcdc 100644--- a/tests/text_optimization/test_citation_formatter_bibliography.py+++ b/tests/text_optimization/test_citation_formatter_bibliography.py@@ -55,6 +55,6 @@ def test_latex_bibitems_are_ordered_by_index():]assert "Doc A" in tex- # thebibliography numbers by POSITION, so out-of-order keys make- # \\cite{3} print [2]. A merged [1, 3] group must not disturb that.- assert items == ["\\bibitem{1}", "\\bibitem{2}", "\\bibitem{3}"]+ # The bibliography reads in index order, and a merged [1, 3] group+ # must not disturb that.+ assert items == ["\\bibitem[1]{1}", "\\bibitem[2]{2}", "\\bibitem[3]{3}"]diff --git a/tests/text_optimization/test_citation_formatter_contracts.py b/tests/text_optimization/test_citation_formatter_contracts.pyindex 06d8f48ee8..94db9f52e0 100644--- a/tests/text_optimization/test_citation_formatter_contracts.py+++ b/tests/text_optimization/test_citation_formatter_contracts.py@@ -294,10 +294,12 @@ class TestRoundTripCitationIdentity:assert "Claims \\cite{1} and \\cite{2}." in latexbib = bibliography_of_latex(latex)assert (- "\\bibitem{1} Alpha Paper. \\url{https://arxiv.org/abs/2401.00001}"+ "\\bibitem[1]{1} Alpha Paper. \\url{https://arxiv.org/abs/2401.00001}"in bib)- assert "\\bibitem{2} Beta Blog. \\url{https://beta.example/post}" in bib+ assert (+ "\\bibitem[2]{2} Beta Blog. \\url{https://beta.example/post}" in bib+ )def test_ris_keeps_index_to_url_pairing(self):ris = RISExporter().export_to_ris(self.DOCUMENT)@@ -317,15 +319,6 @@ class TestRoundTripCitationIdentity:assert "[\\cite{1}](" not in bodyassert "\\cite{1}" in body- @pytest.mark.xfail(- strict=True,- reason=(- "DEFECT (acknowledged in-code): thebibliography numbers "- "entries by position, so with a gap in the index sequence "- "\\cite{5} prints [2]. An explicit \\bibitem[label]{key} "- "would preserve the number the reader sees in the markdown."- ),- )def test_latex_bibitem_survives_a_gap_in_the_index_sequence(self):document = make_doc("See [1] and [5].",@@ -336,6 +329,30 @@ class TestRoundTripCitationIdentity:# A labelled bibitem is what makes \cite{5} render as [5].assert "\\bibitem[5]{5}" in bib+ def test_latex_bibitem_label_keeps_the_index_as_written(self):+ document = make_doc(+ "See [7] and [007].",+ "[7] Seven\nURL: https://seven.example\n\n"+ "[007] Bond\nURL: https://bond.example",+ )+ bib = bibliography_of_latex(LaTeXExporter().export_to_latex(document))+ assert "\\bibitem[7]{7} Seven." in bib+ assert "\\bibitem[007]{007} Bond." in bib++ def test_latex_widest_label_grows_with_the_longest_index(self):+ few = LaTeXExporter().export_to_latex(+ make_doc("See [1].", "[1] One\nURL: https://one.example")+ )+ many = LaTeXExporter().export_to_latex(+ make_doc(+ "See [1] and [120].",+ "[1] One\nURL: https://one.example\n\n"+ "[120] Many\nURL: https://many.example",+ )+ )+ assert "\\begin{thebibliography}{99}" in few+ assert "\\begin{thebibliography}{999}" in many+class TestUrlSchemeHandling:"""``javascript:`` / ``data:`` destinations and link injection."""@@ -769,7 +786,7 @@ class TestBibliographySelection:bib = bibliography_of_latex(LaTeXExporter().export_to_latex(self.POISONED))- assert "\\bibitem{1} Real Local Document." in bib+ assert "\\bibitem[1]{1} Real Local Document." in bibassert "evil.example" not in bib@pytest.mark.xfail(diff --git a/tests/text_optimization/test_integration.py b/tests/text_optimization/test_integration.pyindex 87738afd4f..6126a5c4bf 100644--- a/tests/text_optimization/test_integration.py+++ b/tests/text_optimization/test_integration.py@@ -78,7 +78,7 @@ This is a test with citation [1].assert r"\documentclass[12pt]{article}" in latex_contentassert r"\section{Test Report}" in latex_contentassert r"\cite{1}" in latex_content- assert r"\bibitem{1}" in latex_content+ assert r"\bibitem[1]{1}" in latex_content@patch("local_deep_research.config.search_config.get_setting_from_snapshot")def test_real_world_citation_formatting(self, mock_get_setting):diff --git a/tests/web/services/test_export_generation_contracts.py b/tests/web/services/test_export_generation_contracts.pyindex 91b7d247e9..dfb2e611c2 100644--- a/tests/web/services/test_export_generation_contracts.py+++ b/tests/web/services/test_export_generation_contracts.py@@ -346,7 +346,7 @@ class TestLaTeXBibliographyIsAlreadyHardened:tex = _export_latex(report)bibliography = tex[tex.index("\\begin{thebibliography}") :]- assert "\\bibitem{1}" in bibliography # entry still emitted+ assert "\\bibitem[1]{1}" in bibliography # entry still emittedassert "\\url{" not in bibliography # but with no linkassert "\\input" not in bibliography