
Anyone who builds a quick script to translate an EPUB or academic paper with an LLM hits the same wall fast. On plain prose paragraphs, modern models read smoothly. On real books filled with italics, hyperlinks, footnote anchors, and mathematical formulas, outputs start breaking in predictable ways.
Most failures fall into three buckets: the model drops a closing tag or alters a footnote ID so the exported EPUB refuses to open; it helpfully rewrites an inline equation into plain words; or a strict validator rejects an otherwise perfect translation over a trailing space, burning tokens on retries that often degrade prose quality.
Reliable whole-book translation is less about crafting a longer prompt and more about drawing a clean line between what belongs to the LLM and what deterministic code must enforce.
1. Separate Two Jobs: Translating Meaning vs. Filling Back Structure
Asking a model to translate nuanced prose and simultaneously keep every XHTML tag, attribute, and ID intact in one shot puts two competing constraints on the same generation. Models either produce stiff phrasing to appease markup or quietly drop inline tags to keep the sentence flowing.
In the open-source PDF Craft translation pipeline, the work splits cleanly:
- Context-aware narrative translation: The translator includes neighboring context around group boundaries when translating each segment, then commits only the target group. This helps pronouns and cross-references stay coherent while instructing the model to translate faithfully without summarizing.
- Structure filling anchored to the translated draft: The first-stage translation acts as the full-text truth, while the source XML supplies node identities, styling, and whitespace context. Validation verifies missing blocks, source IDs, and required inline tags before accepting the candidate.
Separating prose quality from markup alignment prevents silent paragraph drops and broken cross-references.
2. The Acceptance Principle: Recover What Is Unambiguous, Retry Only What Is Not
A common engineering mistake is equating strict validation with raw byte-for-byte comparison, forcing a full LLM retry whenever the model adds a harmless trailing space or drops one recoverable word separator inside a plain text slot.
PDF Craft's LLM_ACCEPTANCE rule draws a sharper boundary:
Whenever a surface formatting difference is unambiguous and recoverable by deterministic code, recover it and accept the result rather than wasting tokens on retries. Ask the model to fix output only when information is genuinely missing or placement is ambiguous.
Here is how that rule behaves on real XML and CSS whitespace edge cases:
| Observed Difference | Ambiguous? | Pipeline Action |
|---|---|---|
| Extra trailing space or a single missing normal space inside one plain text slot | No | Once non-whitespace characters align exactly with the full translation, recover the normal space under shared HTML/CSS whitespace rules and accept with zero extra retries. |
Protected whitespace (pre, pre-wrap, xml:space="preserve", or NBSP) | Depends on ownership | Treated strictly separately from collapsible spaces. A single protected owner restores from known translation text; whitespace misplaced onto a normal parent returns to its unique source owner. |
| Missing space on the boundary between two different inline style nodes with multiple valid owners | Yes | Never guess. Trigger a bounded retry so the model clarifies placement; recovery is transactional so a failed candidate is never partially mutated. |
| Dropped text, altered wording, missing source ID, or missing required tags | Yes | Reject strictly and enter bounded repair retries, falling back cleanly rather than emitting broken markup. |
Strict fill-stage alignment against the translated draft and node identities, paired with zero wasted retries on unambiguous whitespace, keeps long books both reliable and affordable. Because fill validation checks against that first-stage draft rather than re-auditing translation meaning against the original source, important passages are still worth comparing directly with the source text.
3. Formula Protection (PDF / .pcex Translation): Visible for Context, Untouchable in Output
Inline formulas create a classic trap in STEM books. Replace equations with opaque placeholders before translation and the model cannot tell what the sentence refers to; let the model emit raw LaTeX freely and it often alters variables or turns 16 \times into plain words.
Specifically within the PDF / .pcex translation pipeline, PDF Craft handles inline equations in three deterministic steps (direct EPUB uploads use separate EPUB math handling rather than these PCEX-specific canonical rules):
- Visible during translation: Inline formulas stay represented as dedicated nodes in chapter XML so the model understands the math in context and writes natural surrounding phrasing.
- No brittle LaTeX diffing inside the fill loop: Structure filling passes canonical expressions via immutable
math-refreferences and checks tag structure, IDs, and full-text alignment against the translated draft, preventing endless retries over phrasing differences. - Deterministic restoration after validation: Once a candidate exits the validation loop cleanly, a deterministic post-pass (
ChapterFormulaInterrupter) restores the exact original source formula by node identity. Whitespace recovery also places boundary spaces onto surviving text/tail fields so formula restoration never swallows adjacent spaces.
Standalone display equations (DisplayFormula) remain hard reading-flow boundaries that supply context to adjacent paragraphs without merging into prose, while formula titles and captions still translate normally so readers understand the explanation while keeping every symbol intact.
4. What This Means for Everyday Readers
You do not need to care about XML validation internals to benefit from this architecture, though knowing how it works helps you get better results:
- Lower risk of corrupt EPUB containers: Strict structural checks prevent malformed markup. In our Scanned PDF to EPUB Benchmark, all 9 test books scored 100/100 for file validity after original-language conversion with translation off (that benchmark measured container validity rather than translated output or chapter completeness).
- Use bilingual output for dense non-fiction: For academic, technical, or philosophy books, choose bilingual EPUB output (see Translated vs. Bilingual EPUB Samples) so the original sentence sits right above the translation whenever you want to verify a term.
- Understand the real boundary: Structural validation guards against dropped nodes, broken tags, and mutated formulas, not every nuance of human literary editing. When bounded retries exhaust on an edge case, the system may preserve original text rather than ship broken markup. Specify key names and terminology upfront in the translation notes, and spot-check 2 to 3 dense pages after export (you can also inspect real translated book examples in the Conversion Examples Library).



