def get_context(tex: str, before: int, after: int) -> str:
    """
    Extract context around a span of TeX. The extracted context should be
    rich enough to support pattern matching to determine the context in which
    a sentence appears. The context extracted is one full line above and
    below the the "before" and "after" character indexes.

    Given an original `tex` with `labelInTOC]{Convolutional layer}`, its corresponding
    context might be ` \\caption[labelInTOC]{Convolutional layer}`.
    """
    context_before = " ".join(tex[:before].splitlines()[-2:])
    context_after = " ".join(tex[after:].splitlines()[:2])
    return context_before + tex[before:after] + context_after