Advanced LaTeX report

Eight distinct advanced LaTeX inputs exercise adjacent matrices, grouped powers, piecewise expressions, nested radicals, sums, and derivatives inside a formatted report.

Compare input and output

Compare the actual HTML/MathML input in your browser with the generated DOCX exported to PDF by LibreOffice. Open the DOCX in Word to inspect editable equations. This example also builds content directly in Word. The browser panel shows only the inserted HTML/MathML fragments, in call order; the PDF includes the surrounding Word document and its styles.

Browser · HTML / MathML

Open HTML preview

LibreOffice · PDF

Open PDF

For Markdown and LaTeX examples, the browser shows the generated HTML or MathML. Browser MathML support, Word styles, fonts and page layout can produce different results.

The PDF panel shows page images. Open the original PDF to zoom, search or select text, or download the DOCX to edit it.

Run

Extract the example bundle, install the example dependencies, and run this command from the folder containing examples/:

python -m examples.27_advanced_latex

Generated DOCX

Reference PDF

Complete source

"""Advanced LaTeX inside a report with prose, tables, and explicit page breaks."""

from docx import Document
from docx.oxml.ns import qn
from docx.shared import Inches, Pt

from mathml2docx import MathmlToDocx, TableStyle
from mathml2docx.math import latex_to_omml

from .advanced_math import CATALOG, source
from .support import save

document = Document()
document.sections[0].left_margin = Inches(0.65)
document.sections[0].right_margin = Inches(0.65)
normal = document.styles["Normal"]
normal.font.name = "Malgun Gothic"
normal.element.get_or_add_rPr().get_or_add_rFonts().set(
    qn("w:eastAsia"), "Malgun Gothic"
)
parser = MathmlToDocx(
    table_style=TableStyle.GRID,
    stylesheet="* {font-family:'Malgun Gothic'} p,td {font-size:10pt} h2 {color:navy}",
)
parser.add_html_to_document(
    "<h1>Advanced LaTeX / 복합 수식</h1><p>행렬·적분·편미분을 다루는 연구 노트입니다.</p>",
    document,
)
for index, entry in enumerate(entry for entry in CATALOG if entry["format"] == "latex"):
    parser.add_html_to_document(
        "<h2>" + entry["id"] + ". " + entry["title"] + "</h2>", document
    )
    code = document.add_paragraph()
    code.paragraph_format.keep_with_next = True
    code_run = code.add_run(source(entry))
    code_run.font.name = "Consolas"
    code_run.font.size = Pt(9)
    document.add_paragraph()._p.append(latex_to_omml(source(entry)))
    parser.add_html_to_document(
        "<p><strong>검토:</strong> check every matrix boundary, grouped power and radical.</p>"
        "<table><tr><th>Original</th><th>Editable result</th></tr>"
        "<tr><td><i>LaTeX input</i></td><td>Separate numerator and denominator roles.</td></tr></table>"
        '<ol start="2"><li>Click and edit a variable.</li><li>다시 저장한 문서에서도 비교합니다.</li></ol>',
        document,
    )
    if index in {2, 5}:
        document.add_page_break()
save(document, "27_advanced_latex.docx")

Equation input files

Independent answers (JSON)