import re


def tex_regex(block_name, regex_name=None):
    """ creates a regular expression that captures
        latex environments. Captures two parts,
        the first one is the whole environment with the tags
        and can be renamed using regex_name.
        The second one is named content and doesn't include the tags.

    :block_name: the name of the block
    :regex_name: the name to give the group that matches
                 the whole block
    :returns:    a compiled regular expression
    """

    names = (regex_name if regex_name else block_name,
             block_name,
             block_name)
    regexp = re.compile(r'(?P<%s>\\begin{%s}(?P<content>.*?)\\end{%s})'
                        % names,
                        re.DOTALL)
    return regexp