markdown_to_latex()

Parse CommonMark/GFM and render as LaTeX.

Usage

Source

markdown_to_latex(
    text,
    *,
    hardbreaks=False,
    smart=False,
    normalize=False,
    unsafe=False,
    footnotes=False,
    extensions=(),
    width=0,
    options=0
)

Converts Markdown to LaTeX source suitable for inclusion in a .tex document. Inline formatting maps to LaTeX commands (\textbf{}, \emph{}, \texttt{}), headings become sectioning commands (\section{}, \subsection{}, etc.), and links use \href{}{}.

The output is a fragment (not a full document): it does not include \documentclass, \begin{document}, or preamble. You can concatenate it into your own LaTeX document structure.

Parameters

text: str

The Markdown string to parse and render. Must be a Python str.

hardbreaks: bool = False

Render soft line breaks as hard breaks (\\). By default, this is False.

smart: bool = False

Convert straight quotes to curly quotes, -- to en-dashes, --- to em-dashes, and ... to ellipses. By default, this is False.

normalize: bool = False

Consolidate adjacent text nodes in the parsed AST. By default, this is False.

unsafe: bool = False

Allow raw HTML to pass through. Since LaTeX is the target format, raw HTML is typically irrelevant. By default, this is False.

footnotes: bool = False

Enable footnote syntax parsing. By default, this is False.

extensions: Sequence[str] = ()

A sequence of GFM extension names to enable. Valid names are "table", "strikethrough", "autolink", "tagfilter", and "tasklist".

width: int = 0

The column at which to wrap output lines. Set to 0 (the default) to disable line wrapping entirely. A value like 80 produces lines of at most 80 characters where possible.

options: int = 0
An integer bitmask of Options flags (e.g., Options.SMART | Options.UNSAFE). Merged via OR with any boolean keyword arguments set to True. Defaults to 0.

Keyword Arguments Vs. Options Flags

The boolean keyword arguments (smart, unsafe, hardbreaks, etc.) are convenience shortcuts for the most common Options flags. When both styles are provided, they are merged via OR. See markdown_to_html() for a detailed explanation.

Returns

str
The rendered LaTeX string.

Examples

Render inline formatting:

from multimark import markdown_to_latex

markdown_to_latex("**bold** and *italic*")
'\\textbf{bold} and \\emph{italic}\n'

Render a heading:

markdown_to_latex("# Introduction")
'\\section{Introduction}\n'

Enable line wrapping at 72 columns:

long_text = "This is a very long paragraph. " * 10
print(markdown_to_latex(long_text, width=72))
This is a very long paragraph. This is a very long paragraph. This is a
very long paragraph. This is a very long paragraph. This is a very long
paragraph. This is a very long paragraph. This is a very long paragraph.
This is a very long paragraph. This is a very long paragraph. This is a
very long paragraph.

Use smart punctuation for typographic output:

markdown_to_latex('"Hello," she said --- "goodbye."', smart=True)
"``Hello,'' she said --- ``goodbye.''\n"