Number appendix sections with letters
This commit is contained in:
1 parent
d982e9266c
commit
6176edccc4
5 files changed
+58
-7
No files matched your search
+35
-3
@@ -2113,10 +2113,17 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
|
||||
/// that number, and label-only blocks are removed.
|
||||
fn number_sections(blocks: &mut Vec<SourceParagraph>) -> Vec<(String, String)> {
|
||||
let mut counters = [0u32; 3];
|
||||
let mut appendix = false;
|
||||
let mut labels = Vec::new();
|
||||
let mut last_number: Option<String> = None;
|
||||
let mut index = 0;
|
||||
while index < blocks.len() {
|
||||
if blocks[index].text.trim() == "\\appendix" {
|
||||
appendix = true;
|
||||
counters = [0; 3];
|
||||
blocks.remove(index);
|
||||
continue;
|
||||
}
|
||||
let level = blocks[index].heading_level;
|
||||
let block_labels = take_labels(&mut blocks[index].text);
|
||||
if level.is_none() {
|
||||
@@ -2149,10 +2156,15 @@ fn number_sections(blocks: &mut Vec<SourceParagraph>) -> Vec<(String, String)> {
|
||||
for deeper in counters.iter_mut().skip(counter + 1) {
|
||||
*deeper = 0;
|
||||
}
|
||||
let first = if appendix {
|
||||
appendix_letter(counters[0])
|
||||
} else {
|
||||
counters[0].to_string()
|
||||
};
|
||||
let number = match level {
|
||||
1 => format!("{}", counters[0]),
|
||||
2 => format!("{}.{}", counters[0], counters[1]),
|
||||
_ => format!("{}.{}.{}", counters[0], counters[1], counters[2]),
|
||||
1 => first,
|
||||
2 => format!("{first}.{}", counters[1]),
|
||||
_ => format!("{first}.{}.{}", counters[1], counters[2]),
|
||||
};
|
||||
blocks[index].heading_number = Some(number.clone());
|
||||
last_number = Some(number.clone());
|
||||
@@ -2166,6 +2178,15 @@ fn number_sections(blocks: &mut Vec<SourceParagraph>) -> Vec<(String, String)> {
|
||||
labels
|
||||
}
|
||||
|
||||
/// The letter used for an appendix section counter.
|
||||
fn appendix_letter(value: u32) -> String {
|
||||
match value {
|
||||
0 => "0".to_string(),
|
||||
1..=26 => char::from(b'A' + value as u8 - 1).to_string(),
|
||||
_ => value.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Replace section references and turn unresolved `\ref`/`\eqref` into `??`.
|
||||
fn resolve_section_refs(blocks: &mut [SourceParagraph], labels: &[(String, String)]) {
|
||||
for block in blocks {
|
||||
@@ -2869,6 +2890,17 @@ mod source_tests {
|
||||
assert_eq!(document.units[1].source, "See 1.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn appendix_switches_to_letter_numbering() {
|
||||
let source = "\\section{Intro}\n\n\\appendix\n\n\\section{Extra}\n\n\\subsection{Detail}";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let blocks = paragraphs(&body).expect("blocks");
|
||||
let document = build_document(&blocks, &geometry);
|
||||
assert_eq!(document.units[0].source, "1 Intro");
|
||||
assert_eq!(document.units[1].source, "A Extra");
|
||||
assert_eq!(document.units[2].source, "A.1 Detail");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn standalone_labels_attach_to_the_previous_heading() {
|
||||
let source = "\\section{A}\n\n\\subsection{B}\n\n\\label{sec:b}\n\nSee \\ref{sec:b} and \\ref{missing}.";
|
||||
|
||||
Reference in new issue
Block a user