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): accept a space before the comma in group citation labels (#6954)

LearningCircuitbd4d0fe589b8
diff --git a/changelog.d/6944.bugfix.md b/changelog.d/6944.bugfix.md
new file mode 100644
index 0000000000..bc8ccddb1f
--- /dev/null
+++ b/changelog.d/6944.bugfix.md
@@ -0,0 +1 @@
+Hand-edited group citation labels with a space before the comma (`[[2 ,3]]`) now stay a group through export instead of falling back to URL lookup and dropping every member after the first; the label and comma-group patterns accept whitespace on either side of the comma, matching the padding they already allowed elsewhere.
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index 76a8852881..2853840c49 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -529,7 +529,7 @@ class CitationFormatter:
r"(?<![\[【])[\[【](\d+)[\]】](?![\]】])"
)
self.comma_citation_pattern = re.compile(
- r"[\[【](\d+(?:,\s*\d+)+)[\]】]"
+ r"[\[【](\d+(?:\s*,\s*\d+)+)[\]】]" # (#6944) space allowed BEFORE the comma too — a hand-edited `[[2 ,3]]` must stay a group
)
# Also match "Source X" or "source X" patterns
self.source_word_pattern = re.compile(r"\b[Ss]ource\s+(\d+)\b")
@@ -1707,7 +1707,7 @@ class QuartoExporter:
r"(?<![\[【])[\[【](\d+)[\]】](?![\]】])"
)
self.comma_citation_pattern = re.compile(
- r"[\[【](\d+(?:,\s*\d+)+)[\]】]"
+ r"[\[【](\d+(?:\s*,\s*\d+)+)[\]】]" # (#6944) space allowed BEFORE the comma too — a hand-edited `[[2 ,3]]` must stay a group
)
def export_to_quarto(self, content: str, title: str | None = None) -> str:
@@ -2405,8 +2405,11 @@ class RISExporter:
import re
# Pattern to match each source entry. The opener is GROUP-AWARE
- # (``\d+(?:,\s*\d+)*``, the spelling used by
- # ``comma_citation_pattern`` and ``_BIB_SOURCES_PATTERN`` above):
+ # (``\d+(?:,\s*\d+)*``, the spelling ``_BIB_SOURCES_PATTERN`` above
+ # uses for Sources lines; the inline ``comma_citation_pattern``,
+ # ``\d+(?:\s*,\s*\d+)+``, also accepts whitespace before a comma
+ # since #6944, while ``format_links_to_markdown`` joins a merged
+ # opener's indices with ``", "``):
# ``format_links_to_markdown`` collapses every citation of one source
# onto a single merged ``[1, 3] Title`` line, which is routine for
# library documents since per-document keying (#5685). A single-index
@@ -3400,7 +3403,7 @@ def _defuse_report_math_for_quarto(content: str) -> str:
# shape; only the label differs ([[1]], [[arxiv.org]], [[arxiv.org-2]],
# [[doi-1]]).
_CITATION_LINK_START_RE = re.compile(r"\[\[([^\[\]\n]+)\]\]\(")
-_CITATION_GROUP_LABEL_RE = re.compile(r"\s*\d+(?:,\s*\d+)+\s*")
+_CITATION_GROUP_LABEL_RE = re.compile(r"\s*\d+(?:\s*,\s*\d+)+\s*")
def _link_destination_ends(text: str) -> array:
@@ -3438,7 +3441,7 @@ class LaTeXExporter:
# Also match Unicode lenticular brackets 【】 (U+3010 and U+3011) that LLMs sometimes generate
self.citation_pattern = re.compile(r"[\[【](\d+)[\]】]")
self.comma_citation_pattern = re.compile(
- r"[\[【](\d+(?:,\s*\d+)+)[\]】]"
+ r"[\[【](\d+(?:\s*,\s*\d+)+)[\]】]" # (#6944) space allowed BEFORE the comma too — a hand-edited `[[2 ,3]]` must stay a group
)
self.heading_patterns = [
(re.compile(r"^# (.+)$", re.MULTILINE), r"\\section{\1}"),
diff --git a/tests/text_optimization/test_latex_citation_links.py b/tests/text_optimization/test_latex_citation_links.py
index f06038b7e0..e350862352 100644
--- a/tests/text_optimization/test_latex_citation_links.py
+++ b/tests/text_optimization/test_latex_citation_links.py
@@ -254,6 +254,19 @@ class TestCommaGroups:
assert exported_answer(markdown) == "Both agree \\cite{2,3}."
+ def test_a_group_label_with_space_before_the_comma_cites_every_member(
+ self,
+ ):
+ # #6944: a hand-edited label with the space before the comma.
+ # The pattern allowed whitespace after the comma only, so this
+ # shape failed the group match, fell to single-citation/URL
+ # lookup, and dropped the second member from the \cite.
+ markdown = report(
+ "Both agree [[2 ,3]](https://arxiv.org/abs/2401.00002)."
+ )
+
+ assert exported_answer(markdown) == "Both agree \\cite{2,3}."
+
def test_a_group_label_with_a_member_the_sources_lack_is_left_as_written(
self,
):