Repository: ManimCommunity/manim
File: manim/utils/tex_file_writing.py (Python)

Code before:
    tex_dir = Path(config.get_dir("tex_dir")).as_posix()
    if not os.path.exists(result):
        command = tex_compilation_command(
            tex_compiler, output_format, tex_file, tex_dir
        )
        exit_code = os.system(command)
        if exit_code != 0:
            log_file = tex_file.replace(".tex", ".log")
            if not Path(log_file).exists():
                raise RuntimeError(
                    f"{tex_compiler} failed but did not produce a log file. "
                    "Check your LaTeX installation."
                )
            with open(log_file, "r") as f:
                log = f.readlines()
                error_pos = [
                    index for index, line in enumerate(log) if line.startswith("!")
                ]
                if error_pos:
                    with open(tex_file, "r") as g:
                        tex = g.readlines()
                        logger.error("LaTeX compilation error: {log[error_pos][2:])}")
                        for log_index in error_pos:
                            index_line = log_index
                            context = (
                                f"Context for error:\n\n"
                            )

                            # Find where the line of the error is indicated in the log file
                            while not log[index_line].startswith("l."):
                                index_line += 1

                            # Find the index of the errored line in the tex file
                            tex_index = (
                                int(log[index_line].split(" ")[0].split(".")[1]) - 1
                            )

                            # Seek the environment scope that contains the error
                            environment = tex_index
                            while not tex[environment].startswith("\\begin"):
                                environment -= 1

                            # Print the entire environment including its end
                            while not tex[environment - 1].startswith("\\end"):
                                context += tex[environment]
                                if environment == tex_index:
                                    context += "^ this line\n"
                                environment += 1
                            logger.error(context)

                        # add insight for errors

Review comment (suggestion):
```suggestion
                                "Context for error:\n\n"
```

To make `flake8` pass

Code after:
    tex_dir = Path(config.get_dir("tex_dir")).as_posix()
    if not os.path.exists(result):
        command = tex_compilation_command(
            tex_compiler, output_format, tex_file, tex_dir
        )
        exit_code = os.system(command)
        if exit_code != 0:
            log_file = tex_file.replace(".tex", ".log")
            if not Path(log_file).exists():
                raise RuntimeError(
                    f"{tex_compiler} failed but did not produce a log file. "
                    "Check your LaTeX installation."
                )
            with open(log_file, "r") as f:
                log = f.readlines()
                error_pos = [
                    index for index, line in enumerate(log) if line.startswith("!")
                ]
                if error_pos:
                    with open(tex_file, "r") as g:
                        tex = g.readlines()
                        logger.error("LaTeX compilation error: {log[error_pos][2:])}")
                        for log_index in error_pos:
                            index_line = log_index
                            context = "Context for error:\n\n"

                            # Find where the line of the error is indicated in the log file
                            while not log[index_line].startswith("l."):
                                index_line += 1

                            # Find the index of the errored line in the tex file
                            tex_index = (
                                int(log[index_line].split(" ")[0].split(".")[1]) - 1
                            )

                            # Seek the environment scope that contains the error
                            environment = tex_index
                            while not tex[environment].startswith("\\begin"):
                                environment -= 1

                            # Print the entire environment including its end
                            while not tex[environment - 1].startswith("\\end"):
                                context += tex[environment]
                                if environment == tex_index:
                                    context += "^ this line\n"
                                environment += 1
                            logger.error(context)

                        # add insight for errors
                        for prog, get_insight in LATEX_ERROR_INSIGHTS:
                            match = re.search(prog, "".join(log[log_index:index_line]))