From dca3593452635b2e8c75da2795fda830c3be451f Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan <126534658+scks1@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:37:17 -0500 Subject: [PATCH] craft-latex: skip the blanks after a control space TeX skips the blanks after a control space, `\ ` or `\` and a tab, as it does after `\` at a line end. The lexer left them to the whitespace branch, which added a second space, so text taken as written, such as a reference or an `\item` label, read `one two`. The control-space branch now skips them the way the line-end branch does, and a blank line after one still ends the paragraph. --- crates/latex/src/lex.rs | 10 ++++++++-- crates/latex/src/tests.rs | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/latex/src/lex.rs b/crates/latex/src/lex.rs index 82eabe15f..5040f9d9f 100644 --- a/crates/latex/src/lex.rs +++ b/crates/latex/src/lex.rs @@ -151,8 +151,14 @@ impl Lexer<'_> { } let Some(sym) = self.bump() else { break }; match sym { - // `\^^I`, likewise. - '\t' => self.out.push(Tok::Cmd(" ".to_string())), + // `\ ` and `\^^I` (a tab) are control spaces too. TeX skips the blanks + // after them, but a blank line after one still ends the paragraph. + ' ' | '\t' => { + self.out.push(Tok::Cmd(" ".to_string())); + if self.blanks() >= 2 { + self.out.push(Tok::Par); + } + } '(' => { let m = self.until("\\)"); self.out.push(Tok::Math(m, false)); diff --git a/crates/latex/src/tests.rs b/crates/latex/src/tests.rs index 5f6311094..85fee24e4 100644 --- a/crates/latex/src/tests.rs +++ b/crates/latex/src/tests.rs @@ -304,6 +304,25 @@ fn a_control_space_at_a_line_end_adds_no_second_space() { assert_eq!(texts("\\begin{document}\\ref{one\\\r\n two}\\end{document}"), vec!["one two".to_string()]); } +#[test] +fn blanks_after_a_control_space_add_no_second_space() { + // TeX skips the blanks after `\ ` and after `\` and a tab, but they were read as a space of + // their own, so text taken as written had two spaces. + for cs in ["\\ ", "\\\t"] { + assert_eq!(tokenize(&format!("a{cs} \tb")), vec![Tok::Text("a".into()), Tok::Cmd(" ".into()), Tok::Text("b".into())], "{cs:?}"); + assert_eq!(texts(&format!("\\begin{{document}}\\ref{{one{cs}\n two}}\\end{{document}}")), vec!["one two".to_string()], "{cs:?}"); + assert_eq!(texts(&format!("\\begin{{document}}\\ref{{one{cs} two}}\\end{{document}}")), vec!["one two".to_string()], "{cs:?}"); + let t = texts(&format!("\\begin{{document}}\\begin{{description}}\\item[one{cs} two] x\\end{{description}}\\end{{document}}")); + assert_eq!(t.iter().map(|s| s.trim()).collect::>(), vec!["one two x"], "{cs:?}"); + // A blank line after it still ends the paragraph. + assert_eq!( + texts(&format!("\\begin{{document}}first{cs}\n\nsecond\\end{{document}}")), + vec!["first".to_string(), "second".to_string()], + "{cs:?}" + ); + } +} + #[test] fn a_control_space_in_an_item_label_is_a_space() { // The optional argument kept a control symbol as written, so the label read `one\ two`.