Skip to main content

cellix_doc/
latex.rs

1//! What a paper needs that isn't on the page: which file to build, what to build it with,
2//! what went wrong and where, and Markdown turned into LaTeX for export.
3
4use crate::diagram::{Kind, fenced_tikz, is_tikz, kind_of};
5use crate::figure;
6use std::collections::HashMap;
7use std::path::{Path, PathBuf};
8
9use crate::table::{Align, Edit};
10use pulldown_cmark::{Alignment, CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};
11
12/// A problem from a build, at a line counting from one.
13#[derive(Clone, Debug, PartialEq, Eq)]
14pub struct Problem {
15    pub file: PathBuf,
16    pub line: usize,
17    pub message: String,
18}
19
20/// The file a build starts from: the one a `% !TEX root` comment names, this one if it's a
21/// whole document, or a whole document nearby that really takes this one in.
22pub fn root_of(path: &Path, text: &str) -> PathBuf {
23    let folder = path.parent().unwrap_or(Path::new(""));
24    // TeXShop writes `!TEX`, TeXstudio `!TeX`.
25    let named = text.lines().take(20).find_map(|line| {
26        let rest = line.trim().strip_prefix('%')?.trim_start();
27        let rest = rest
28            .get(4..)
29            .filter(|_| rest[..4].eq_ignore_ascii_case("!tex"))?;
30        let rest = rest.trim_start();
31        let rest = rest
32            .get(4..)
33            .filter(|_| rest[..4].eq_ignore_ascii_case("root"))?;
34        Some(rest.trim_start_matches([' ', '=']).trim().to_string())
35    });
36    if let Some(name) = named.filter(|n| !n.is_empty()) {
37        return folder.join(name);
38    }
39    if crate::paper::is_whole(text) {
40        return path.to_path_buf();
41    }
42    crate::paper::root_including(path).unwrap_or_else(|| path.to_path_buf())
43}
44
45/// A command a paper defines.
46#[derive(Clone, Debug, PartialEq, Eq)]
47pub struct Macro {
48    pub name: String,
49    pub arguments: usize,
50    pub body: String,
51}
52
53/// Where numbering stands.
54#[derive(Clone, Debug, Default, PartialEq, Eq)]
55pub struct Counters {
56    pub algorithms: usize,
57    /// Chapter, section, subsection and subsubsection.
58    pub sections: [usize; 4],
59    pub equations: usize,
60    pub figures: usize,
61    pub tables: usize,
62    /// By counter name, for theorem-like environments.
63    pub theorems: HashMap<String, usize>,
64    /// After `\appendix`, sections count in letters.
65    pub appendix: bool,
66}
67
68impl Counters {
69    /// Counts a heading, and returns the number it prints with. Level 0 is a chapter.
70    pub fn heading(&mut self, level: usize) -> String {
71        self.sections[level] += 1;
72        self.sections[level + 1..].fill(0);
73        let first = usize::from(self.sections[0] == 0);
74        let parts: Vec<String> = (first..=level)
75            .map(|n| {
76                let value = self.sections[n];
77                if self.appendix && n == first {
78                    char::from(b'A' + (value.saturating_sub(1) % 26) as u8).to_string()
79                } else {
80                    value.to_string()
81                }
82            })
83            .collect();
84        parts.join(".")
85    }
86}
87
88/// A theorem-like environment.
89#[derive(Clone, Debug, PartialEq, Eq)]
90pub struct Theorem {
91    pub title: String,
92    /// The counter it shares, usually its own.
93    pub counter: String,
94    pub numbered: bool,
95}
96
97/// Names that need no `\newtheorem` to be read as what they are.
98const THEOREMS: [(&str, &str); 12] = [
99    ("theorem", "Theorem"),
100    ("thm", "Theorem"),
101    ("lemma", "Lemma"),
102    ("lem", "Lemma"),
103    ("proposition", "Proposition"),
104    ("prop", "Proposition"),
105    ("corollary", "Corollary"),
106    ("cor", "Corollary"),
107    ("definition", "Definition"),
108    ("defn", "Definition"),
109    ("remark", "Remark"),
110    ("example", "Example"),
111];
112
113/// A paper as LaTeX sees it: what's defined and how things number, across all its files.
114#[derive(Clone, Debug, Default, PartialEq, Eq)]
115pub struct Project {
116    pub macros: HashMap<String, Macro>,
117    pub theorems: HashMap<String, Theorem>,
118    /// What each label will print as.
119    pub labels: HashMap<String, String>,
120    /// Numbering as each file starts.
121    pub starts: HashMap<PathBuf, Counters>,
122    /// Numbering as each file ends, so the file that `\\input`s it numbers on from there.
123    pub after: HashMap<PathBuf, Counters>,
124    /// What each citation key shows as: author and year.
125    pub citations: HashMap<String, String>,
126    /// The paper's own packages and commands, which a piece built alone still needs.
127    pub setup: Vec<String>,
128    /// Each `.bib` entry as its reference reads: authors, year and title.
129    pub references: HashMap<String, String>,
130    /// The `\\bibliographystyle`, which says how references are numbered.
131    pub style: String,
132    /// The keys the paper cites, in the order it first cites them.
133    pub cited: Vec<String>,
134    /// The `.bib` files the paper names with `\\bibliography` or `\\addbibresource`.
135    pub bibliographies: Vec<PathBuf>,
136    /// The folders `\graphicspath` names, as written.
137    pub graphicspath: Vec<String>,
138    /// Where `\includegraphics` looks, as LaTeX does: the root's folder, wherever the file
139    /// naming the picture is, then each `\graphicspath` folder. Empty for an unsaved file.
140    pub pictures: Vec<PathBuf>,
141}
142
143/// The folders of a `\graphicspath{{figures/}{img/}}` line.
144fn graphicspath_of(code: &str) -> Vec<String> {
145    let Some(at) = code.find("\\graphicspath") else {
146        return Vec::new();
147    };
148    let rest = code[at + "\\graphicspath".len()..].trim_start();
149    let Some((from, to)) = braced(rest, 0) else {
150        return Vec::new();
151    };
152    let mut inside = &rest[from..to];
153    let mut found = Vec::new();
154    while let Some(open) = inside.find('{') {
155        let Some((from, to)) = braced(inside, open) else {
156            break;
157        };
158        let name = inside[from..to].trim();
159        if !name.is_empty() {
160            found.push(name.to_string());
161        }
162        inside = &inside[to + 1..];
163    }
164    found
165}
166
167/// The argument of `\name{...}` on a line, as a byte range.
168fn braced(text: &str, open: usize) -> Option<(usize, usize)> {
169    if !text[open..].starts_with('{') {
170        return None;
171    }
172    let mut depth = 0;
173    for (i, c) in text[open..].char_indices() {
174        match c {
175            '{' => depth += 1,
176            '}' => {
177                depth -= 1;
178                if depth == 0 {
179                    return Some((open + 1, open + i));
180                }
181            }
182            _ => {}
183        }
184    }
185    None
186}
187
188fn word_after<'a>(line: &'a str, command: &str) -> Option<&'a str> {
189    let start = line.find(command)? + command.len();
190    let (from, to) = braced(line, start)?;
191    Some(&line[from..to])
192}
193
194/// Each row of an `align`, as the number it prints with. Starred or `\notag` rows have none,
195/// and `\tag` names its own.
196pub fn equation_rows(source: &str, numbered: bool, equations: &mut usize) -> Vec<Option<String>> {
197    display_rows(source)
198        .iter()
199        .map(|row| {
200            if let Some(tag) = word_after(row, "\\tag") {
201                return Some(tag.to_string());
202            }
203            if !numbered || row.contains("\\notag") || row.contains("\\nonumber") {
204                return None;
205            }
206            *equations += 1;
207            Some(equations.to_string())
208        })
209        .collect()
210}
211
212/// A display's rows: one for an `equation`, one for each `\\` of an `align`.
213pub fn display_rows(source: &str) -> Vec<&str> {
214    if !source.contains("{align") && !source.contains("{gather") {
215        return vec![source];
216    }
217    let bare = |row: &&str| {
218        let row = row.trim();
219        !row.is_empty() && !(row.starts_with("\\end{") && row.ends_with('}'))
220    };
221    source.split("\\\\").filter(bare).collect()
222}
223
224fn read_macro(line: &str, project: &mut Project) {
225    let text = line.trim_start();
226    for command in ["\\newcommand", "\\renewcommand", "\\providecommand"] {
227        let Some(rest) = text.strip_prefix(command) else {
228            continue;
229        };
230        let rest = rest.strip_prefix('*').unwrap_or(rest);
231        let (name, after) = if let Some(inner) = rest.strip_prefix('{') {
232            let Some((name, after)) = inner.split_once('}') else {
233                return;
234            };
235            (name.to_string(), after)
236        } else {
237            let end = rest[1..]
238                .find(|c: char| !c.is_ascii_alphabetic())
239                .map_or(rest.len(), |i| i + 1);
240            (rest[..end].to_string(), &rest[end..])
241        };
242        let mut after = after;
243        let mut arguments = 0;
244        if let Some(inner) = after.strip_prefix('[') {
245            let Some((count, tail)) = inner.split_once(']') else {
246                return;
247            };
248            arguments = count.trim().parse().unwrap_or(0);
249            after = tail;
250        }
251        if let Some((from, to)) = braced(after, 0) {
252            let body = after[from..to].to_string();
253            let name = name.trim().to_string();
254            project.macros.insert(
255                name.clone(),
256                Macro {
257                    name,
258                    arguments,
259                    body,
260                },
261            );
262        }
263        return;
264    }
265    if let Some(rest) = text.strip_prefix("\\def\\") {
266        let end = rest
267            .find(|c: char| !c.is_ascii_alphabetic())
268            .unwrap_or(rest.len());
269        if let Some((from, to)) = braced(&rest[end..], 0) {
270            let name = format!("\\{}", &rest[..end]);
271            let body = rest[end..][from..to].to_string();
272            project.macros.insert(
273                name.clone(),
274                Macro {
275                    name,
276                    arguments: 0,
277                    body,
278                },
279            );
280        }
281        return;
282    }
283    for command in ["\\DeclareMathOperator*", "\\DeclareMathOperator"] {
284        let Some(rest) = text.strip_prefix(command) else {
285            continue;
286        };
287        let Some((from, to)) = braced(rest, 0) else {
288            return;
289        };
290        let name = rest[from..to].to_string();
291        if let Some((a, b)) = braced(rest, to + 1) {
292            let body = format!("\\operatorname{{{}}}", &rest[a..b]);
293            let arguments = 0;
294            project.macros.insert(
295                name.clone(),
296                Macro {
297                    name,
298                    arguments,
299                    body,
300                },
301            );
302        }
303        return;
304    }
305    if let Some(rest) = text.strip_prefix("\\DeclarePairedDelimiter") {
306        let rest = rest.trim_start();
307        let (name, tail) = match braced(rest, 0) {
308            Some((from, to)) => (rest[from..to].to_string(), &rest[to + 1..]),
309            None => {
310                let end = rest[1..]
311                    .find(|c: char| !c.is_ascii_alphabetic())
312                    .map_or(rest.len(), |i| i + 1);
313                (rest[..end].to_string(), &rest[end..])
314            }
315        };
316        let Some((a, b)) = braced(tail, 0) else {
317            return;
318        };
319        let Some((c, d)) = braced(tail, b + 1) else {
320            return;
321        };
322        let body = format!("\\left{} #1 \\right{}", &tail[a..b], &tail[c..d]);
323        project.macros.insert(
324            name.clone(),
325            Macro {
326                name,
327                arguments: 1,
328                body,
329            },
330        );
331        return;
332    }
333    if let Some(rest) = text.strip_prefix("\\newtheorem") {
334        let numbered = !rest.starts_with('*');
335        let rest = rest.trim_start_matches('*');
336        let Some((a, b)) = braced(rest, 0) else {
337            return;
338        };
339        let name = rest[a..b].to_string();
340        let mut tail = &rest[b + 1..];
341        let mut counter = name.clone();
342        if let Some(inner) = tail.strip_prefix('[') {
343            let Some((shared, after)) = inner.split_once(']') else {
344                return;
345            };
346            counter = shared.to_string();
347            tail = after;
348        }
349        if let Some((c, d)) = braced(tail, 0) {
350            let title = tail[c..d].to_string();
351            project.theorems.insert(
352                name,
353                Theorem {
354                    title,
355                    counter,
356                    numbered,
357                },
358            );
359        }
360    }
361}
362
363impl Project {
364    /// The theorem-like environment called `name`, declared or a familiar one.
365    pub fn theorem(&self, name: &str) -> Option<Theorem> {
366        let plain = name.trim_end_matches('*');
367        if let Some(found) = self.theorems.get(plain) {
368            return Some(found.clone());
369        }
370        let known = THEOREMS.iter().find(|(key, _)| *key == plain)?;
371        Some(Theorem {
372            title: known.1.to_string(),
373            counter: plain.to_string(),
374            numbered: !name.ends_with('*'),
375        })
376    }
377
378    /// Expands the paper's commands in a formula, so the engine sees what they mean.
379    pub fn expand(&self, source: &str) -> String {
380        let mut text = source.to_string();
381        // Macros use macros, but a loop among them must end.
382        for _ in 0..8 {
383            let next = self.expand_once(&text);
384            if next == text {
385                break;
386            }
387            text = next;
388        }
389        text
390    }
391
392    fn expand_once(&self, text: &str) -> String {
393        let mut out = String::with_capacity(text.len());
394        let mut rest = text;
395        while let Some(at) = rest.find('\\') {
396            out.push_str(&rest[..at]);
397            rest = &rest[at..];
398            let end = rest[1..]
399                .find(|c: char| !c.is_ascii_alphabetic())
400                .map_or(rest.len(), |i| i + 1);
401            let name = &rest[..end.max(2).min(rest.len())];
402            let Some(found) = self.macros.get(name) else {
403                out.push_str(name);
404                rest = &rest[name.len()..];
405                continue;
406            };
407            let mut tail = rest[name.len()..]
408                .strip_prefix('*')
409                .unwrap_or(&rest[name.len()..]);
410            let mut body = found.body.clone();
411            for number in 1..=found.arguments {
412                tail = tail.trim_start();
413                let Some((from, to)) = braced(tail, 0) else {
414                    break;
415                };
416                body = body.replace(&format!("#{number}"), &tail[from..to]);
417                tail = &tail[to + 1..];
418            }
419            out.push_str(&body);
420            rest = tail;
421        }
422        out.push_str(rest);
423        out
424    }
425
426    /// Reads a paper from its root file, following `\input` as LaTeX would.
427    pub fn read(root: &Path) -> Self {
428        let mut project = Self::default();
429        let mut counters = Counters::default();
430        let folder = root.parent().unwrap_or(Path::new("")).to_path_buf();
431        project.walk(root, &folder, &mut counters, &mut Vec::new());
432        project.pictures = std::iter::once(folder.clone())
433            .chain(project.graphicspath.iter().map(|path| folder.join(path)))
434            .collect();
435        // The ones it names, wherever they are, then any beside it.
436        let beside = std::fs::read_dir(&folder)
437            .into_iter()
438            .flatten()
439            .flatten()
440            .map(|e| e.path())
441            .filter(|p| p.extension().is_some_and(|e| e == "bib"));
442        let named = project.bibliographies.clone();
443        for path in named.into_iter().chain(beside) {
444            if let Ok(text) = std::fs::read_to_string(path) {
445                for entry in bib_entries(&text) {
446                    project
447                        .references
448                        .entry(entry.key.clone())
449                        .or_insert_with(|| entry.reference());
450                    let shown = entry.short();
451                    project.citations.entry(entry.key).or_insert(shown);
452                }
453            }
454        }
455        project
456    }
457
458    /// Reads one file that isn't saved anywhere yet.
459    pub fn from_text(text: &str) -> Self {
460        let mut project = Self::default();
461        let mut counters = Counters::default();
462        project.lines(text, Path::new(""), &mut counters, &mut Vec::new());
463        project
464    }
465
466    /// Reads one file of the paper. `stack` holds the files being read in, so one that takes
467    /// itself in is read once.
468    fn walk(
469        &mut self,
470        path: &Path,
471        folder: &Path,
472        counters: &mut Counters,
473        stack: &mut Vec<PathBuf>,
474    ) {
475        let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
476        if stack.contains(&canonical) || stack.len() > 16 {
477            return;
478        }
479        let Ok(text) = std::fs::read_to_string(path) else {
480            return;
481        };
482        stack.push(canonical);
483        self.starts.insert(path.to_path_buf(), counters.clone());
484        self.lines(&text, folder, counters, stack);
485        self.after.insert(path.to_path_buf(), counters.clone());
486        stack.pop();
487    }
488
489    fn lines(
490        &mut self,
491        text: &str,
492        folder: &Path,
493        counters: &mut Counters,
494        stack: &mut Vec<PathBuf>,
495    ) {
496        self.setup.extend(setup_of(text));
497        self.bibliographies.extend(bibliographies_of(text, folder));
498        for line in text.lines() {
499            let code = line.split('%').next().unwrap_or("");
500            self.graphicspath.extend(graphicspath_of(code));
501        }
502        if let Some(style) = text.lines().find_map(|l| {
503            let at = l.find("\\bibliographystyle{")?;
504            let rest = &l[at + "\\bibliographystyle{".len()..];
505            rest.split('}').next()
506        }) {
507            self.style = style.trim().to_string();
508        }
509        for key in cited_in(text) {
510            if !self.cited.contains(&key) {
511                self.cited.push(key);
512            }
513        }
514        let mut current = String::new();
515        let lines: Vec<&str> = text.lines().collect();
516        let mut index = 0;
517        while index < lines.len() {
518            let line = lines[index];
519            let code = line.split('%').next().unwrap_or("");
520            read_macro(code, self);
521            let trimmed = code.trim_start();
522            // Anywhere on the line, as LaTeX and the word count read them.
523            for (_, name, _) in crate::paper::inputs(crate::paper::uncommented(line)) {
524                let file = crate::paper::resolve(folder, name);
525                self.walk(&file, folder, counters, stack);
526            }
527            if trimmed.starts_with("\\appendix") {
528                counters.appendix = true;
529                counters.sections = [0; 4];
530            }
531            for (name, level) in [
532                ("chapter", 0),
533                ("section", 1),
534                ("subsection", 2),
535                ("subsubsection", 3),
536            ] {
537                if trimmed.starts_with(&format!("\\{name}{{")) {
538                    current = counters.heading(level);
539                }
540            }
541            if let Some(name) =
542                word_after(trimmed, "\\begin").filter(|_| trimmed.starts_with("\\begin"))
543            {
544                match name {
545                    "figure" | "figure*" => {
546                        counters.figures += 1;
547                        current = counters.figures.to_string();
548                    }
549                    "table" | "table*" => {
550                        counters.tables += 1;
551                        current = counters.tables.to_string();
552                    }
553                    _ if is_math(name) => {
554                        let close = format!("\\end{{{name}}}");
555                        let last = (index..lines.len()).find(|n| lines[*n].contains(&close));
556                        if let Some(last) = last {
557                            let source = lines[index..=last].join("\n");
558                            self.number_equation(&source, !name.ends_with('*'), counters);
559                            index = last + 1;
560                            continue;
561                        }
562                    }
563                    _ => {
564                        if let Some(theorem) = self.theorem(name).filter(|t| t.numbered) {
565                            let count = counters.theorems.entry(theorem.counter).or_default();
566                            *count += 1;
567                            current = count.to_string();
568                        }
569                    }
570                }
571            }
572            if let Some(label) = word_after(code, "\\label") {
573                self.labels.insert(label.to_string(), current.clone());
574            }
575            index += 1;
576        }
577    }
578
579    /// Numbers a display, and gives each `\label` in it the number of its own row.
580    fn number_equation(&mut self, source: &str, numbered: bool, counters: &mut Counters) {
581        let numbers = equation_rows(source, numbered, &mut counters.equations);
582        let rows = display_rows(source);
583        for (row, number) in rows.iter().zip(numbers) {
584            if let (Some(label), Some(number)) = (word_after(row, "\\label"), number) {
585                self.labels.insert(label.to_string(), number);
586            }
587        }
588    }
589}
590
591/// Environments that are one display of math.
592pub fn is_math(name: &str) -> bool {
593    let plain = name.trim_end_matches('*');
594    [
595        "equation",
596        "align",
597        "gather",
598        "multline",
599        "displaymath",
600        "eqnarray",
601        "flalign",
602    ]
603    .contains(&plain)
604}
605
606/// One entry of a bibliography.
607#[derive(Clone, Debug, PartialEq, Eq)]
608pub struct BibEntry {
609    pub key: String,
610    pub title: String,
611    pub authors: String,
612    pub year: String,
613}
614
615impl BibEntry {
616    /// `Knuth, Donald (1984). The TeXbook.`, as a references list reads.
617    pub fn reference(&self) -> String {
618        let authors = self.authors.replace(" and ", "; ");
619        let year = if self.year.is_empty() {
620            String::new()
621        } else {
622            format!(" ({})", self.year)
623        };
624        let title = if self.title.is_empty() {
625            String::new()
626        } else {
627            format!(" {}.", self.title.trim_end_matches('.'))
628        };
629        let lead = if authors.is_empty() {
630            self.key.clone()
631        } else {
632            authors
633        };
634        format!("{lead}{year}.{title}")
635    }
636
637    /// `Knuth 1984`, as a citation reads on the page.
638    pub fn short(&self) -> String {
639        let names: Vec<&str> = self.authors.split(" and ").collect();
640        let surname = names
641            .first()
642            .map(|name| surname_of(name))
643            .filter(|name| !name.is_empty())
644            .unwrap_or_else(|| self.key.clone());
645        // Two authors read as both, as natbib prints them; three or more as et al.
646        let more = match names.len() {
647            2 => format!(" and {}", surname_of(names[1])),
648            n if n > 2 => " et al.".to_string(),
649            _ => String::new(),
650        };
651        format!("{surname}{more} {}", self.year).trim().to_string()
652    }
653}
654
655/// What BibTeX takes as a name's family part: all before a comma (`van der Berg, Jan`), or the
656/// last word, where a braced group counts as one (`{World Health Organization}`), without the
657/// braces that only kept it whole.
658fn surname_of(name: &str) -> String {
659    let name = name.trim();
660    let family = if let Some((family, _)) = split_outside_braces(name, ',') {
661        family.trim().to_string()
662    } else if name.ends_with('}') {
663        // The braced group at the end, whole.
664        let mut depth = 0;
665        let mut start = 0;
666        for (at, c) in name.char_indices().rev() {
667            match c {
668                '}' => depth += 1,
669                '{' => {
670                    depth -= 1;
671                    if depth == 0 {
672                        start = at;
673                        break;
674                    }
675                }
676                _ => {}
677            }
678        }
679        name[start..].to_string()
680    } else {
681        name.split_whitespace()
682            .next_back()
683            .unwrap_or(name)
684            .to_string()
685    };
686    family.replace(['{', '}'], "")
687}
688
689/// Splits at the first `separator` that isn't inside braces.
690fn split_outside_braces(text: &str, separator: char) -> Option<(&str, &str)> {
691    let mut depth = 0;
692    for (at, c) in text.char_indices() {
693        match c {
694            '{' => depth += 1,
695            '}' => depth -= 1,
696            _ if c == separator && depth == 0 => return Some((&text[..at], &text[at + 1..])),
697            _ => {}
698        }
699    }
700    None
701}
702
703/// The keys a text cites, in order, each once.
704fn cited_in(text: &str) -> Vec<String> {
705    let mut keys = Vec::new();
706    for line in text.lines() {
707        let code = line.split('%').next().unwrap_or("");
708        let mut rest = code;
709        while let Some(at) = rest.find('\\') {
710            let after = &rest[at + 1..];
711            let name: String = after
712                .chars()
713                .take_while(char::is_ascii_alphabetic)
714                .collect();
715            rest = &after[name.len()..];
716            if !CITES.contains(&name.as_str()) {
717                continue;
718            }
719            // Past any options, as in `\\citep[see][p.~3]{key}`.
720            while rest.starts_with('[') {
721                rest = rest.find(']').map_or("", |end| &rest[end + 1..]);
722            }
723            let Some(inner) = rest.strip_prefix('{').and_then(|r| r.split_once('}')) else {
724                continue;
725            };
726            for key in inner.0.split(',').map(str::trim).filter(|k| !k.is_empty()) {
727                if !keys.iter().any(|k| k == key) {
728                    keys.push(key.to_string());
729                }
730            }
731            rest = inner.1;
732        }
733    }
734    keys
735}
736
737/// The `.bib` files a text names, as paths from `folder`: `\\bibliography{a,b}`, or
738/// biblatex's `\\addbibresource[options]{a.bib}`.
739fn bibliographies_of(text: &str, folder: &Path) -> Vec<PathBuf> {
740    let mut found = Vec::new();
741    for line in text.lines() {
742        let code = crate::paper::uncommented(line);
743        for command in ["\\bibliography", "\\addbibresource"] {
744            let mut rest = code;
745            while let Some(at) = rest.find(command) {
746                let mut after = &rest[at + command.len()..];
747                rest = after;
748                // Not `\\bibliographystyle`.
749                if after.starts_with(|c: char| c.is_ascii_alphabetic()) {
750                    continue;
751                }
752                after = after.trim_start();
753                while let Some(options) = after.strip_prefix('[') {
754                    after = options
755                        .find(']')
756                        .map_or("", |end| options[end + 1..].trim_start());
757                }
758                let Some(inner) = after.strip_prefix('{') else {
759                    continue;
760                };
761                let Some(end) = inner.find('}') else {
762                    break;
763                };
764                let after = inner;
765                for name in after[..end]
766                    .split(',')
767                    .map(str::trim)
768                    .filter(|n| !n.is_empty())
769                {
770                    let file = if name.ends_with(".bib") {
771                        name.to_string()
772                    } else {
773                        format!("{name}.bib")
774                    };
775                    found.push(folder.join(file));
776                }
777                rest = &after[end..];
778            }
779        }
780    }
781    found
782}
783
784/// TeX's accents in a `.bib` field as the letters they make: `Cand\\`es` reads "Candès".
785fn detex(text: &str) -> String {
786    const ACCENTS: [(char, &str, &str); 7] = [
787        ('`', "aeiouAEIOU", "àèìòùÀÈÌÒÙ"),
788        ('\'', "aeiouyAEIOUY", "áéíóúýÁÉÍÓÚÝ"),
789        ('^', "aeiouAEIOU", "âêîôûÂÊÎÔÛ"),
790        ('"', "aeiouyAEIOU", "äëïöüÿÄËÏÖÜ"),
791        ('~', "anoANO", "ãñõÃÑÕ"),
792        ('c', "cC", "çÇ"),
793        ('v', "csznrCSZNR", "čšžňřČŠŽŇŘ"),
794    ];
795    let chars: Vec<char> = text.chars().collect();
796    let mut out = String::with_capacity(text.len());
797    let mut i = 0;
798    while i < chars.len() {
799        if chars[i] == '\\'
800            && let Some(&mark) = chars.get(i + 1)
801        {
802            // `\\c c` and `\\v s` need a space; the others don't.
803            let mut at = i + 2;
804            if mark.is_ascii_alphabetic() && chars.get(at) == Some(&' ') {
805                at += 1;
806            }
807            if let Some((_, from, to)) = ACCENTS.iter().find(|(m, _, _)| *m == mark)
808                && let Some(&letter) = chars.get(at)
809                && let Some(n) = from.chars().position(|c| c == letter)
810                && let Some(made) = to.chars().nth(n)
811            {
812                out.push(made);
813                i = at + 1;
814                continue;
815            }
816            let word: String = chars[i + 1..]
817                .iter()
818                .take_while(|c| c.is_ascii_alphabetic())
819                .collect();
820            let special = match word.as_str() {
821                "ss" => Some("ß"),
822                "o" => Some("ø"),
823                "O" => Some("Ø"),
824                "aa" => Some("å"),
825                "AA" => Some("Å"),
826                "l" => Some("ł"),
827                "L" => Some("Ł"),
828                "ae" => Some("æ"),
829                _ => None,
830            };
831            if let Some(special) = special {
832                out.push_str(special);
833                i += 1 + word.len();
834                if chars.get(i) == Some(&' ') {
835                    i += 1;
836                }
837                continue;
838            }
839        }
840        out.push(chars[i]);
841        i += 1;
842    }
843    out
844}
845
846fn field(body: &str, name: &str) -> String {
847    let lower = body.to_lowercase();
848    let mut from = 0;
849    while let Some(found) = lower[from..].find(name) {
850        let at = from + found;
851        let before = lower[..at].chars().next_back();
852        let rest = lower[at + name.len()..].trim_start();
853        if before.is_none_or(|c| !c.is_alphanumeric()) && rest.starts_with('=') {
854            let value = body[at + name.len()..].trim_start()[1..].trim_start();
855            let open = value.chars().next().unwrap_or(' ');
856            let text = match open {
857                '{' => braced(value, 0).map_or("", |(a, b)| &value[a..b]),
858                '"' => value[1..].split('"').next().unwrap_or(""),
859                _ => value.split([',', '\n']).next().unwrap_or(""),
860            };
861            return detex(&text.replace(['{', '}'], "")).trim().to_string();
862        }
863        from = at + name.len();
864    }
865    String::new()
866}
867
868/// Reads a `.bib` file. Entries can start on one line and take their brace on the next.
869pub fn bib_entries(text: &str) -> Vec<BibEntry> {
870    let mut entries = Vec::new();
871    let mut rest = text;
872    while let Some(at) = rest.find('@') {
873        rest = &rest[at + 1..];
874        let Some(open) = rest.find('{') else {
875            break;
876        };
877        let kind = rest[..open].trim().to_lowercase();
878        let Some((from, to)) = braced(rest, open) else {
879            break;
880        };
881        let body = &rest[from..to];
882        if !["comment", "string", "preamble"].contains(&kind.as_str()) {
883            let (key, fields) = body.split_once(',').unwrap_or((body, ""));
884            if !key.trim().is_empty() {
885                entries.push(BibEntry {
886                    key: key.trim().to_string(),
887                    title: field(fields, "title"),
888                    authors: field(fields, "author"),
889                    year: field(fields, "year"),
890                });
891            }
892        }
893        rest = &rest[to + 1..];
894    }
895    entries
896}
897
898/// The engines Cellix knows, best first, as commands that take the file last. Each writes the
899/// SyncTeX map that links the PDF back to the source.
900pub const ENGINES: [&[&str]; 3] = [
901    &["tectonic", "-X", "compile", "--synctex", "--keep-logs"],
902    &[
903        "latexmk",
904        "-pdf",
905        "-synctex=1",
906        "-interaction=nonstopmode",
907        "-file-line-error",
908    ],
909    &[
910        "pdflatex",
911        "-synctex=1",
912        "-interaction=nonstopmode",
913        "-file-line-error",
914    ],
915];
916
917/// The commands that cite, and the ones that refer to a label.
918pub const CITES: [&str; 10] = [
919    "cite",
920    "citep",
921    "citet",
922    "citeauthor",
923    "citeyear",
924    "autocite",
925    "parencite",
926    "textcite",
927    "footcite",
928    "nocite",
929];
930pub const REFS: [&str; 6] = ["ref", "eqref", "cref", "Cref", "autoref", "pageref"];
931
932/// What a pdflatex pass left to do, from its `.aux` file: BibTeX or Biber to run, if either,
933/// and how many more passes it takes for citations and references to settle. Tectonic and
934/// latexmk do this themselves. Biblatex with Biber writes no `\bibdata`, only its own
935/// `\abx@aux` lines and a `.bcf` file, which `bcf` says is there.
936pub fn more_passes(aux: &str, bcf: bool) -> (Option<Bibliography>, usize) {
937    let tool = if aux.contains("\\bibdata") {
938        Some(Bibliography::Bibtex)
939    } else if aux.contains("\\abx@aux") || bcf {
940        Some(Bibliography::Biber)
941    } else {
942        None
943    };
944    let passes = if tool.is_some() {
945        2
946    } else {
947        usize::from(aux.contains("\\newlabel") || aux.contains("\\citation"))
948    };
949    (tool, passes)
950}
951
952/// What reads a paper's `.bib` files between passes.
953#[derive(Clone, Copy, Debug, PartialEq, Eq)]
954pub enum Bibliography {
955    Bibtex,
956    Biber,
957}
958
959impl Bibliography {
960    pub fn program(self) -> &'static str {
961        match self {
962            Self::Bibtex => "bibtex",
963            Self::Biber => "biber",
964        }
965    }
966
967    pub fn name(self) -> &'static str {
968        match self {
969            Self::Bibtex => "BibTeX",
970            Self::Biber => "Biber",
971        }
972    }
973
974    /// Why a run failed, from its exit code and what it said, or None if it didn't. BibTeX
975    /// exits 1 for warnings, which LaTeX reports again, and 2 or more for errors, such as a
976    /// `.bib` file it can't find.
977    pub fn failure(self, code: Option<i32>, said: &str) -> Option<String> {
978        let failed = match self {
979            Self::Bibtex => code.is_none_or(|c| c >= 2),
980            Self::Biber => code != Some(0),
981        };
982        if !failed {
983            return None;
984        }
985        let chatter = [
986            "This is ",
987            "The top-level auxiliary file",
988            "The style file",
989            "Database file #",
990            "Warning--",
991            "INFO - ",
992            "WARN - ",
993        ];
994        let lines = || said.lines().map(str::trim).filter(|l| !l.is_empty());
995        let error = lines().find_map(|l| l.strip_prefix("ERROR - "));
996        let first = lines().find(|l| !chatter.iter().any(|c| l.starts_with(c)));
997        Some(
998            error
999                .or(first)
1000                .unwrap_or("it ended without saying why")
1001                .to_string(),
1002        )
1003    }
1004}
1005
1006/// Whether a pass's log says references or citations may still be wrong. With `\include`,
1007/// labels live in each chapter's own `.aux`, so only the log tells.
1008pub fn asks_rerun(log: &str) -> bool {
1009    let log = unwrapped(log);
1010    [
1011        "Rerun to get",
1012        "Label(s) may have changed",
1013        "Please rerun LaTeX",
1014    ]
1015    .iter()
1016    .any(|asks| log.contains(asks))
1017}
1018
1019/// Whether a key to a citation or a label is being typed: the text of a line up to the caret
1020/// ends inside `\cite{` or `\ref{`. Gives which, and the char where the key being typed starts,
1021/// after any keys before it.
1022pub fn reference_slot(before: &str) -> Option<(Slot, usize)> {
1023    let chars: Vec<char> = before.chars().collect();
1024    let open = chars.iter().rposition(|c| *c == '{')?;
1025    if chars[open + 1..]
1026        .iter()
1027        .any(|c| matches!(c, '}' | '\\' | '{'))
1028    {
1029        return None;
1030    }
1031    // Past options, as in `\citep[see][p.~3]{`.
1032    let mut end = open;
1033    while end > 0 && chars[end - 1] == ']' {
1034        end = chars[..end].iter().rposition(|c| *c == '[')?;
1035    }
1036    let name_start = chars[..end]
1037        .iter()
1038        .rposition(|c| !(c.is_ascii_alphabetic() || *c == '*'))?;
1039    if chars[name_start] != '\\' {
1040        return None;
1041    }
1042    let name: String = chars[name_start + 1..end].iter().collect();
1043    let name = name.trim_end_matches('*');
1044    let slot = if CITES.contains(&name) {
1045        Slot::Cite
1046    } else if REFS.contains(&name) {
1047        Slot::Ref
1048    } else {
1049        return None;
1050    };
1051    let key = chars[open + 1..]
1052        .iter()
1053        .rposition(|c| *c == ',')
1054        .map_or(open + 1, |comma| open + 2 + comma);
1055    let key = key
1056        + chars[key..]
1057            .iter()
1058            .take_while(|c| c.is_whitespace())
1059            .count();
1060    Some((slot, key))
1061}
1062
1063/// What goes inside a reference's braces.
1064#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1065pub enum Slot {
1066    Cite,
1067    Ref,
1068}
1069
1070/// Finds `file:line: message` in a build's output. Every engine's errors reduce to this.
1071pub fn problems(output: &str, folder: &Path) -> Vec<Problem> {
1072    let mut found = Vec::new();
1073    for line in output.lines() {
1074        let line = line.strip_prefix("error: ").unwrap_or(line);
1075        let mut parts = line.splitn(3, ':');
1076        let (Some(file), Some(number), Some(message)) = (parts.next(), parts.next(), parts.next())
1077        else {
1078            continue;
1079        };
1080        let file = file.trim().trim_start_matches("./");
1081        let Ok(number) = number.trim().parse() else {
1082            continue;
1083        };
1084        // Tectonic leaves the extension off an `\input` file.
1085        let known = [".tex", ".bib", ".sty", ".cls"];
1086        let name = if known.iter().any(|e| file.ends_with(e)) {
1087            file.to_string()
1088        } else if !file.is_empty() && !file.contains(' ') {
1089            format!("{file}.tex")
1090        } else {
1091            continue;
1092        };
1093        found.push(Problem {
1094            file: folder.join(name),
1095            line: number,
1096            message: message.trim().to_string(),
1097        });
1098    }
1099    found.dedup();
1100    found
1101}
1102
1103/// A TeX log with the lines TeX broke at 79 characters put back together.
1104fn unwrapped(log: &str) -> String {
1105    let mut out = String::new();
1106    for line in log.lines() {
1107        out.push_str(line);
1108        if line.len() != 79 && line.chars().count() != 79 {
1109            out.push('\n');
1110        }
1111    }
1112    out
1113}
1114
1115/// What a build's warnings come from: the log, which holds the last pass alone, or what the
1116/// engine said when it left no log.
1117fn last_pass(said: &str, log: &str) -> String {
1118    unwrapped(if log.trim().is_empty() { said } else { log })
1119}
1120
1121/// The file a log names after `(`, if it's one of the paper's. Tectonic leaves `.tex` off.
1122fn opened(name: &str, folder: &Path) -> Option<PathBuf> {
1123    if name.is_empty() {
1124        return None;
1125    }
1126    let path: PathBuf = folder
1127        .join(name)
1128        .components()
1129        .filter(|c| *c != std::path::Component::CurDir)
1130        .collect();
1131    if path.is_file() {
1132        return Some(path);
1133    }
1134    let tex = PathBuf::from(format!("{}.tex", path.display()));
1135    tex.is_file().then_some(tex)
1136}
1137
1138/// Follows the files TeX opens, `(./sections/a.tex` … `)`, through one line of its log.
1139fn follow(line: &str, folder: &Path, open: &mut Vec<Option<PathBuf>>) {
1140    let mut rest = line;
1141    while let Some(at) = rest.find(['(', ')']) {
1142        if rest[at..].starts_with(')') {
1143            open.pop();
1144            rest = &rest[at + 1..];
1145            continue;
1146        }
1147        let after = &rest[at + 1..];
1148        let (name, next) = match after.strip_prefix('"') {
1149            Some(quoted) => {
1150                let end = quoted.find('"').unwrap_or(quoted.len());
1151                (&quoted[..end], &quoted[(end + 1).min(quoted.len())..])
1152            }
1153            None => {
1154                let end = after
1155                    .find(|c: char| c.is_whitespace() || matches!(c, '(' | ')' | '[' | '{'))
1156                    .unwrap_or(after.len());
1157                (&after[..end], &after[end..])
1158            }
1159        };
1160        open.push(opened(name, folder));
1161        rest = next;
1162    }
1163}
1164
1165/// Where a build's warnings point, as problems a reader can walk with F8: tectonic's own
1166/// `warning: file:line: …`, and LaTeX's undefined references and citations, at their line in
1167/// the file TeX was reading then. Each place is given once, from the last pass.
1168pub fn warning_places(said: &str, log: &str, folder: &Path, root: &Path) -> Vec<Problem> {
1169    let mut found = Vec::new();
1170    for line in said.lines() {
1171        if let Some(rest) = line.trim().strip_prefix("warning: ") {
1172            found.extend(problems(rest, folder));
1173        }
1174    }
1175    let mut open: Vec<Option<PathBuf>> = Vec::new();
1176    for line in last_pass(said, log).lines() {
1177        let reading = open.iter().rev().find_map(Option::as_ref);
1178        if line.contains("undefined on input line")
1179            && let Some(at) = line.rfind("input line ")
1180        {
1181            let number: String = line[at + "input line ".len()..]
1182                .chars()
1183                .take_while(char::is_ascii_digit)
1184                .collect();
1185            if let Ok(number) = number.parse() {
1186                let message = line
1187                    .trim()
1188                    .trim_start_matches("LaTeX Warning: ")
1189                    .trim_start_matches("Package natbib Warning: ")
1190                    .to_string();
1191                found.push(Problem {
1192                    file: reading.map_or_else(|| root.to_path_buf(), Clone::clone),
1193                    line: number,
1194                    message,
1195                });
1196            }
1197        }
1198        follow(line, folder, &mut open);
1199    }
1200    let mut seen = Vec::new();
1201    found.retain(|p| {
1202        let new = !seen.contains(p);
1203        if new {
1204            seen.push(p.clone());
1205        }
1206        new
1207    });
1208    found
1209}
1210
1211/// What a build warned about, each once: a build that "worked" can still have unresolved
1212/// citations and references. Only the last pass counts, since latexmk reports every one.
1213pub fn warnings(said: &str, log: &str) -> Vec<String> {
1214    let marks = ["warning:", "Warning:", "undefined on input"];
1215    // What only sums up the warnings already counted.
1216    let summaries = [
1217        "There were undefined",
1218        "There were multiply-defined",
1219        "warnings were issued by the TeX engine",
1220    ];
1221    let own = said
1222        .lines()
1223        .filter(|line| line.trim_start().starts_with("warning:"));
1224    let text = last_pass(said, log);
1225    let mut found: Vec<String> = Vec::new();
1226    for line in text.lines().chain(own) {
1227        let line = line.trim();
1228        let warned =
1229            marks.iter().any(|m| line.contains(m)) && !summaries.iter().any(|s| line.contains(s));
1230        if warned && !found.iter().any(|f| f == line) {
1231            found.push(line.to_string());
1232        }
1233    }
1234    found
1235}
1236
1237fn escape(text: &str) -> String {
1238    let mut out = String::new();
1239    for c in text.chars() {
1240        match c {
1241            '\\' => out.push_str("\\textbackslash{}"),
1242            '&' | '%' | '$' | '#' | '_' | '{' | '}' => {
1243                out.push('\\');
1244                out.push(c);
1245            }
1246            '~' => out.push_str("\\textasciitilde{}"),
1247            '^' => out.push_str("\\textasciicircum{}"),
1248            other => out.push(other),
1249        }
1250    }
1251    out
1252}
1253
1254/// Changes a `tabular`, given its lines from `\begin` to `\end`, and the row and column the
1255/// caret is in, counting rows of cells. None means the table is gone.
1256pub fn edit_tabular(text: &str, row: usize, column: usize, edit: Edit) -> Option<String> {
1257    let mut lines: Vec<String> = text.lines().map(str::to_string).collect();
1258    let is_row = |line: &String| line.contains("\\\\");
1259    let rows: Vec<usize> = (0..lines.len()).filter(|i| is_row(&lines[*i])).collect();
1260    let at = *rows.get(row.min(rows.len().checked_sub(1)?))?;
1261    let cells = |line: &str| line.split("\\\\").next().unwrap_or("").matches('&').count() + 1;
1262    let columns = cells(&lines[at]);
1263    let blank = || format!("    {} \\\\", vec![""; columns].join(" & "));
1264    let spec = |lines: &mut [String], change: &dyn Fn(&mut Vec<char>)| {
1265        let line = &lines[0];
1266        let open = line.rfind('{').unwrap_or(0) + 1;
1267        let close = line.rfind('}').unwrap_or(line.len());
1268        let mut letters: Vec<char> = line[open..close].chars().collect();
1269        change(&mut letters);
1270        let letters: String = letters.into_iter().collect();
1271        lines[0] = format!("{}{letters}{}", &line[..open], &line[close..]);
1272    };
1273    match edit {
1274        Edit::RowAbove => lines.insert(at, blank()),
1275        Edit::RowBelow => lines.insert(at + 1, blank()),
1276        Edit::DeleteRow if rows.len() == 1 => return None,
1277        Edit::DeleteRow => {
1278            lines.remove(at);
1279        }
1280        Edit::ColumnLeft | Edit::ColumnRight | Edit::DeleteColumn => {
1281            if edit == Edit::DeleteColumn && columns == 1 {
1282                return None;
1283            }
1284            let right = usize::from(edit == Edit::ColumnRight);
1285            for index in rows {
1286                let line = lines[index].clone();
1287                let (body, _) = line.split_once("\\\\").unwrap_or((&line, ""));
1288                let mut parts: Vec<String> =
1289                    body.split('&').map(|c| c.trim().to_string()).collect();
1290                if edit == Edit::DeleteColumn {
1291                    if column < parts.len() {
1292                        parts.remove(column);
1293                    }
1294                } else {
1295                    parts.insert((column + right).min(parts.len()), String::new());
1296                }
1297                lines[index] = format!("    {} \\\\", parts.join(" & "));
1298            }
1299            spec(&mut lines, &|letters| {
1300                let spot = (column + right).min(letters.len());
1301                if edit == Edit::DeleteColumn {
1302                    if column < letters.len() {
1303                        letters.remove(column);
1304                    }
1305                } else {
1306                    letters.insert(spot, 'l');
1307                }
1308            });
1309        }
1310        Edit::Align(align) => {
1311            let letter = match align {
1312                Align::Right => 'r',
1313                Align::Centre => 'c',
1314                _ => 'l',
1315            };
1316            spec(&mut lines, &|letters| {
1317                if let Some(slot) = letters.get_mut(column) {
1318                    *slot = letter;
1319                }
1320            });
1321        }
1322        Edit::Delete => return None,
1323    }
1324    Some(lines.join("\n"))
1325}
1326
1327/// A new table, ready to fill in.
1328pub fn new_table(rows: usize, columns: usize) -> String {
1329    let columns = columns.max(1);
1330    let row = |text: &dyn Fn(usize) -> String| {
1331        let cells: Vec<String> = (0..columns).map(text).collect();
1332        format!("    {} \\\\\n", cells.join(" & "))
1333    };
1334    let mut out = format!(
1335        "\\begin{{table}}[h]\n  \\centering\n  \\begin{{tabular}}{{{}}}\n    \\hline\n",
1336        "l".repeat(columns)
1337    );
1338    out.push_str(&row(&|c| format!("Column {}", c + 1)));
1339    out.push_str("    \\hline\n");
1340    for _ in 1..rows.max(2) {
1341        out.push_str(&row(&|_| String::new()));
1342    }
1343    out.push_str("    \\hline\n  \\end{tabular}\n  \\caption{}\n\\end{table}");
1344    out
1345}
1346
1347/// Environments only LaTeX can draw, each built on its own for the page.
1348pub const DRAWN: [&str; 7] = [
1349    "tikzpicture",
1350    "tikzcd",
1351    "forest",
1352    "circuitikz",
1353    "algorithm",
1354    "algorithmic",
1355    "algorithm2e",
1356];
1357
1358/// What starts a line of setup a piece built alone needs too.
1359const SETUP: [&str; 10] = [
1360    "\\usepackage",
1361    "\\usetikzlibrary",
1362    "\\usepgfplotslibrary",
1363    "\\tikzset",
1364    "\\tikzcdset",
1365    "\\pgfplotsset",
1366    "\\newcommand",
1367    "\\renewcommand",
1368    "\\DeclareMathOperator",
1369    "\\def",
1370];
1371
1372/// Packages that shape a whole document, which a small picture must not have.
1373const PAGE_PACKAGES: [&str; 5] = ["geometry", "hyperref", "cleveref", "fancyhdr", "natbib"];
1374
1375/// The lines of a file's preamble worth repeating, with commands that run over several lines
1376/// kept whole.
1377fn setup_of(text: &str) -> Vec<String> {
1378    let mut out = Vec::new();
1379    let mut lines = text.lines();
1380    while let Some(line) = lines.next() {
1381        if line.contains("\\begin{document}") {
1382            break;
1383        }
1384        let code = line.split('%').next().unwrap_or("").trim();
1385        let wanted = SETUP.iter().any(|start| code.starts_with(start))
1386            && !PAGE_PACKAGES.iter().any(|name| code.contains(name));
1387        if !wanted {
1388            continue;
1389        }
1390        let mut whole = code.to_string();
1391        let open = |text: &str| text.matches('{').count() as i64 - text.matches('}').count() as i64;
1392        while open(&whole) > 0 {
1393            let Some(next) = lines.next() else { break };
1394            whole.push('\n');
1395            whole.push_str(next.split('%').next().unwrap_or(""));
1396        }
1397        out.push(whole);
1398    }
1399    out
1400}
1401
1402/// A piece of a paper as a file of its own, cut to its size.
1403pub fn standalone(setup: &[String], body: &str) -> String {
1404    let mut out = String::from(
1405        "\\documentclass[border=2pt,varwidth=16cm]{standalone}\n\\usepackage{amsmath,amssymb}\n\\usepackage{tikz}\n",
1406    );
1407    if body.contains("tikzcd") {
1408        out.push_str("\\usepackage{tikz-cd}\n");
1409    }
1410    if body.contains("\\begin{algorithmic}") {
1411        out.push_str("\\usepackage{algpseudocode}\n");
1412    }
1413    if body.contains("forest") {
1414        out.push_str("\\usepackage{forest}\n");
1415    }
1416    if body.contains("circuitikz") {
1417        out.push_str("\\usepackage{circuitikz}\n");
1418    }
1419    for line in setup {
1420        out.push_str(line);
1421        out.push('\n');
1422    }
1423    out.push_str("\\begin{document}\n");
1424    out.push_str(body);
1425    out.push_str("\n\\end{document}\n");
1426    out
1427}
1428
1429/// Where a cell's text starts in a `tabular`'s lines, in chars.
1430pub fn tabular_cell_offset(text: &str, row: usize, column: usize) -> usize {
1431    let is_row = |line: &&str| line.contains("\\\\");
1432    let mut at = 0;
1433    let mut seen = 0;
1434    for line in text.split('\n') {
1435        if is_row(&line) {
1436            if seen == row {
1437                let chars: Vec<char> = line.chars().collect();
1438                let mut start = 0;
1439                let mut bars = 0;
1440                while bars < column && start < chars.len() {
1441                    bars += usize::from(chars[start] == '&');
1442                    start += 1;
1443                }
1444                let spaces = chars[start..].iter().take_while(|c| **c == ' ').count();
1445                let stops = matches!(chars.get(start + spaces), Some('&' | '\\'));
1446                return at + start + spaces.min(if stops { 1 } else { spaces });
1447            }
1448            seen += 1;
1449        }
1450        at += line.chars().count() + 1;
1451    }
1452    0
1453}
1454
1455const HEADER: &str = "\\documentclass[11pt]{article}\n\\usepackage{amsmath,amssymb,graphicx,hyperref}\n\\usepackage[margin=1in]{geometry}\n";
1456
1457/// The preamble an exported document needs. Packages past the core load only when the body uses
1458/// them, so a small TeX, with no TikZ or `ulem`, still builds a document that doesn't.
1459fn header_for(body: &str) -> String {
1460    let mut header = String::from(HEADER);
1461    // A code block shows commands without running them, so what it holds needs no package.
1462    let mut run = String::new();
1463    let mut rest = body;
1464    while let Some(at) = rest.find("\\begin{verbatim}") {
1465        run.push_str(&rest[..at]);
1466        rest = rest[at..]
1467            .find("\\end{verbatim}")
1468            .map_or("", |end| &rest[at + end..]);
1469    }
1470    run.push_str(rest);
1471    let body = run.as_str();
1472    let wanted = [
1473        ("\\toprule", "booktabs"),
1474        ("\\textcolor", "xcolor"),
1475        ("\\color", "xcolor"),
1476        ("\\tikz", "tikz"),
1477        ("\\begin{tikzpicture}", "tikz"),
1478        ("\\begin{tikzcd}", "tikz-cd"),
1479    ];
1480    let mut loaded: Vec<&str> = Vec::new();
1481    for (used, package) in wanted {
1482        if body.contains(used) && !loaded.contains(&package) {
1483            loaded.push(package);
1484        }
1485    }
1486    if !loaded.is_empty() {
1487        header.push_str(&format!("\\usepackage{{{}}}\n", loaded.join(",")));
1488    }
1489    if body.contains("\\sout") {
1490        header.push_str("\\usepackage[normalem]{ulem}\n");
1491    }
1492    header.push_str("\\begin{document}\n");
1493    header
1494}
1495
1496/// A tag from Markdown's marks, as LaTeX.
1497fn tag_in_latex(tag: &str) -> Option<&'static str> {
1498    Some(match tag {
1499        "<sub>" => "\\textsubscript{",
1500        "<sup>" => "\\textsuperscript{",
1501        "<mark>" => "\\colorbox{yellow}{",
1502        "<ins>" => "\\textcolor{green!50!black}{",
1503        "<del>" => "\\textcolor{red}{\\sout{",
1504        "</del>" => "}}",
1505        "</sub>" | "</sup>" | "</mark>" | "</ins>" => "}",
1506        _ => return None,
1507    })
1508}
1509
1510/// What each footnote says, by its name.
1511fn footnote_texts(markdown: &str) -> HashMap<String, String> {
1512    let definitions = markdown.lines().filter_map(|line| {
1513        let rest = line.strip_prefix("[^")?;
1514        let (id, text) = rest.split_once("]:")?;
1515        Some((id.to_string(), text.trim().to_string()))
1516    });
1517    definitions.collect()
1518}
1519
1520/// Markdown as a LaTeX document, with diagrams as source.
1521pub fn from_markdown(markdown: &str) -> String {
1522    from_markdown_with(markdown, &mut |_, _| None)
1523}
1524
1525/// Markdown as a LaTeX document. Math goes through untouched, which is the point. `picture`
1526/// draws a diagram to a file and names it, since LaTeX can't read one.
1527pub fn from_markdown_with(
1528    markdown: &str,
1529    picture: &mut dyn FnMut(Kind, &str) -> Option<String>,
1530) -> String {
1531    let markdown = figure::rewrite_sized(markdown, |image, width| {
1532        let share = f64::from(width) / 100.0;
1533        format!("![{}]({} \"{share:.2}\")", image.alt, image.path)
1534    });
1535    let options = Options::ENABLE_TABLES
1536        | Options::ENABLE_TASKLISTS
1537        | Options::ENABLE_STRIKETHROUGH
1538        | Options::ENABLE_FOOTNOTES
1539        | Options::ENABLE_GFM
1540        | Options::ENABLE_MATH;
1541    let (front, markdown) = crate::front::split(&markdown);
1542    // A footnote goes in where it's cited, so its text is found first.
1543    let notes = footnote_texts(markdown);
1544    let markdown = crate::html::marked(markdown);
1545    let mut out = String::new();
1546    if !front.is_empty() {
1547        let field = |value: &Option<String>| value.as_deref().map(escape).unwrap_or_default();
1548        out.push_str(&format!(
1549            "\\title{{{}}}\n\\author{{{}}}\n\\date{{{}}}\n\\maketitle\n",
1550            field(&front.title),
1551            field(&front.author),
1552            field(&front.date)
1553        ));
1554    }
1555    let mut verbatim = false;
1556    let mut columns = 0;
1557    let mut cell = 0;
1558    let mut diagram: Option<(Kind, String)> = None;
1559    let mut tikz: Option<(String, String)> = None;
1560    let mut skipping = false;
1561    for event in Parser::new_ext(&markdown, options) {
1562        if let Some((language, source)) = &mut tikz {
1563            match event {
1564                Event::Text(text) => source.push_str(&text),
1565                Event::End(TagEnd::CodeBlock) => {
1566                    let body = fenced_tikz(language, source.trim_end());
1567                    out.push_str(&format!("\n\\begin{{center}}\n{body}\n\\end{{center}}\n"));
1568                    tikz = None;
1569                }
1570                _ => {}
1571            }
1572            continue;
1573        }
1574        if let Some((kind, source)) = &mut diagram {
1575            match event {
1576                Event::Text(text) => source.push_str(&text),
1577                Event::End(TagEnd::CodeBlock) => {
1578                    match picture(*kind, source) {
1579                        Some(file) => out.push_str(&format!(
1580                            "\n\\begin{{center}}\n\\includegraphics[width=\\linewidth]{{{file}}}\n\\end{{center}}\n"
1581                        )),
1582                        None => out.push_str(&format!(
1583                            "\n\\begin{{verbatim}}\n{source}\\end{{verbatim}}\n"
1584                        )),
1585                    }
1586                    diagram = None;
1587                }
1588                _ => {}
1589            }
1590            continue;
1591        }
1592        match event {
1593            Event::Start(Tag::Heading { level, .. }) => out.push_str(match level {
1594                HeadingLevel::H1 => "\n\\section*{",
1595                HeadingLevel::H2 => "\n\\subsection*{",
1596                HeadingLevel::H3 => "\n\\subsubsection*{",
1597                _ => "\n\\paragraph*{",
1598            }),
1599            Event::End(TagEnd::Heading(_)) => out.push_str("}\n"),
1600            Event::Start(Tag::Paragraph) => out.push('\n'),
1601            Event::End(TagEnd::Paragraph) => out.push('\n'),
1602            Event::Start(Tag::Strong) => out.push_str("\\textbf{"),
1603            Event::Start(Tag::Emphasis) => out.push_str("\\emph{"),
1604            Event::Start(Tag::Strikethrough) => out.push_str("\\sout{"),
1605            Event::End(TagEnd::Strong | TagEnd::Emphasis | TagEnd::Strikethrough) => {
1606                out.push('}');
1607            }
1608            Event::InlineHtml(tag) if tag_in_latex(&tag).is_some() => {
1609                out.push_str(tag_in_latex(&tag).unwrap_or_default());
1610            }
1611            Event::FootnoteReference(id) => {
1612                let text = notes.get(id.as_ref()).map_or("", String::as_str);
1613                out.push_str(&format!("\\footnote{{{}}}", escape(text)));
1614            }
1615            Event::Start(Tag::FootnoteDefinition(_)) => skipping = true,
1616            Event::End(TagEnd::FootnoteDefinition) => skipping = false,
1617            _ if skipping => {}
1618            Event::Start(Tag::Link { dest_url, .. }) => {
1619                out.push_str(&format!("\\href{{{}}}{{", escape(&dest_url)));
1620            }
1621            Event::End(TagEnd::Link) => out.push('}'),
1622            Event::Start(Tag::Image {
1623                dest_url, title, ..
1624            }) => {
1625                out.push_str(&format!(
1626                    "\n\\includegraphics[width={title}\\linewidth]{{{dest_url}}}\n\\iffalse "
1627                ));
1628            }
1629            Event::End(TagEnd::Image) => out.push_str("\\fi\n"),
1630            Event::Start(Tag::BlockQuote(_)) => out.push_str("\n\\begin{quote}"),
1631            Event::End(TagEnd::BlockQuote(_)) => out.push_str("\\end{quote}\n"),
1632            Event::Start(Tag::List(None)) => out.push_str("\n\\begin{itemize}\n"),
1633            Event::Start(Tag::List(Some(_))) => out.push_str("\n\\begin{enumerate}\n"),
1634            Event::End(TagEnd::List(false)) => out.push_str("\\end{itemize}\n"),
1635            Event::End(TagEnd::List(true)) => out.push_str("\\end{enumerate}\n"),
1636            Event::Start(Tag::Item) => out.push_str("\\item "),
1637            Event::End(TagEnd::Item) => out.push('\n'),
1638            Event::TaskListMarker(done) => {
1639                out.push_str(if done {
1640                    "[$\\boxtimes$] "
1641                } else {
1642                    "[$\\square$] "
1643                });
1644            }
1645            Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(language)))
1646                if kind_of(&language).is_some() =>
1647            {
1648                diagram = kind_of(&language).map(|kind| (kind, String::new()));
1649            }
1650            Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(language))) if is_tikz(&language) => {
1651                tikz = Some((language.to_string(), String::new()));
1652            }
1653            Event::Start(Tag::CodeBlock(_)) => {
1654                verbatim = true;
1655                out.push_str("\n\\begin{verbatim}\n");
1656            }
1657            Event::End(TagEnd::CodeBlock) => {
1658                verbatim = false;
1659                out.push_str("\\end{verbatim}\n");
1660            }
1661            Event::Start(Tag::Table(aligns)) => {
1662                columns = aligns.len();
1663                let spec: String = aligns
1664                    .iter()
1665                    .map(|a| match a {
1666                        Alignment::Center => 'c',
1667                        Alignment::Right => 'r',
1668                        _ => 'l',
1669                    })
1670                    .collect();
1671                out.push_str(&format!("\n\\begin{{tabular}}{{{spec}}}\n\\toprule\n"));
1672            }
1673            Event::End(TagEnd::Table) => out.push_str("\\bottomrule\n\\end{tabular}\n"),
1674            Event::Start(Tag::TableHead | Tag::TableRow) => cell = 0,
1675            Event::End(TagEnd::TableHead) => out.push_str(" \\\\\n\\midrule\n"),
1676            Event::End(TagEnd::TableRow) => out.push_str(" \\\\\n"),
1677            Event::Start(Tag::TableCell) => {
1678                if cell > 0 && cell < columns {
1679                    out.push_str(" & ");
1680                }
1681                cell += 1;
1682            }
1683            Event::Text(text) if verbatim => out.push_str(&text),
1684            Event::Text(text) => out.push_str(&escape(&text)),
1685            Event::Code(text) => out.push_str(&format!("\\texttt{{{}}}", escape(&text))),
1686            Event::InlineMath(text) => out.push_str(&format!("${text}$")),
1687            Event::DisplayMath(text) => out.push_str(&format!("\n\\[\n{}\n\\]\n", text.trim())),
1688            Event::SoftBreak => out.push('\n'),
1689            Event::HardBreak => out.push_str("\\\\\n"),
1690            Event::Rule => out.push_str("\n\\noindent\\rule{\\linewidth}{0.4pt}\n"),
1691            _ => {}
1692        }
1693    }
1694    out.push_str("\n\\end{document}\n");
1695    header_for(&out) + &out
1696}
1697
1698#[cfg(test)]
1699mod tests {
1700    use super::*;
1701    #[test]
1702    fn a_key_is_being_typed_inside_cite_and_ref() {
1703        assert_eq!(reference_slot("See \\cite{kn"), Some((Slot::Cite, 10)));
1704        assert_eq!(
1705            reference_slot("See \\citep[see][p.~3]{a, kn"),
1706            Some((Slot::Cite, 25))
1707        );
1708        assert_eq!(reference_slot("In \\eqref{"), Some((Slot::Ref, 10)));
1709        assert_eq!(reference_slot("\\textbf{bold"), None);
1710        assert_eq!(reference_slot("\\ref{done} and"), None);
1711    }
1712    #[test]
1713    fn a_paper_reads_the_bibliography_it_names_and_its_accents() {
1714        let dir = tempfile::tempdir().unwrap();
1715        std::fs::create_dir(dir.path().join("bib")).unwrap();
1716        std::fs::write(
1717            dir.path().join("bib/refs.bib"),
1718            "@article{candes2006, author = {Cand\\`es, Emmanuel and Tao, Terence}, year = 2006}\n",
1719        )
1720        .unwrap();
1721        let root = dir.path().join("main.tex");
1722        std::fs::write(&root, "\\begin{document}\nSee \\cite{candes2006}.\n\\bibliography{bib/refs}\n\\end{document}\n").unwrap();
1723        let project = Project::read(&root);
1724        assert_eq!(
1725            project.citations.get("candes2006").map(String::as_str),
1726            Some("Candès and Tao 2006")
1727        );
1728        assert_eq!(detex("G\\\"odel, \\c c and \\ss"), "Gödel, ç and ß");
1729    }
1730    #[test]
1731    fn braced_and_particled_names_cite_whole() {
1732        let entry = |authors: &str| BibEntry {
1733            key: "k".into(),
1734            title: String::new(),
1735            authors: authors.into(),
1736            year: "2020".into(),
1737        };
1738        assert_eq!(
1739            entry("{World Health Organization}").short(),
1740            "World Health Organization 2020"
1741        );
1742        assert_eq!(entry("van der Berg, Jan").short(), "van der Berg 2020");
1743        assert_eq!(
1744            entry("Jan {van der Berg} and Ada Lovelace").short(),
1745            "van der Berg and Lovelace 2020"
1746        );
1747        assert_eq!(entry("Knuth, Donald").short(), "Knuth 2020");
1748    }
1749
1750    #[test]
1751    fn warnings_point_at_their_lines() {
1752        let root = Path::new("/p/main.tex");
1753        let output = "warning: sections/results.tex:4: Overfull \\hbox (3pt too wide)\nLaTeX Warning: Reference `sec:x' on page 1 undefined on input line 12.\n";
1754        let found = warning_places(output, "", Path::new("/p"), root);
1755        assert_eq!(found.len(), 2, "{found:?}");
1756        assert_eq!(found[0].file, Path::new("/p/sections/results.tex"));
1757        assert_eq!((found[1].file.as_path(), found[1].line), (root, 12));
1758        let entry = BibEntry {
1759            key: "er".into(),
1760            title: String::new(),
1761            authors: "Erd\u{151}s, Paul and R\u{e9}nyi, Alfr\u{e9}d".into(),
1762            year: "1959".into(),
1763        };
1764        assert_eq!(entry.short(), "Erd\u{151}s and R\u{e9}nyi 1959");
1765    }
1766
1767    #[test]
1768    fn pdflatex_runs_again_until_references_settle() {
1769        assert_eq!(more_passes("\\relax\n", false), (None, 0));
1770        assert_eq!(more_passes("\\newlabel{sec:a}{{1}{1}}\n", false), (None, 1));
1771        assert_eq!(
1772            more_passes("\\citation{k}\n\\bibdata{refs}\n", false),
1773            (Some(Bibliography::Bibtex), 2)
1774        );
1775    }
1776
1777    #[test]
1778    fn biblatex_with_biber_runs_biber() {
1779        let aux = "\\relax\n\\abx@aux@refcontext{nty/global//global/global}\n\\abx@aux@cite{0}{knuth84}\n";
1780        assert_eq!(more_passes(aux, true), (Some(Bibliography::Biber), 2));
1781        assert_eq!(more_passes("\\relax\n", true).0, Some(Bibliography::Biber));
1782        // Biblatex on the BibTeX backend names its data as BibTeX wants.
1783        let aux = "\\abx@aux@cite{0}{k}\n\\bibdata{main-blx,refs}\n";
1784        assert_eq!(more_passes(aux, true).0, Some(Bibliography::Bibtex));
1785    }
1786
1787    #[test]
1788    fn a_log_asking_for_another_pass_gets_one() {
1789        assert!(asks_rerun(
1790            "(./ch1.aux)\n\nLaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.\n"
1791        ));
1792        assert!(asks_rerun(
1793            "Package biblatex Warning: Please rerun LaTeX.\n(biblatex)                Page breaks have changed.\n"
1794        ));
1795        assert!(!asks_rerun("Output written on main.pdf (1 page).\n"));
1796    }
1797
1798    #[test]
1799    fn a_bibliography_that_fails_says_why() {
1800        let bibtex = "This is BibTeX, Version 0.99d (TeX Live 2023)\nThe top-level auxiliary file: main.aux\nThe style file: plain.bst\nI couldn't open database file refs.bib\n---line 3 of file main.aux\n :\\bibdata{refs\n :              }\nI'm skipping whatever remains of this command\n(There was 1 error message)\n";
1801        assert_eq!(
1802            Bibliography::Bibtex.failure(Some(2), bibtex).as_deref(),
1803            Some("I couldn't open database file refs.bib")
1804        );
1805        let warned = "This is BibTeX\nWarning--I didn't find a database entry for \"x\"\n";
1806        assert_eq!(Bibliography::Bibtex.failure(Some(1), warned), None);
1807        let biber = "INFO - This is Biber 2.19\nINFO - Reading 'main.bcf'\nERROR - Cannot find 'refs.bib'!\nINFO - ERRORS: 1\n";
1808        assert_eq!(
1809            Bibliography::Biber.failure(Some(2), biber).as_deref(),
1810            Some("Cannot find 'refs.bib'!")
1811        );
1812        assert_eq!(Bibliography::Biber.failure(Some(0), "INFO - Done\n"), None);
1813    }
1814
1815    #[test]
1816    fn front_matter_becomes_the_title_block() {
1817        let tex = from_markdown_with(
1818            "---\ntitle: The Plan\nauthor: Ada\n---\n\nWords.\n",
1819            &mut |_, _| None,
1820        );
1821        assert!(tex.contains("\\title{The Plan}"), "{tex}");
1822        assert!(tex.contains("\\maketitle"), "{tex}");
1823        assert!(!tex.contains("title:"), "{tex}");
1824    }
1825
1826    #[test]
1827    fn a_piece_built_alone_keeps_the_papers_setup_and_not_its_page() {
1828        let text = "\\documentclass{article}\n\\usepackage{tikz,bm}\n\\usepackage{hyperref}\n\\usepackage[margin=1in]{geometry}\n\\usetikzlibrary{arrows.meta}\n\\tikzset{\n  box/.style={draw}\n}\n\\newcommand{\\R}{\\mathbb{R}} % reals\n\\begin{document}\n\\newcommand{\\later}{x}\n\\end{document}\n";
1829        let setup = setup_of(text);
1830        assert_eq!(setup.len(), 4, "{setup:?}");
1831        assert!(setup[2].contains("box/.style"));
1832        let tex = standalone(&setup, "\\begin{tikzcd} A \\arrow[r] & B \\end{tikzcd}");
1833        assert!(tex.contains("\\usepackage{tikz-cd}"));
1834        assert!(tex.contains("\\newcommand{\\R}"));
1835        assert!(!tex.contains("geometry"));
1836        assert!(!tex.contains("later"));
1837        assert!(tex.ends_with("\\end{document}\n"));
1838    }
1839
1840    #[test]
1841    fn tikz_goes_into_the_paper_as_itself() {
1842        let tex = from_markdown("```tikz\n\\draw (0,0) -- (1,1);\n```\n");
1843        assert!(
1844            tex.contains("\\begin{tikzpicture}\n\\draw (0,0) -- (1,1);\n\\end{tikzpicture}"),
1845            "{tex}"
1846        );
1847        assert!(!tex.contains("verbatim"), "{tex}");
1848    }
1849
1850    #[test]
1851    fn an_export_loads_only_the_packages_it_uses() {
1852        let plain = from_markdown("# Notes\n\nSome $x^2$ and a [link](https://x.y).\n");
1853        for package in ["tikz", "ulem", "booktabs", "xcolor"] {
1854            assert!(!plain.contains(package), "{package}: {plain}");
1855        }
1856        let rich = from_markdown(
1857            "~~gone~~ <mark>lit</mark>\n\n| A |\n|---|\n| b |\n\n```tikz\n\\draw (0,0) -- (1,1);\n```\n",
1858        );
1859        assert!(
1860            rich.contains("\\usepackage{booktabs,xcolor,tikz}"),
1861            "{rich}"
1862        );
1863        assert!(rich.contains("\\usepackage[normalem]{ulem}"), "{rich}");
1864    }
1865
1866    #[test]
1867    fn footnotes_and_raised_text_reach_the_paper() {
1868        let tex = from_markdown("Water H~2~O and x^2^ here[^a].\n\n[^a]: Worth saying.\n");
1869        assert!(tex.contains("\\textsubscript{2}"), "{tex}");
1870        assert!(tex.contains("\\textsuperscript{2}"), "{tex}");
1871        assert!(tex.contains("\\footnote{Worth saying.}"), "{tex}");
1872        assert_eq!(tex.matches("Worth saying").count(), 1, "{tex}");
1873    }
1874
1875    #[test]
1876    fn a_diagram_and_a_sized_image_reach_the_paper() {
1877        let text = "```mermaid\nflowchart LR\n  A --> B\n```\n\n![Plot](a.png){width=40%}\n";
1878        let mut asked = Vec::new();
1879        let tex = from_markdown_with(text, &mut |kind, source| {
1880            asked.push((kind, source.to_string()));
1881            Some("drawn.png".to_string())
1882        });
1883        assert_eq!(asked[0].0, Kind::Mermaid);
1884        assert!(asked[0].1.contains("A --> B"));
1885        assert!(
1886            tex.contains("\\includegraphics[width=\\linewidth]{drawn.png}"),
1887            "{tex}"
1888        );
1889        assert!(
1890            tex.contains("\\includegraphics[width=0.40\\linewidth]{a.png}"),
1891            "{tex}"
1892        );
1893        assert!(!tex.contains("flowchart"), "{tex}");
1894        let fallback = from_markdown(text);
1895        assert!(fallback.contains("flowchart"), "{fallback}");
1896    }
1897
1898    #[test]
1899    fn a_tabular_edits_like_a_table() {
1900        let text = "\\begin{tabular}{lr}\n    \\hline\n    A & B \\\\\n    1 & 2 \\\\\n    \\hline\n\\end{tabular}";
1901        let more = edit_tabular(text, 1, 0, Edit::RowBelow).unwrap();
1902        assert_eq!(more.matches("\\\\").count(), 3, "{more}");
1903        let wider = edit_tabular(text, 0, 0, Edit::ColumnRight).unwrap();
1904        assert!(wider.contains("{llr}"), "{wider}");
1905        assert!(wider.contains("A &  & B"), "{wider}");
1906        let narrower = edit_tabular(text, 0, 1, Edit::DeleteColumn).unwrap();
1907        assert!(narrower.contains("{l}"), "{narrower}");
1908        assert!(!narrower.contains('B'), "{narrower}");
1909        let aligned = edit_tabular(text, 0, 0, Edit::Align(Align::Centre)).unwrap();
1910        assert!(aligned.contains("{cr}"), "{aligned}");
1911        let fewer = edit_tabular(text, 1, 0, Edit::DeleteRow).unwrap();
1912        assert!(!fewer.contains('1'), "{fewer}");
1913        assert_eq!(edit_tabular(text, 0, 0, Edit::Delete), None);
1914    }
1915
1916    #[test]
1917    fn a_new_table_has_its_size() {
1918        let table = new_table(3, 2);
1919        assert!(table.contains("{ll}"));
1920        assert!(table.contains("Column 1 & Column 2"));
1921        assert_eq!(table.matches("\\\\\n").count(), 3, "{table}");
1922    }
1923
1924    #[test]
1925    fn errors_are_found_with_their_lines() {
1926        let output = "note: Running TeX ...\nerror: main.tex:3: Undefined control sequence\nerror: halted on potentially-recoverable error as specified\n./sections/intro.tex:12: Missing $ inserted.\n";
1927        let found = problems(output, Path::new("/w"));
1928        assert_eq!(found.len(), 2, "{found:?}");
1929        assert_eq!(found[0].file, Path::new("/w/main.tex"));
1930        assert_eq!(found[0].line, 3);
1931        assert_eq!(found[0].message, "Undefined control sequence");
1932        assert_eq!(found[1].file, Path::new("/w/sections/intro.tex"));
1933    }
1934
1935    #[test]
1936    fn a_root_comment_names_the_main_file() {
1937        let chapter = Path::new("/w/ch/one.tex");
1938        assert_eq!(root_of(chapter, "\\section{A}\n"), chapter);
1939        let text = "% !TEX root = ../main.tex\n\\section{A}\n";
1940        assert_eq!(root_of(chapter, text), Path::new("/w/ch/../main.tex"));
1941    }
1942
1943    #[test]
1944    fn a_root_is_a_document_that_really_takes_the_file_in() {
1945        let dir = tempfile::tempdir().unwrap();
1946        let paper = dir.path().join("paper");
1947        let chapter = paper.join("chapters/one");
1948        std::fs::create_dir_all(&chapter).unwrap();
1949        let intro = chapter.join("intro.tex");
1950        std::fs::write(&intro, "\\section{Introduction}\n").unwrap();
1951        // Whole documents that only mention it, and come first by name.
1952        std::fs::write(
1953            chapter.join("a-notes.tex"),
1954            "\\documentclass{article}\n% \\input{intro}\nThe intro is long.\n",
1955        )
1956        .unwrap();
1957        std::fs::write(
1958            paper.join("a-draft.tex"),
1959            "\\documentclass{article}\nOn chapters/one/intro.\n",
1960        )
1961        .unwrap();
1962        let main = paper.join("main.tex");
1963        std::fs::write(
1964            &main,
1965            "\\documentclass{book}\n\\begin{document}\n\\include{chapters/one/intro}\n\\end{document}\n",
1966        )
1967        .unwrap();
1968        assert_eq!(
1969            root_of(&intro, "\\section{Introduction}\n"),
1970            main,
1971            "two folders up"
1972        );
1973        assert_eq!(
1974            root_of(&intro, "% \\documentclass{article}\n\\section{A}\n"),
1975            main,
1976            "a commented-out class isn't a whole document"
1977        );
1978        let stray = chapter.join("stray.tex");
1979        std::fs::write(&stray, "Text.\n").unwrap();
1980        assert_eq!(root_of(&stray, "Text.\n"), stray, "nothing takes it in");
1981        assert_eq!(
1982            root_of(&stray, "% !TeX root = ../../main.tex\nText.\n"),
1983            chapter.join("../../main.tex"),
1984            "TeXstudio's spelling"
1985        );
1986    }
1987
1988    #[test]
1989    fn an_input_anywhere_on_a_line_numbers_on() {
1990        let dir = tempfile::tempdir().unwrap();
1991        let root = dir.path().join("main.tex");
1992        std::fs::write(
1993            &root,
1994            "\\documentclass{article}\n\\begin{document}\n\\section{One}\nSee: \\input two\n\\input{self}\n\\end{document}\n",
1995        )
1996        .unwrap();
1997        std::fs::write(
1998            dir.path().join("two.tex"),
1999            "\\section{Two}\\label{sec:two}\n",
2000        )
2001        .unwrap();
2002        std::fs::write(
2003            dir.path().join("self.tex"),
2004            "\\section{Again}\\label{sec:again}\n\\input{self}\n",
2005        )
2006        .unwrap();
2007        let project = Project::read(&root);
2008        assert_eq!(project.labels["sec:two"], "2");
2009        assert_eq!(
2010            project.labels["sec:again"], "3",
2011            "a file that takes itself in, once"
2012        );
2013    }
2014
2015    #[test]
2016    fn a_paper_names_its_bibliographies_with_options_and_comments() {
2017        let text = "\\addbibresource[backend=biber]{lit/library.bib}\n\\bibliographystyle{plain}\n% \\bibliography{old}\n\\bibliography{a, ../shared/b}\n";
2018        let found = bibliographies_of(text, Path::new("/w"));
2019        assert_eq!(
2020            found,
2021            [
2022                Path::new("/w/lit/library.bib"),
2023                Path::new("/w/a.bib"),
2024                Path::new("/w/../shared/b.bib"),
2025            ]
2026        );
2027    }
2028
2029    #[test]
2030    fn a_bibliography_is_read_however_it_is_laid_out() {
2031        let bib = "@book\n{knuth84,\n title = {The {TeX}book},\n author = \"Knuth, Donald\",\n year = 1984\n}\n@comment{not this}\n@article{a2, title={B}, author={Ada Lovelace and Bob Smith}, year={2020}}\n";
2032        let entries = bib_entries(bib);
2033        assert_eq!(entries.len(), 2, "{entries:?}");
2034        assert_eq!(entries[0].key, "knuth84");
2035        assert_eq!(entries[0].title, "The TeXbook");
2036        assert_eq!(entries[0].short(), "Knuth 1984");
2037        assert_eq!(entries[1].short(), "Lovelace and Smith 2020");
2038    }
2039
2040    #[test]
2041    fn errors_in_input_files_keep_their_file() {
2042        let found = problems(
2043            "error: sections/two:10: Undefined control sequence\n",
2044            Path::new("/w"),
2045        );
2046        assert_eq!(found[0].file, Path::new("/w/sections/two.tex"));
2047        assert!(problems("error: halted: 5: nope\n", Path::new("/w")).len() < 2);
2048    }
2049
2050    #[test]
2051    fn a_paper_is_read_across_its_files() {
2052        let dir = tempfile::tempdir().unwrap();
2053        let root = dir.path().join("main.tex");
2054        std::fs::write(
2055            &root,
2056            "\\documentclass{article}\n\\input{macros}\n\\begin{document}\n\\section{One}\\label{sec:a}\n\\begin{equation}a\\label{eq:a}\\end{equation}\n\\input{two}\n\\end{document}\n",
2057        )
2058        .unwrap();
2059        std::fs::write(
2060            dir.path().join("macros.tex"),
2061            "\\newcommand{\\R}{\\mathbb{R}}\n\\newcommand{\\KL}[2]{D(#1\\|#2)}\n\\DeclareMathOperator*{\\argmax}{arg\\,max}\n\\DeclarePairedDelimiter{\\norm}{\\lVert}{\\rVert}\n\\newtheorem{thm}{Theorem}\n\\newtheorem{lem}[thm]{Lemma}\n",
2062        )
2063        .unwrap();
2064        std::fs::write(
2065            dir.path().join("two.tex"),
2066            "\\section{Two}\\label{sec:b}\n\\begin{align}\na &= b \\label{eq:b}\\\\\nc &= d \\notag\\\\\ne &= f \\label{eq:c}\n\\end{align}\n\\begin{thm}\\label{t:a}x\\end{thm}\n\\begin{lem}\\label{t:b}y\\end{lem}\n",
2067        )
2068        .unwrap();
2069        let project = Project::read(&root);
2070        assert_eq!(project.labels["sec:b"], "2");
2071        assert_eq!(project.labels["eq:a"], "1");
2072        assert_eq!(project.labels["eq:b"], "2", "per row");
2073        assert_eq!(project.labels["eq:c"], "3", "skipping the notag row");
2074        assert_eq!(project.labels["t:a"], "1");
2075        assert_eq!(project.labels["t:b"], "2", "a shared counter");
2076        let start = &project.starts[&dir.path().join("two.tex")];
2077        assert_eq!(start.sections[1], 1, "the next file carries on");
2078        assert_eq!(project.expand(r"x \in \R"), r"x \in \mathbb{R}");
2079        assert_eq!(project.expand(r"\KL{p}{q}"), r"D(p\|q)");
2080        assert_eq!(project.expand(r"\norm*{x}"), r"\left\lVert x \right\rVert");
2081        assert_eq!(project.theorem("lem").unwrap().counter, "thm");
2082        assert_eq!(project.theorem("proposition").unwrap().title, "Proposition");
2083        assert_eq!(
2084            root_of(&dir.path().join("two.tex"), "\\section{Two}\n"),
2085            root
2086        );
2087    }
2088
2089    #[test]
2090    fn macros_that_loop_still_end() {
2091        let mut project = Project::default();
2092        read_macro(r"\newcommand{\a}{\b}", &mut project);
2093        read_macro(r"\newcommand{\b}{\a}", &mut project);
2094        let _ = project.expand(r"\a");
2095    }
2096
2097    #[test]
2098    fn code_blocks_load_no_packages() {
2099        let shown = "Shown, not drawn:\n\n```latex\n\\begin{tikzcd} A \\arrow[r] & B \\end{tikzcd}\n\\sout{x} \\toprule \\color{red}\n```\n\nAnd `\\tikz` inline.\n";
2100        let tex = from_markdown(shown);
2101        let (preamble, _) = tex.split_once("\\begin{document}").unwrap();
2102        for package in ["tikz", "ulem", "booktabs", "xcolor"] {
2103            assert!(!preamble.contains(package), "{package}: {preamble}");
2104        }
2105        assert!(
2106            tex.contains("\\begin{tikzcd} A"),
2107            "the code is still shown: {tex}"
2108        );
2109        let used = from_markdown("~~gone~~\n\n```tikzcd\nA \\arrow[r] & B\n```\n");
2110        let (preamble, _) = used.split_once("\\begin{document}").unwrap();
2111        assert!(preamble.contains("ulem"), "{preamble}");
2112        assert!(preamble.contains("tikz-cd"), "{preamble}");
2113    }
2114
2115    #[test]
2116    fn warnings_are_noticed() {
2117        assert_eq!(
2118            warnings(
2119                "note: ok\nwarning: main.tex:3: Citation `x' undefined\n",
2120                ""
2121            )
2122            .len(),
2123            1
2124        );
2125        assert!(warnings("note: fine\n", "").is_empty());
2126    }
2127
2128    /// A pdflatex log over a root and a section file, with a citation key long enough that
2129    /// TeX breaks its warning at 79 characters.
2130    const SECTION_LOG: &str = "This is pdfTeX, Version 3.141592653\n(./main.tex\nLaTeX2e <2023-11-01>\n(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls\nDocument Class: article 2023/05/17 v1.4n Standard LaTeX document class\n(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))\n(./main.aux)\n\nLaTeX Warning: Reference `sec:intro' on page 1 undefined on input line 3.\n\n(./sections/results.tex\nOverfull \\hbox (3.0pt too wide) in paragraph at lines 1--2\n\nLaTeX Warning: Reference `sec:gone' on page 1 undefined on input line 2.\n\n\nLaTeX Warning: Citation `averyveryveryveryveryveryverylongcitationkeyhere' on p\nage 1 undefined on input line 5.\n\n) [1] (./main.aux)\n\nLaTeX Warning: There were undefined references.\n\n )\nOutput written on main.pdf (1 page, 1160 bytes).\n";
2131
2132    fn section_paper() -> tempfile::TempDir {
2133        let dir = tempfile::tempdir().unwrap();
2134        std::fs::create_dir(dir.path().join("sections")).unwrap();
2135        for file in ["main.tex", "main.aux", "sections/results.tex"] {
2136            std::fs::write(dir.path().join(file), "x\n").unwrap();
2137        }
2138        dir
2139    }
2140
2141    #[test]
2142    fn warnings_in_a_section_file_point_at_that_file() {
2143        let dir = section_paper();
2144        let root = dir.path().join("main.tex");
2145        let section = dir.path().join("sections/results.tex");
2146        let found = warning_places("", SECTION_LOG, dir.path(), &root);
2147        let places: Vec<(&Path, usize)> =
2148            found.iter().map(|p| (p.file.as_path(), p.line)).collect();
2149        assert_eq!(
2150            places,
2151            [
2152                (root.as_path(), 3),
2153                (section.as_path(), 2),
2154                (section.as_path(), 5)
2155            ],
2156            "{found:?}"
2157        );
2158        assert!(found[2].message.contains("page 1 undefined"), "{found:?}");
2159        // Tectonic names an `\input` file without its extension.
2160        let tectonic = "(main.tex (article.cls\n) (sections/results\n\nLaTeX Warning: Reference `x' on page 1 undefined on input line 4.\n\n) )\n";
2161        let found = warning_places("", tectonic, dir.path(), &root);
2162        assert_eq!(found[0].file, section, "{found:?}");
2163    }
2164
2165    #[test]
2166    fn each_warning_counts_once_from_the_last_pass() {
2167        let dir = section_paper();
2168        let root = dir.path().join("main.tex");
2169        // latexmk shows every pass; the log holds the last. Here the first pass had more.
2170        let first = "LaTeX Warning: Reference `sec:method' on page 1 undefined on input line 9.\n";
2171        let said = format!(
2172            "Latexmk: Run number 1 of rule 'pdflatex'\n{first}{SECTION_LOG}Latexmk: Run number 2 of rule 'pdflatex'\n{SECTION_LOG}"
2173        );
2174        let counted = warnings(&said, SECTION_LOG);
2175        assert_eq!(counted.len(), 3, "{counted:#?}");
2176        let placed = warning_places(&said, SECTION_LOG, dir.path(), &root);
2177        assert_eq!(placed.len(), 3, "{placed:#?}");
2178        // Tectonic says its own each pass, and the summary is not one more.
2179        let said = "warning: main.tex:4: Overfull \\hbox (3pt too wide)\nwarning: main.tex:4: Overfull \\hbox (3pt too wide)\nwarning: warnings were issued by the TeX engine; use --print and/or --keep-logs for details.\n";
2180        assert_eq!(warnings(said, SECTION_LOG).len(), 4);
2181        assert_eq!(warnings(said, "").len(), 1);
2182    }
2183
2184    #[test]
2185    fn markdown_becomes_latex_with_math_untouched() {
2186        let tex = from_markdown(
2187            "# Title\n\nSome **bold** 50% text with $x_i^2$.\n\n$$\\frac{a}{b}$$\n\n| A | B |\n|---|--:|\n| 1 | 2 |\n\n- one\n\n```\ncode_here\n```\n",
2188        );
2189        assert!(tex.contains("\\section*{Title}"));
2190        assert!(tex.contains("\\textbf{bold}"));
2191        assert!(tex.contains("50\\% text"));
2192        assert!(tex.contains("$x_i^2$"), "math is left alone: {tex}");
2193        assert!(tex.contains("\\frac{a}{b}"));
2194        assert!(tex.contains("\\begin{tabular}{lr}"));
2195        assert!(tex.contains("1 & 2"));
2196        assert!(tex.contains("\\begin{itemize}"));
2197        assert!(tex.contains("code_here"), "verbatim isn't escaped");
2198    }
2199}