Professional LaTeX Document Conversion
From Hard-coded Numbers to Dynamic References
Pre-Conversion Checklist
Before starting any replacements, ensure you have proper backups!
Essential Steps
Advanced Regex Patterns
Citations - Multiple Scenarios
Basic single citations
Find:
\[(\d+)\]
Replace:
\\cite{$1-key}Range citations [1-3] or [1–3]
Find:
\[(\d+)[-–](\d+)\]
Replace:
\\cite{$1-key,$2-key}Manual cleanup needed for intermediate references
Multiple citations [1, 2, 5]
Find:
\[(\d+(?:,\s*\d+)*)\]
Replace:
\\cite{process-this-manually}Citations with pages [1, p. 15]
Find:
\[(\d+),\s*p\.\s*(\d+)\]
Replace:
\\cite[$2]{$1-key}Figures - Context-Aware Patterns
Standard references
Find:
(?i)(figure|fig\.?)\s+(\d+)
Replace:
Figure~\\ref{fig:$2}With parentheses (Figure 1) or (Fig. 1)
Find:
\((?i)(figure|fig\.?)\s+(\d+)\)
Replace:
(Figure~\\ref{fig:$2})Beginning of sentences
Find:
^(Figure|Fig\.?)\s+(\d+)
Replace:
Figure~\\ref{fig:$2}Plural figures
Find:
(?i)(figures)\s+(\d+)[-–](\d+)
Replace:
Figures~\\ref{fig:$2}--\\ref{fig:$3}Tables - Enhanced Patterns
Standard references
Find:
(?i)(table)\s+(\d+)
Replace:
Table~\\ref{tab:$2}Multiple tables
Find:
(?i)(tables)\s+(\d+)\s+and\s+(\d+)
Replace:
Tables~\\ref{tab:$2} and~\\ref{tab:$3}Automated Label Generation
Smart Label Insertion
For figures with meaningful captions
Find:
(\\caption\{([^}]{1,50})[^}]*\})
Replace:
$1\n\\label{fig:auto-generated-from-caption}For tables
Find:
(\\caption\{([^}]{1,50})[^}]*\})
Replace:
$1\n\\label{tab:auto-generated-from-caption}Advanced Workflow
Phase 1: Analysis and Preparation
Count occurrences to estimate scope:
# Count citation occurrences
grep -o '\[\d\+\]' document.tex | sort | uniq -c
# Count figure references
grep -o -i 'figure \d\+' document.tex | sort | uniq -c
# Count table references
grep -o -i 'table \d\+' document.tex | sort | uniq -cPhase 2: Systematic Replacement
Follow this sequence for best results.
- Handle special cases first:
- Citations with page numbers
- Figure/table ranges
- Parenthetical references
- Standard replacements:
- Single citations
- Single figure/table references
- Add labels systematically
Phase 3: Quality Control
# Find unreferenced labels
grep '\\label{' *.tex | cut -d':' -f2 | sort > labels.txt
grep '\\ref{' *.tex | cut -d':' -f2 | sort > refs.txt
comm -23 labels.txt refs.txt # Shows unused labels
# Find broken references (compile and check log)
pdflatex document.tex 2>&1 | grep -i "reference.*undefined"Professional Citation Management
Smart Bibliography Generation
import re
# Extract all citation numbers from your document
with open('document.tex', 'r') as f:
content = f.read()
citations = re.findall(r'\[(\d+)\]', content)
unique_citations = sorted(set(map(int, citations)))
# Generate template .bib entries
for num in unique_citations:
print(f"""
@article{{ref{num},
author = {{Author, First}},
title = {{Title from reference {num}}},
journal = {{Journal Name}},
year = {{Year}},
volume = {{Vol}},
pages = {{Pages}}
}}""")Use the cleveref package for smart references that automatically determine the reference type (Figure, Table, etc.).
% In preamble, add consistent reference formatting
\usepackage{cleveref} % Smart references
\crefname{figure}{Figure}{Figures}
\crefname{table}{Table}{Tables}
% Then use \cref{fig:label} instead of Figure~\ref{fig:label}Troubleshooting Common Issues
| Issue | Solution |
|---|---|
| Multiple replacements on same line | Use global flag in your editor: /g |
| LaTeX special characters in regex | Escape backslashes: \\ in find/replace |
| Reference ordering after conversion | Run: pdflatex → bibtex → pdflatex → pdflatex |
| Overlapping patterns | Process from most specific to most general |
Final Verification Checklist
Use a LaTeX-aware editor (TeXstudio, VS Code with LaTeX Workshop) that can highlight undefined references and provide auto-completion for labels.
Follow this guide systematically for professional, maintainable LaTeX documents with dynamic cross-references!