LaTeX TOC Parsing and Chapter Structure Extraction
This document explains how Codexis extracts chapter structure from LaTeX documents and generates individual HTML files for each chapter.
Overview
The LaTeX to HTML conversion process in Codexis involves several steps to properly extract and represent the document's hierarchical structure:
- TOC File Generation: The LaTeX document is compiled to generate a
.tocfile - TOC Parsing: The
.tocfile is parsed to extract chapter, section, and subsection information - Hierarchical Structure: The flat TOC entries are organized into a hierarchical structure
- HTML Generation: make4ht is configured to generate separate HTML files for chapters
- Navigation: Links are added between chapter files to enable easy navigation
TOC File Format
LaTeX generates a Table of Contents (TOC) file with the extension .toc when you compile a document. The format of this file varies depending on whether you're using standard LaTeX or the hyperref package:
Standard LaTeX Format
\contentsline {chapter}{\numberline {1}Introduction}{1}
\contentsline {section}{\numberline {1.1}Background}{2}
Hyperref Format
\contentsline {chapter}{\numberline {1}Introduction}{1}{chapter.1}
\contentsline {section}{\numberline {1.1}Background}{2}{section.1.1}
Each line consists of:
- The command
\contentsline - The entry type (
chapter,section, etc.) - The entry title, which may contain LaTeX commands
- The page number
- (With hyperref) The anchor reference ID
Implementation Details
1. Parsing the TOC File
The main parsing function is parse_toc_file/1 in Codexis.Builders.LaTeXToHTMLConverter, which:
- Reads the TOC file content
- Uses regular expressions to extract entry information
- Cleans LaTeX commands from titles
- Creates a data structure for each entry
# Parse a TOC file to extract document structure
defp parse_toc_file(toc_file) do
# Read TOC file...
# Extract entries...
# Build hierarchical structure...
end
2. Building Hierarchical Structure
The hierarchical document structure is built using the build_hierarchical_structure/1 function, which:
- Maps entry types to levels (chapter = 1, section = 2, etc.)
- Creates a tree-like structure with parent-child relationships
- Assigns HTML filenames to top-level entries (typically chapters)
# Example of hierarchical structure:
[
%{
level: 1,
type: "chapter",
title: "Introduction",
page: 1,
file: "chapter1.html",
children: [
%{
level: 2,
type: "section",
title: "Background",
page: 2,
children: []
}
]
}
]
3. Splitting HTML by Chapter
The chapter-based HTML generation is configured through a custom Lua script for make4ht that:
- Identifies chapter boundaries in the generated HTML
- Extracts content for each chapter
- Creates separate HTML files for each chapter
- Generates a table of contents file (index.html)
- Adds navigation links between chapter files
Special Handling
LaTeX Commands in Titles
LaTeX titles often contain commands that need to be properly handled:
# Clean LaTeX commands from titles
defp clean_latex_title(title) do
title
|> String.replace(~r/\\LaTeX\b/, "LaTeX")
|> String.replace(~r/\\textbf\{([^}]+)\}/, "\\1")
# ... more replacements
end
Mathematical Notation
Math expressions in titles are detected and replaced with a placeholder:
"Quadratic Formula: $x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$"
# Becomes:
"Quadratic Formula: [math]"
Appendices
Appendices are treated as chapters but with special filename handling:
case entry.type do
"chapter" -> "chapter#{entry.page}.html"
"appendix" -> "appendix#{String.downcase(extract_appendix_letter(clean_title))}.html"
# ...
end
Usage
The TOC parsing functionality is automatically used when converting LaTeX to HTML with chapter splitting enabled:
LaTeXToHTMLConverter.convert(source_dir, main_tex_file, output_dir, split_chapters: true)
Or when using the Make4HTWrapper directly:
Make4HTWrapper.convert(main_tex_file, output_dir, %{split_by_chapter: true})
Demo and Testing
You can test the TOC parsing functionality using the provided demo script:
mix run examples/toc_parser_demo.exs examples/latex_samples/complex.toc
For automated testing, see the test/codexis/builders/toc_parser_test.exs file.
Limitations and Future Improvements
- Current implementation primarily focuses on chapter-level splitting
- Math expressions in titles are replaced with placeholders
- Complex nested structures beyond chapter > section > subsection might not be perfectly represented
- Future improvements could include customizable splitting levels and better handling of special cases