def _update_image_references(tex_contents_dict, old_filename, new_filename, verbose=False):
    """Update references from old_filename to new_filename in all tex content."""
    if old_filename == new_filename:
        return  # No change needed
    
    old_base = os.path.splitext(old_filename)[0]
    new_base = os.path.splitext(new_filename)[0]
    
    if verbose:
        print(f"Updating LaTeX references: {old_filename} -> {new_filename}")
    
    for tex_file in tex_contents_dict:
        # Handle both string and list content
        if isinstance(tex_contents_dict[tex_file], list):
            content = ''.join(tex_contents_dict[tex_file])
        else:
            content = tex_contents_dict[tex_file]
        
        content_changed = False
        
        # Pattern 1: Direct filename with full extension, handling comments and newlines
        pattern1 = r'(\{(?:%\s*\n\s*)?[^}]*?)' + regex.escape(old_filename) + r'((?:%\s*\n\s*)?[^}]*?\})'
        replacement1 = r'\1' + new_filename + r'\2'
        
        new_content = regex.sub(pattern1, replacement1, content, flags=regex.IGNORECASE | regex.DOTALL)
        if new_content != content:
            content = new_content
            content_changed = True
            if verbose:
                print(f"Applied pattern 1 (full filename) in {tex_file}")
        else:
            # Pattern 2: Base filename without extension, handling comments and newlines
            # Only apply this if Pattern 1 didn't match to avoid double replacements
            pattern2 = r'(\{(?:%\s*\n\s*)?[^}]*?)' + regex.escape(old_base) + r'((?:%\s*\n\s*)?[^}]*?\})'
            replacement2 = r'\1' + new_base + r'.jpg\2'
            
            new_content = regex.sub(pattern2, replacement2, content, flags=regex.IGNORECASE | regex.DOTALL)
            if new_content != content:
                content = new_content
                content_changed = True
                if verbose:
                    print(f"Applied pattern 2 (base filename) in {tex_file}")
            else:
                # Pattern 3: Handle cases where extension is split across lines with comments
                # This specifically targets patterns like: images/filename%\n.png
                pattern3 = r'(\{[^}]*?)' + regex.escape(old_base) + r'(%\s*\n\s*)(\.png)([^}]*?\})'
                replacement3 = r'\1' + new_base + r'\2.jpg\4'
                
                new_content = regex.sub(pattern3, replacement3, content, flags=regex.IGNORECASE | regex.DOTALL)
                if new_content != content:
                    content = new_content
                    content_changed = True
                    if verbose:
                        print(f"Applied pattern 3 (split extension) in {tex_file}")
        
        # Update the content back in the appropriate format
        if content_changed:
            if isinstance(tex_contents_dict[tex_file], list):
                # Convert back to list format, preserving line endings
                tex_contents_dict[tex_file] = content.split('\n')
            else:
                tex_contents_dict[tex_file] = content
            
            if verbose:
                print(f"Updated references in {tex_file}")
    
    # Re-write the updated tex files to the output directory
    if verbose and any(tex_contents_dict.values()):
        print("Re-writing updated tex files...")
    
    return tex_contents_dict
Update references from old_filename to new_filename in all tex content.
arxiv_latex_cleaner/arxiv_latex_cleaner.py