From d211889e93f4684552a6ae6f997a131966808e56 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Mon, 19 Oct 2026 15:53:47 +0800 Subject: [PATCH] Polish preprint title details and plainnat bibliography formatting --- README.md | 5 +- crates/texrra-core/src/document_parser.rs | 1 + docs/ROADMAP.md | 2 +- docs/STATUS.md | 8 ++- src/rust_typeset.rs | 88 ++++++++++++++++++++++- src/rust_typeset/frontend.rs | 4 +- src/rust_typeset/texlang_adapter.rs | 9 ++- tests/typeset.rs | 14 +++- 8 files changed, 120 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 794ae56..f9a79b3 100644 --- a/README.md +++ b/README.md @@ -377,8 +377,9 @@ author/editor, title, journal, booktitle, volume, number, pages, publisher, institution, school, organization, edition, address, month, note, and year fields. BibTeX `@string` macros and single-level `crossref` inheritance are supported. Manual `thebibliography` entries also work. Documents loading `natbib` or -`arxiv.sty` render `\citet`/`\citep` as author-year citations and omit numeric -bibliography labels; other documents keep numeric citations. Custom `.bst` +`arxiv.sty` render `\citet`/`\citep` as author-year citations, format `.bib` +entries in plainnat order (`First Last`, `In ...`, `pages`, publisher/year), +and omit numeric bibliography labels; other documents keep numeric citations. Custom `.bst` sorting/formatting and page-number references (`\pageref`) are not implemented. A paragraph starts with the current `\parindent` (17pt by default); the diff --git a/crates/texrra-core/src/document_parser.rs b/crates/texrra-core/src/document_parser.rs index a73c79b..3d397f0 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -1983,6 +1983,7 @@ impl Parser { self.flush_paragraph(); let mut heading = Run::plain("Abstract".to_string()); heading.bold = true; + heading.small_caps = true; self.push_block(Block::Centered { runs: vec![heading], }); diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 4a9b886..93e82fd 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -14,7 +14,7 @@ contents and figure/table lists with dotted leaders; native PDF rectangles; unif chapter headings with chapter-prefixed section numbering and page breaks; deferred top-float queue for figures; document-level custom `\maketitle`; abstract quotation formatting, unnumbered `\paragraph` headings, natbib-style -author-year citations; +author-year citations and plainnat bibliography formatting; `\vbox`/`\vtop` vertical-material capture with inline splicing (`build_positioned_pages` is the only page builder and no separate break planner remains). diff --git a/docs/STATUS.md b/docs/STATUS.md index bd22c94..e3c97fe 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -66,7 +66,9 @@ the source changes; the SVG preview is refreshed per page. `\hspace*`, skips), text symbols/letters/punctuation, break advisories, metadata, `\paragraph` run-in headings, citations/references, tables/lists/images. - Citations: numeric citations by default; `natbib`/`arxiv.sty` selects author-year - `\citet`/`\citep` from `.bib` metadata and drops numeric bibliography labels. + `\citet`/`\citep` from `.bib` metadata, formats bibliography entries in + plainnat order (`First Last`, `In ...`, `pages`, publisher/year), and drops + numeric bibliography labels. - Live preview: paragraph cache, page-hash diffing, warm rebuilds in the low milliseconds for small edits. @@ -90,6 +92,10 @@ See `docs/ROADMAP.md` for concrete implementation plans. the next page. - Contents: chapter headings are listed with chapter-prefixed section numbers, but contents are not split per chapter. +- Preprint title blocks: ORCID icons embedded in `\author` are dropped because + metadata does not carry PDF images, and links/URLs remain plain black text + instead of colored hyperref links. Title-block vertical spacing is tighter + than the reference class. ## Verification diff --git a/src/rust_typeset.rs b/src/rust_typeset.rs index b380bfb..737c0cf 100644 --- a/src/rust_typeset.rs +++ b/src/rust_typeset.rs @@ -889,6 +889,25 @@ fn source_directory(file_name: &str) -> &Path { .unwrap_or_else(|| Path::new(".")) } +fn natbib_author_list(author: &str) -> String { + let names = author + .split(" and ") + .map(|name| { + let name = name.trim(); + match name.split_once(',') { + Some((last, first)) => format!("{} {}", first.trim(), last.trim()), + None => name.to_string(), + } + }) + .collect::>(); + match names.as_slice() { + [] => String::new(), + [only] => only.clone(), + [first, second] => format!("{first} and {second}"), + [rest @ .., last] => format!("{}, and {last}", rest.join(", ")), + } +} + struct BibEntry { kind: String, key: String, @@ -896,7 +915,10 @@ struct BibEntry { } impl BibEntry { - fn to_latex(&self) -> String { + fn to_latex(&self, author_year: bool) -> String { + if author_year { + return self.natbib_latex(); + } let field = |name: &str| self.fields.get(name).map(String::as_str).unwrap_or(""); let mut parts = Vec::new(); let author = field("author"); @@ -964,6 +986,70 @@ impl BibEntry { } escape_bib_text(&parts.join(" ")) } + + /// plainnat/author-year layout used by `natbib` and the arXiv class. + fn natbib_latex(&self) -> String { + let field = |name: &str| self.fields.get(name).map(String::as_str).unwrap_or(""); + let authors = if !field("author").is_empty() { + natbib_author_list(field("author")) + } else if !field("editor").is_empty() { + format!("{} (ed.)", field("editor")) + } else { + String::new() + }; + let mut output = String::new(); + if !authors.is_empty() { + output.push_str(&authors); + output.push_str(". "); + } + let title = field("title"); + if !title.is_empty() { + output.push_str(title); + output.push_str(". "); + } + let mut tail = Vec::new(); + let booktitle = field("booktitle"); + if !booktitle.is_empty() { + tail.push(format!("In \\emph{{{booktitle}}}")); + } else if !field("journal").is_empty() { + tail.push(format!("\\emph{{{}}}", field("journal"))); + } + if !field("volume").is_empty() { + let mut volume = field("volume").to_string(); + if !field("number").is_empty() { + volume.push_str(&format!("({})", field("number"))); + } + tail.push(volume); + } + if !field("pages").is_empty() { + tail.push(format!( + "pages {}", + field("pages").replace("--", "\u{2013}") + )); + } + if !tail.is_empty() { + output.push_str(&tail.join(", ")); + output.push_str(", "); + } + let publisher = if field("publisher").is_empty() { + field("organization") + } else { + field("publisher") + }; + let year = field("year"); + match (publisher.is_empty(), year.is_empty()) { + (false, false) => output.push_str(&format!("{publisher}, {year}.")), + (false, true) => output.push_str(&format!("{publisher}.")), + (true, false) => output.push_str(&format!("{year}.")), + (true, true) => { + if output.ends_with(", ") { + output.truncate(output.len() - 2); + output.push('.'); + } + } + } + escape_bib_text(output.trim()) + } } fn escape_bib_text(source: &str) -> String { diff --git a/src/rust_typeset/frontend.rs b/src/rust_typeset/frontend.rs index ec16f8f..f2588e7 100644 --- a/src/rust_typeset/frontend.rs +++ b/src/rust_typeset/frontend.rs @@ -453,8 +453,8 @@ const PRELUDE: &str = r#" \def\And{\texrraauthorseparator} \def\AND{\texrraauthorseparator} \def\ifhmode{\iftrue} -\def\paragraph#1{\par\texrraunnumberedheadingstart{3}#1\texrraheadingend\par} -\def\subparagraph#1{\par\texrraunnumberedheadingstart{3}#1\texrraheadingend\par} +\def\paragraph#1{\par\noindent{\bfseries #1}\hspace{1em}} +\def\subparagraph#1{\par\noindent{\itshape #1}\hspace{1em}} \def\@startsection#1#2#3#4#5#6#7{\par\texrraheadingstart{#2}#7\texrraheadingend\par} \def\setlength#1#2{#1=#2\relax} \def\addtolength#1#2{\advance#1#2\relax} diff --git a/src/rust_typeset/texlang_adapter.rs b/src/rust_typeset/texlang_adapter.rs index 623a518..32c0f68 100644 --- a/src/rust_typeset/texlang_adapter.rs +++ b/src/rust_typeset/texlang_adapter.rs @@ -530,6 +530,10 @@ struct CompilerState { pending_page_decorations: Vec, #[serde(skip)] document_started: bool, + /// True when `natbib` (or a class that loads it) is active; controls + /// author-year bibliography formatting and citations. + #[serde(default)] + author_year_bibliography: bool, } struct PendingPageDecoration { @@ -4352,6 +4356,7 @@ fn load_document_class( token, FrontendEventKind::PackageLoaded(class.clone()), ); + input.state_mut().author_year_bibliography |= matches!(class.as_str(), "natbib" | "arxiv"); SourceDependency::record( &mut input.state_mut().dependencies.borrow_mut(), &path, @@ -4647,6 +4652,7 @@ fn use_package( token, FrontendEventKind::PackageLoaded(package.clone()), ); + input.state_mut().author_year_bibliography |= matches!(package.as_str(), "natbib" | "arxiv"); SourceDependency::record( &mut input.state_mut().dependencies.borrow_mut(), &path, @@ -6215,6 +6221,7 @@ fn bibliography_primitive( } .map_err(|message| input.fatal_error(error::SimpleTokenError::new(token, message)))?; + let author_year = input.state().author_year_bibliography; let mut source = String::from("\\begin{thebibliography}{9}"); for entry in entries { source.push_str("\\texrrabibmeta{"); @@ -6226,7 +6233,7 @@ fn bibliography_primitive( source.push_str("}\\bibitem{"); source.push_str(&entry.key); source.push('}'); - source.push_str(&entry.to_latex()); + source.push_str(&entry.to_latex(author_year)); } source.push_str("\\end{thebibliography}"); let generated = lex_generated_tex(input, &source, token)?; diff --git a/tests/typeset.rs b/tests/typeset.rs index 9d9f980..360a241 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -999,7 +999,7 @@ Body. let value: serde_json::Value = serde_json::from_str(&preview).expect("preview JSON"); let svg = value["pages"][0]["svg"].as_str().expect("svg"); assert!( - svg.contains(">Abstract"), + svg.contains(">BSTRACT"), "missing abstract heading: {svg}" ); assert!( @@ -1218,8 +1218,16 @@ fn natbib_citations_use_author_year() { "numeric citation was not replaced: {second}" ); let third = page_svg(&value, 2); + let george = third.find("Geor").expect("bibliography given name missing"); + let kour = third + .find(">Kour") + .expect("bibliography family name missing"); assert!( - third.contains(">Kour"), - "bibliography entry missing: {third}" + george < kour, + "bibliography should render given name before family name: {third}" + ); + assert!( + !third.contains(">[1]"), + "author-year bibliography should omit numeric labels: {third}" ); }