Hierarchical Parsing
Overview¶
The parser uses LaTeX structure to identify document components. It operates on each version separately, then passes the resulting trees to the publication-level standardization pipeline. The main implementation is in gather.py, hierarchy.py, and pipeline.py.
Root File Detection¶
find_document_root() searches the source directory and its subdirectories for .tex files containing an uncommented \documentclass declaration. When several candidates exist, a ranking heuristic selects a primary manuscript using source and metadata cues. The choice is logged so that ambiguous bundles can be inspected.
The parser therefore does not assume that every publication uses a file named main.tex, and it does not concatenate all .tex files in the archive.
Targeted checkpoint inspection found an ambiguity when the article title is defined in an included shared file: the heuristic selected a bundled style guide with its own title instead of the article root. An explicit-root local probe recovered the article tree. This case and the scope of the diagnostic are documented in Statistics.
Multi-file Resolution¶
compilation_path() recursively follows \input and \include from the chosen root. Commented commands are ignored. concatenate_compilation_tex() expands included content at the position of each include, preserving the surrounding order. Files outside this path are recorded as unused rather than parsed as article content.
The traversal detects recursive include cycles. A repeated include can be expanded again when it is not part of the active recursion chain. The implementation also evaluates supported conditional constructs; unknown conditional macros are not treated as a complete simulation of LaTeX compilation.
An unresolved source include is recorded in missing_includes. The available content can still be parsed, but the version is classified as partial rather than fully parsed.
The reviewed resolver searches the including file’s directory and the source root. A targeted case declared an additional content/ search directory through \input@path; files existed there but were not found by the default resolver. A local diagnostic using that literal declared path recovered the missing sections. Other inspected warnings arose from filename case mismatches or from input commands inside verbatim examples, command definitions, and unsupported conditional branches. The report retains the exported outcomes rather than treating every warning as confirmed article-content loss.
Document Hierarchy¶
parse_tex_to_tree() creates a document root and processes the expanded source in order. A stack tracks the active heading levels. A subsection is attached below the relevant section; a subsequent section closes deeper levels before a sibling section is added. Prose becomes paragraphs containing sentence nodes.
Lists form additional levels: a list contains list-item nodes, whose contents are parsed further into sentences and other supported components. Acknowledgements and appendices are retained, including supported unnumbered headings. Reference headings and bibliography environments are excluded from the body hierarchy; their entries are handled separately by the bibliography extractor.
Leaf Elements¶
The parser distinguishes three principal leaf categories:
Sentence: prose split at sentence boundaries. The splitter protects supported abbreviations, URLs, decimal numbers, mathematics, and command arguments from inappropriate splitting.
Formula: a display-math block, including supported equation environments and display delimiters.
Figure: a figure or table environment retained as one component. The source of the environment is kept without requiring the binary image itself.
Figure and table internals are not interpreted as ordinary prose sentences. List environments are expanded into items rather than treated as a single figure-like leaf.
Edge Cases¶
list_local_versions() combines local version directories with version information in M1 metadata and status records. The full pipeline records four outcomes:
| Outcome | Interpretation |
|---|---|
| Parsed | A nonempty version was processed without a recorded failure or unresolved include. |
| Partial | Available content was parsed, but a required include could not be resolved. |
| Empty | M1 explicitly marked the version as having no TeX source; it contributes no parsed elements. |
| Failed | Required sources or a document root were missing, or processing raised an exception. |
These outcomes and their reasons are retained in the per-publication coverage records in metrics_m2.json. A missing source directory is not automatically reclassified as an expected empty M1 record.
The implementation is a rule-based source parser rather than a full LaTeX compiler. Dynamic include macros, unfamiliar document commands, ambiguous roots, and complex conditional compilation can affect the recovered content. A parsed status means that the implemented processing completed without a recorded problem; it is not a measurement of semantic accuracy or proof that every source construct was interpreted correctly.