From c2310d85ac1f518e517aa36c654e560ca29f3dce Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 27 Oct 2026 23:07:00 +0800 Subject: [PATCH] Emit author-year labels for project BibTeX The project .bib inliner now derives author-year labels (with a/b disambiguation for duplicate author-years) and writes them as optional \bibitem labels. The route-D citation pass uses those labels only when they look like author-year labels: \citep gets parentheses, \citet becomes Author (Year), and reference entries lose their numeric brackets. Inline thebibliography entries without such labels remain numeric. The arXiv sample now prints (Kour and Saabne, 2014a; Keshet et al., 2016) and Kour and Saabne (2014b), matching natbib semantics more closely. Test count stays 1073. --- docs/TEX_FIDELITY_ROADMAP.md | 4 ++- docs/WEB_ROUTE_D_EXPERIMENT.md | 6 ++-- src/main.rs | 62 ++++++++++++++++++++++++++++++---- src/route_d.rs | 35 +++++++++++++++++-- tests/server.rs | 2 +- 5 files changed, 96 insertions(+), 13 deletions(-) diff --git a/docs/TEX_FIDELITY_ROADMAP.md b/docs/TEX_FIDELITY_ROADMAP.md index faac519..1a6b24d 100644 --- a/docs/TEX_FIDELITY_ROADMAP.md +++ b/docs/TEX_FIDELITY_ROADMAP.md @@ -44,7 +44,9 @@ execution is not a goal. "A Preprint" undertitle are drawn, and two author groups are laid out side by side as a centred two-column halign. ORCID icons are still missing; - - reference style is numeric, not natbib author-year; + - project `.bib` entries now produce author-year labels and natbib-style + parenthetical citations; inline `thebibliography` without labels remains + numeric, and the full natbib option surface is not implemented; - display math is approximate; - `\fbox`/rule figures now draw the rule body as a filled box; the fbox frame and vector/PDF images are still missing, and float placement differs. diff --git a/docs/WEB_ROUTE_D_EXPERIMENT.md b/docs/WEB_ROUTE_D_EXPERIMENT.md index 685b5b6..d9bc167 100644 --- a/docs/WEB_ROUTE_D_EXPERIMENT.md +++ b/docs/WEB_ROUTE_D_EXPERIMENT.md @@ -51,8 +51,8 @@ now means route-D. | Floats, captions, tables, booktabs | Supported | table/math tests, corpus | | PNG/JPEG/BMP `\includegraphics` | Supported | project image tests | | PDF/SVG/EPS figures | **Not supported** | undecodable images become placeholder rules | -| Inline `thebibliography` + `\cite` | Supported | citation tests | -| `.bib` + BibTeX data | Partial | simple `@article`/`@inproceedings` fields are inlined as `thebibliography`; complex BibTeX/BibLaTeX styles are not supported | +| Inline `thebibliography` + `\cite` | Supported | numeric or custom labels; citation tests | +| `.bib` + BibTeX data | Partial | simple `@article`/`@inproceedings` fields are inlined as labeled `thebibliography`; project citations use author-year labels (`Kour and Saabne, 2014a`) | | Local `.cls`/`.sty`/`\input` | Supported | project class/package tests | | Arbitrary package code, TikZ/pgf | **Not supported** | text/class subset only | | Two-column / `multicols` | Flattened | wrappers consumed; no column layout | @@ -145,7 +145,7 @@ PDF was rendered page-by-page next to the route-D output. | Title | `A TEMPLATE FOR THE arxiv STYLE`, small caps, rules above/below | normal case, no rules | | Author block | two affiliations, ORCID icons, footnote symbols | one concatenated paragraph, no ORCID | | Abstract/keywords | small-caps heading, bold `Keywords`, `·` separators | bold heading, comma separators | -| Citations | natbib author-year `(Kour and Saabne, 2014a; …)` | numeric `[1, 3]` | +| Citations | natbib author-year `(Kour and Saabne, 2014a; …)` | author-year `(Kour and Saabne, 2014a; Keshet et al., 2016)` for `.bib`; numeric for unlabeled inline bibliographies | | Table | booktabs rules, `Size (µm)`, `~100`, `10^6` | readable ASCII fallback `Size (um)`, `approx. 100`, `10**6`; rough header | | Figure | framed rule box above the caption | filled rule body; fbox frame missing | | Display equation | fully typeset fraction/product/sum | approximate, visibly jumbled | diff --git a/src/main.rs b/src/main.rs index 8bbeb52..dece3e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1241,12 +1241,15 @@ fn expand_project_bibliography(source: &str, store: &projects::ProjectStore, id: output.push_str(names); output.push('}'); } else { + let labels = disambiguate_bib_labels(&entries); output.push_str("\\begin{thebibliography}{99}\n"); - for (key, entry) in entries { - output.push_str("\\bibitem{"); - output.push_str(&key); + for ((key, entry, _), label) in entries.iter().zip(labels) { + output.push_str("\\bibitem["); + output.push_str(&label); + output.push_str("]{"); + output.push_str(key); output.push_str("} "); - output.push_str(&entry); + output.push_str(entry); output.push('\n'); } output.push_str("\\end{thebibliography}\n"); @@ -1259,7 +1262,7 @@ fn expand_project_bibliography(source: &str, store: &projects::ProjectStore, id: /// Parse the small BibTeX subset used by paper templates: entries with /// braced or quoted field values. Unknown entry types are skipped. -fn parse_bib_entries(text: &str) -> Vec<(String, String)> { +fn parse_bib_entries(text: &str) -> Vec<(String, String, String)> { let mut entries = Vec::new(); let mut cursor = 0usize; while let Some(relative) = text[cursor..].find('@') { @@ -1314,7 +1317,7 @@ fn parse_bib_entries(text: &str) -> Vec<(String, String)> { } let fields = parse_bib_fields(&body[comma + 1..]); if let Some(entry) = format_bib_entry(&fields) { - entries.push((key.to_string(), entry)); + entries.push((key.to_string(), entry, bib_label(&fields))); } } entries @@ -1388,6 +1391,53 @@ fn parse_bib_fields(text: &str) -> Vec<(String, String)> { fields } +/// Author-year label for a BibTeX entry, e.g. `Kour and Saabne, 2014`. +fn bib_label(fields: &[(String, String)]) -> String { + let field = |name: &str| { + fields + .iter() + .find(|(field, _)| field == name) + .map(|(_, value)| value.as_str()) + }; + let authors: Vec<&str> = field("author") + .unwrap_or("") + .split(" and ") + .map(|author| author.split(',').next().unwrap_or(author).trim()) + .filter(|author| !author.is_empty()) + .collect(); + let names = match authors.as_slice() { + [] => "Anon.".to_string(), + [one] => (*one).to_string(), + [one, two] => format!("{one} and {two}"), + [one, ..] => format!("{one} et al."), + }; + match field("year") { + Some(year) => format!("{names}, {year}"), + None => names, + } +} + +/// Append a/b suffixes when two entries share the same author-year label. +fn disambiguate_bib_labels(entries: &[(String, String, String)]) -> Vec { + let mut totals: std::collections::HashMap<&str, usize> = std::collections::HashMap::new(); + for (_, _, label) in entries { + *totals.entry(label.as_str()).or_default() += 1; + } + let mut seen: std::collections::HashMap<&str, usize> = std::collections::HashMap::new(); + entries + .iter() + .map(|(_, _, label)| { + if totals.get(label.as_str()).copied().unwrap_or(0) <= 1 { + return label.clone(); + } + let index = seen.entry(label.as_str()).or_default(); + let suffix = char::from(b'a' + (*index).min(25) as u8); + *index += 1; + format!("{label}{suffix}") + }) + .collect() +} + fn format_bib_entry(fields: &[(String, String)]) -> Option { let field = |name: &str| { fields diff --git a/src/route_d.rs b/src/route_d.rs index 22ef0e8..d96f913 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -7119,6 +7119,12 @@ fn resolve_citations(paragraphs: &mut [SourceParagraph], bibliography: &[(String rest = ""; break; }; + let author_year = bibliography.iter().any(|(_, entry)| { + entry + .strip_prefix('\u{E030}') + .and_then(|rest| rest.split_once('\u{E031}')) + .is_some_and(|(label, _)| label.contains(", ")) + }); let numbers: Vec = body .split(',') .map(|key| { @@ -7135,7 +7141,24 @@ fn resolve_citations(paragraphs: &mut [SourceParagraph], bibliography: &[(String (position + 1).to_string() }) .collect(); - let _ = write!(text, "[{}]", numbers.join(", ")); + if author_year { + if command == r"\citep" { + let _ = write!(text, "({})", numbers.join("; ")); + } else if command == r"\citet" { + let labels: Vec = numbers + .iter() + .map(|label| match label.split_once(", ") { + Some((author, year)) => format!("{author} ({year})"), + None => label.clone(), + }) + .collect(); + text.push_str(&labels.join("; ")); + } else { + text.push_str(&numbers.join("; ")); + } + } else { + let _ = write!(text, "[{}]", numbers.join(", ")); + } rest = &after[consumed..]; } text.push_str(rest); @@ -13274,6 +13297,10 @@ fn append_bibliography( .unwrap_or_else(|| format!("{}", index + 1)), None => format!("{}", index + 1), }; + let labelled = entry + .strip_prefix('\u{E030}') + .and_then(|rest| rest.split_once('\u{E031}')) + .is_some_and(|(label, _)| label.contains(", ")); let body = entry .split_once('\u{E031}') .map(|(_, body)| body) @@ -13284,7 +13311,11 @@ fn append_bibliography( list_depth: None, list_label: 0, right_skip: 0, - text: format!("[{marker}] {body}"), + text: if labelled { + body.to_string() + } else { + format!("[{marker}] {body}") + }, page_break: false, centered: false, right_aligned: false, diff --git a/tests/server.rs b/tests/server.rs index c81c323..cb0cd2e 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1253,7 +1253,7 @@ fn project_preview_resolves_bibtex() { } let text: String = text.split_whitespace().collect(); assert!(text.contains("References"), "{text}"); - assert!(text.contains("[1]"), "{text}"); + assert!(text.contains("Doe,2024"), "{text}"); assert!(text.contains("Alphaentry"), "{text}"); assert!(!text.contains("[?]"), "{text}"); }