mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
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.
This commit is contained in:
1 parent
9f73a01143
commit
dca3593452
2 files changed
+27
-2
No files matched your search
@@ -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));
|
||||
|
||||
@@ -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<_>>(), 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`.
|
||||
|
||||
Reference in new issue
Block a user