如何通过find、exec和sed为行前置变量?
Hey there! Let's work through this .tex file refactoring challenge together. Since you’ve already tried existing forum solutions without luck, let’s dive into targeted, actionable approaches tailored to your goal of building a standardized metadata management system for your files.
This is my go-to for custom, scalable refactoring—perfect if you need to handle edge cases or extend the workflow later. Here’s a practical example that extracts your \section and \status tags, moves them into a structured metadata header, and cleans up the original content:
import os import re # Set your target directory containing .tex files TEX_DIRECTORY = "./your_tex_collection" # Define a clean metadata template to add at the top of each file METADATA_TEMPLATE = """%% --------------------- %% Metadata %% Title: {title} %% Status: {status} %% --------------------- """ for filename in os.listdir(TEX_DIRECTORY): if not filename.endswith(".tex"): continue file_path = os.path.join(TEX_DIRECTORY, filename) with open(file_path, "r", encoding="utf-8") as f: content = f.read() # Extract title and status using regex (adjust if your tags have edge cases) title_match = re.search(r"\\section\{(.*?)\}", content) status_match = re.search(r"\\status\{(.*?)\}", content) if title_match and status_match: title = title_match.group(1) status = status_match.group(1) # Remove the original \status line and prepend the metadata header cleaned_content = re.sub(r"\\status\{.*?\}\s*", "", content) final_content = METADATA_TEMPLATE.format(title=title, status=status) + cleaned_content # Write back the modified file (always back up originals first!) with open(file_path, "w", encoding="utf-8") as f: f.write(final_content) print(f"Successfully processed: {filename}") else: print(f"Skipping {filename}: Missing title or status tag")
Tips for this approach:
- Backup first: Copy all your .tex files to a backup folder before running the script.
- Adjust regex: If your
\sectionor\statustags have nested braces or unusual formatting, tweak the regex patterns to match your actual file structure. - Extend as needed: You can add logic to export metadata to a separate CSV/JSON file for easier bulk management.
If you’re comfortable with terminal commands, sed and awk can handle simple refactoring jobs fast. Here’s a bash loop that adds a metadata header and removes the original \status line:
# Loop through all .tex files in the current directory for file in *.tex; do # Extract title and status from the file title=$(sed -n 's/\\section{\(.*\)}/\1/p' "$file") status=$(sed -n 's/\\status{\(.*\)}/\1/p' "$file") # Insert metadata at the top and delete the original \status line sed -e "1i\\%% Metadata\n%% Title: $title\n%% Status: $status\n%% ---------" -e '/\\status{.*}/d' "$file" > "${file%.tex}_refactored.tex" done
Note:
This works best if your \section and \status tags follow a consistent format. If you have files with missing tags or complex line structures, you’ll need to add error-checking to the loop.
If you want your metadata to integrate directly with your LaTeX workflow, use packages like metadata or datatool to standardize how you store and access these tags:
- Add the package to your document preamble:
\usepackage{metadata} - Replace your existing
\status{Work in progress}with\metadata{status}{Work in progress} - You can then call metadata fields anywhere in your document (e.g.,
\getmetadata{status}) or export them to an external file for management.
For batch updating existing files, combine this with the Python/command-line scripts above to automate replacing old tags with the new package syntax.
内容的提问来源于stack exchange,提问作者Etsaf

