Polish preprint title details and plainnat bibliography formatting
This commit is contained in:
1 parent
7f1f202de1
commit
d211889e93
8 files changed
+120
-11
No files matched your search
+87
-1
@@ -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::<Vec<_>>();
|
||||
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 {
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -530,6 +530,10 @@ struct CompilerState {
|
||||
pending_page_decorations: Vec<PendingPageDecoration>,
|
||||
#[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)?;
|
||||
|
||||
Reference in new issue
Block a user