Md Abdus Samad
  • About
    • News
    • Contact
  • Publications
  • Top Global Scholarships
  • University Information
  • LaTeX
  • Miscellaneous
    • List of Publishers
    • Journal Templates
    • Verifying Journal Indexing
    • Reference, Image Quality, and Detexify
    • Author Services by Major Publishers
    • Document Conversion and Figure Tools
    • Manuscript Anonymization
    • Switching Elsevier LaTeX Templates
    • DOCX to LaTeX Convert
    • LaTeX Reference and Label Management
    • Latex Reference Converter
    • Sequential Section Labels
    • Open Access Journals having Discount Policy
    • Overleaf git sync issues
    • Latexdiff Configuration Guide
    • Open source tools
    • Windows shortcuts & commands
    • Mathpix PDF to Word

On this page

  • Pre-Conversion Checklist
    • Essential Steps
  • Advanced Regex Patterns
    • Citations - Multiple Scenarios
    • Figures - Context-Aware Patterns
    • Tables - Enhanced Patterns
  • Automated Label Generation
    • Smart Label Insertion
  • Advanced Workflow
    • Phase 1: Analysis and Preparation
    • Phase 2: Systematic Replacement
    • Phase 3: Quality Control
  • Professional Citation Management
    • Smart Bibliography Generation
  • Troubleshooting Common Issues
  • Final Verification Checklist

Professional LaTeX Document Conversion

From Hard-coded Numbers to Dynamic References

Author

Dr. Md Abdus Samad

Published

May 30, 2025

Pre-Conversion Checklist

WarningImportant

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 -c

Phase 2: Systematic Replacement

WarningOrder matters!

Follow this sequence for best results.

  1. Handle special cases first:
    • Citations with page numbers
    • Figure/table ranges
    • Parenthetical references
  2. Standard replacements:
    • Single citations
    • Single figure/table references
  3. 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}}
}}""")
TipPro Tip

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

TipFinal Pro Tip

Use a LaTeX-aware editor (TeXstudio, VS Code with LaTeX Workshop) that can highlight undefined references and provide auto-completion for labels.


NoteReady to Transform Your LaTeX Documents?

Follow this guide systematically for professional, maintainable LaTeX documents with dynamic cross-references!

 

© 2025 Dr. Md Abdus Samad. All rights reserved.