Citation Automation with PowerShell

Replace Numeric Citations with Citation Keys in LaTeX

Author

Dr. Md Abdus Samad

Published

January 1, 2026

The Problem

NoteWhy This Matters

Have you ever found yourself manually replacing hundreds of \cite{1} with \cite{authorYEARkeyword} in your LaTeX document? This PowerShell script automates the entire process in seconds.

If you’re working on a research paper or thesis in LaTeX, you’ve probably encountered this frustrating situation: you have a BibTeX file with proper citation keys (like martinez2023neural), but your LaTeX document uses numeric references like \cite{1}, \cite{2}, etc.

Example of the Problem

Your .tex file looks like this:

Recent advances in neural networks have revolutionized climate modeling \cite{1}\cite{2}.
These computational breakthroughs enable unprecedented accuracy in predictions \cite{3}.

But your .bib file has entries like:

@article{martinez2023neural,
  title={Neural network approaches to climate modeling},
  author={Martinez, Elena and Chen, Wei and O'Brien, Patrick},
  journal={Nature Climate Change},
  year={2023}
}

The disconnect between numeric citations and proper citation keys creates maintenance headaches.

Common Issues

Issue Example Impact
Numeric citations \cite{1} instead of \cite{martinez2023neural} Hard to maintain
Manual replacement Find-replace one by one Time-consuming, error-prone
Large documents 50+ citations Impractical to fix manually

The Solution: PowerShell Automation

What You’ll Learn

  • How to create a mapping between numbers and citation keys
  • How to use regex patterns to find and replace citations
  • How to automate the entire process with PowerShell

Prerequisites

  • Windows PC (PowerShell comes pre-installed)
  • A LaTeX file with numeric citations
  • A BibTeX file with citation keys

Step 1: Create Your Citation Mapping

First, create a mapping between the numbers and actual citation keys. Example bibliography:

%1
@article{martinez2023neural,
  title={Neural network approaches to climate modeling},
  author={Martinez, Elena and Chen, Wei},
  journal={Nature Climate Change},
  year={2023}
}

%2
@inproceedings{johnson2024quantum,
  title={Quantum computing applications in cryptography},
  author={Johnson, Michael A and Lee, Sarah K},
  booktitle={Proceedings of the ACM Conference},
  year={2024}
}

%3
@article{patel2022sustainable,
  title={Sustainable urban development},
  author={Patel, Rajesh and Williams, Jennifer},
  journal={Urban Studies},
  year={2022}
}

Step 2: The PowerShell Script

Copy and save as replace-citations.ps1:

# LaTeX Citation Replacement Script
# Replaces \cite{number} with \cite{citationkey}

# Create the citation mapping
$citationMap = @{
    '1' = 'martinez2023neural'
    '2' = 'johnson2024quantum'
    '3' = 'patel2022sustainable'
    '4' = 'anderson2021machine'
    '5' = 'nguyen2023biodiversity'
}

# Interactive file selection
Write-Host "Please select your LaTeX file..." -ForegroundColor Cyan
Add-Type -AssemblyName System.Windows.Forms

$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Filter = "LaTeX files (*.tex)|*.tex|All files (*.*)|*.*"
$openFileDialog.Title = "Select your LaTeX file"

if ($openFileDialog.ShowDialog() -eq 'OK') {
    $inputFile = $openFileDialog.FileName
    $outputFile = $inputFile -replace '\.tex$', '_updated.tex'

    Write-Host "Selected: $inputFile" -ForegroundColor Green
} else {
    Write-Host "No file selected. Exiting." -ForegroundColor Red
    exit
}

# Read the file
Write-Host "`nProcessing file..." -ForegroundColor Cyan
$content = Get-Content $inputFile -Raw

# Count citations
$beforeCount = ([regex]::Matches($content, '\\cite\{\d+\}')).Count
Write-Host "Found $beforeCount numeric citations" -ForegroundColor Yellow

# Replace citations using regex
$pattern = '\\cite\{(\d+)\}'
$result = [regex]::Replace($content, $pattern, {
    param($match)
    $number = $match.Groups[1].Value
    $key = $citationMap[$number]

    if ($key) {
        return "\cite{$key}"
    } else {
        Write-Host "Warning: No mapping for citation $number" -ForegroundColor Yellow
        return $match.Value
    }
})

# Save the result
$result | Set-Content $outputFile -NoNewline

# Show results
Write-Host "`nReplacement complete!" -ForegroundColor Green
Write-Host "Output: $outputFile" -ForegroundColor Green

Write-Host "`nPreview of replacements:" -ForegroundColor Magenta
$matches = [regex]::Matches($result, '\\cite\{[a-z]+\d{4}[a-z]+\}') | Select-Object -First 3
foreach ($m in $matches) {
    Write-Host "  $($m.Value)" -ForegroundColor White
}

Step 3: How to Use the Script

First-Time Setup (One Time Only)

WarningEnable PowerShell Scripts
  1. Press Windows + X
  2. Click “Windows PowerShell (Admin)”
  3. Run: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
  4. Type Y and press Enter

Running the Script

  1. Save the script - Copy the code above and save as replace-citations.ps1

  2. Run it:

    • Right-click on replace-citations.ps1
    • Click “Run with PowerShell”
    • OR open PowerShell and run: .\replace-citations.ps1
  3. Select your file - A file browser will open, select your .tex file

  4. Done! - The script creates a new file: yourfile_updated.tex


Example: Before and After

Before (input.tex)

\documentclass{article}
\begin{document}

The intersection of quantum computing and cryptographic systems presents
fascinating challenges \cite{1}\cite{2}.

Machine learning algorithms have demonstrated remarkable capabilities \cite{4},
particularly when applied to climate analysis \cite{1}.

Urban planners recognize the importance of green infrastructure \cite{3},
while conservation biologists emphasize biodiversity preservation \cite{5}.

\bibliographystyle{plain}
\bibliography{references}
\end{document}

After (input_updated.tex)

\documentclass{article}
\begin{document}

The intersection of quantum computing and cryptographic systems presents
fascinating challenges \cite{martinez2023neural}\cite{johnson2024quantum}.

Machine learning algorithms have demonstrated remarkable capabilities \cite{anderson2021machine},
particularly when applied to climate analysis \cite{martinez2023neural}.

Urban planners recognize the importance of green infrastructure \cite{patel2022sustainable},
while conservation biologists emphasize biodiversity preservation \cite{nguyen2023biodiversity}.

\bibliographystyle{plain}
\bibliography{references}
\end{document}

How It Works

1. The Hashtable (Dictionary)

$citationMap = @{
    '1' = 'martinez2023neural'
    '2' = 'johnson2024quantum'
}

Creates a lookup table. When the script finds \cite{1}, it replaces with martinez2023neural.

2. The Regex Pattern

$pattern = '\\cite\{(\d+)\}'
Pattern Part Matches
\\cite The literal text \cite
\{ Opening brace {
(\d+) One or more digits (captured)
\} Closing brace }

3. The Replacement Logic

[regex]::Replace($content, $pattern, {
    param($match)
    $number = $match.Groups[1].Value  # Extract the number
    $key = $citationMap[$number]       # Look up the key
    return "\cite{$key}"               # Replace with key
})

For each match:

  1. Extracts the number from \cite{1}1
  2. Looks up 1 in the map → martinez2023neural
  3. Replaces with \cite{martinez2023neural}

Scaling Up

For larger bibliographies (50, 100+ references), simply extend the $citationMap:

$citationMap = @{
    '1' = 'martinez2023neural'
    '2' = 'johnson2024quantum'
    '3' = 'patel2022sustainable'
    # ... add as many as you need
    '50' = 'thompson2024algorithms'
    '100' = 'williams2023framework'
}

The script handles them all just as easily!


Tips and Best Practices

TipDo’s
  • Keep backups - The script creates a new file, but always keep your original
  • Test first - Try with a small sample file before running on your full document
  • Verify mappings - Double-check that your citation numbers match your keys
  • Use descriptive keys - Keys like martinez2023neural are better than ref1
CautionDon’ts
  • Don’t use the same citation key twice in your mapping
  • Don’t forget the quotes around numbers: '1' not 1
  • Don’t panic if you see warnings - the script keeps original citations if no mapping is found

Troubleshooting

Issue: “Scripts are disabled”

Solution: Run PowerShell as Administrator and execute:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Issue: “No citations were replaced”

Possible causes:

  • Your citations don’t use the format \cite{number}
  • Numbers in citations don’t match numbers in your map
  • Check for spaces: \cite{ 1 } won’t match (remove spaces)

Issue: “Warning: No mapping for citation X”

Solution: You’re using citation number X but haven’t added it to $citationMap. Either:

  • Add the mapping
  • Or ignore it (the script leaves it unchanged)

Advanced: Combining Multiple Citations

The script handles consecutive citations automatically:

Before:

Multiple studies \cite{1}\cite{2}\cite{3} have shown...

After:

Multiple studies \cite{martinez2023neural}\cite{johnson2024quantum}\cite{patel2022sustainable} have shown...
TipPro Tip

You can combine these manually afterward:

Multiple studies \cite{martinez2023neural,johnson2024quantum,patel2022sustainable} have shown...

This renders as [1,2,3] instead of [1][2][3].


Quick Reference

Task Command/Action
Enable scripts Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Run script Right-click → “Run with PowerShell”
Alternative run .\replace-citations.ps1 in PowerShell
Output file originalname_updated.tex

Summary

NoteKey Benefits

Converting numeric citations to proper citation keys with this script:

  • Process hundreds of citations in seconds
  • Eliminate human error from manual find-replace
  • Maintain consistency across your document
  • Focus on your research instead of formatting

Once you set up the citation map, you can reuse the script for any document using the same bibliography.

Next Steps

  1. Copy the script to a .ps1 file
  2. Update the $citationMap with your citations
  3. Run it on your LaTeX file
  4. Enjoy your properly formatted citations!