This Python script checks the grammar, spelling, and other issues in LaTeX files (.tex) within a specified directory using pydetex
to extract plain text and language_tool_python
for grammar checking. It saves the results of each file's grammar check in a separate report file in a folder named grammar_check
.
You need to install the required libraries via pip:
pip install pydetex language_tool_python
- Replace
'/path/to/latex/project'
with the actual path to your LaTeX project directory. - Run the script.
- Check the
grammar_check
folder for individual grammar check reports for each LaTeX file.
import os
import pydetex.pipelines as pip
import language_tool_python
# Initialize LanguageTool
tool = language_tool_python.LanguageTool('en-US')
# Directory where your LaTeX project is located
latex_project_directory = '.'
# Directory where you want to save the grammar check reports
output_directory = 'grammar_check'
os.makedirs(output_directory, exist_ok=True)
# List all .tex files and check each one
for root, dirs, files in os.walk(latex_project_directory):
for file in files:
if file.endswith('.tex'):
path_to_file = os.path.join(root, file)
print("Checking ", path_to_file)
with open(path_to_file, 'r', encoding='utf-8') as f:
tex_content = f.read()
# Convert LaTeX to plain text
plain_text = pip.simple(tex_content)
# Check grammar
matches = tool.check(plain_text)
# Output filename
output_file_path = os.path.join(output_directory, f'grammar_check_{file}.txt')
# Write the matches to the output file
with open(output_file_path, 'w', encoding='utf-8') as out_f:
for match in matches:
out_f.write(f"{match}\n\n")
print(f"Grammar check complete for {file}, results saved to {output_file_path}")
print("Grammar checking for all .tex files is complete.")
tool.close()
This script is provided under the MIT License.
Replace '/path/to/latex/project'
with the actual path to your LaTeX project directory in the script section. Once you've filled in the details, you can create a gist on GitHub and paste this Markdown content there with a suitable name.