import re


def fmtlog(txt):
    """
    Reformat the text of the one-line log as LaTeX.

    Arguments:
        txt: string to reformat.

    Returns:
        A LaTeX formatted version of the input.
    """
    # Replace TeX special characters in the whole text.
    specials = ("_", "#", "%", r"\$", "{", "}")
    for s in specials:
        txt = re.sub(r"(?<!\\)" + s, "\\" + s, txt)
    # Remove periods at the end of lines.
    txt = re.sub(r"\.$", "", txt, flags=re.MULTILINE)
    lines = txt.split("\n")
    # Remove reference to HEAD
    lines[0] = re.sub(r"\(.*\) ", "", lines[0])
    # Use typewriter font for the commit id.
    lines = [r"\texttt{" + re.sub(" ", r"} ", ln, count=1) for ln in lines if ln]
    return "\\\\\n".join(lines)