Skip to content

feat: add latex_figure_context_extractor_mapper operator#923

Open
liyuyi-2001 wants to merge 1 commit intodatajuicer:mainfrom
liyuyi-2001:feat/latex-figure-context-extractor-mapper
Open

feat: add latex_figure_context_extractor_mapper operator#923
liyuyi-2001 wants to merge 1 commit intodatajuicer:mainfrom
liyuyi-2001:feat/latex-figure-context-extractor-mapper

Conversation

@liyuyi-2001
Copy link

@liyuyi-2001 liyuyi-2001 commented Feb 26, 2026

Summary

Add a new mapper operator latex_figure_context_extractor_mapper that extracts figures and their citing context from LaTeX source.

What it does

  • Parses figure, figure*, wrapfigure environments
  • Handles subfigure environments and \subfigure/\subfloat commands
  • Extracts caption, label, and \includegraphics paths for each figure
  • Finds prose paragraphs that cite each figure via \ref/\cref/\Cref/\autoref
  • Fans out one paper row into N figure rows (one per figure/subfigure)
  • Drops samples that contain no figures with images

Output fields

Field Type Description
images list[str] Image paths from \includegraphics
caption str Figure caption text
label str LaTeX label string
citing_paragraphs list[str] Paragraphs that cite this figure
parent_caption str Parent figure caption (subfigures only)
parent_label str Parent figure label (subfigures only)

Changes

  • data_juicer/ops/mapper/latex_figure_context_extractor_mapper.py — operator implementation
  • data_juicer/ops/mapper/__init__.py — registration
  • data_juicer/config/config_all.yaml — config entry
  • tests/ops/mapper/test_latex_figure_context_extractor_mapper.py — 21 unit tests
  • docs/operators/mapper/latex_figure_context_extractor_mapper.md — documentation (EN/CN)

Testing

  • 21 unit tests covering: single figures, subfigure environments, subfigure/subfloat commands, nested braces, caption variants, citation matching, label boundaries, custom keys, batch fan-out, and edge cases.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @liyuyi-2001, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a powerful new data processing operator that significantly enhances the ability to extract structured information from LaTeX documents. By intelligently parsing figure environments, identifying associated captions, labels, image paths, and crucially, the textual context that cites these figures, it enables the creation of rich, figure-centric datasets. This functionality is particularly valuable for research papers and technical documents, allowing for deeper analysis and organization of visual content and its narrative integration.

Highlights

  • New Operator: LaTeX Figure Context Extractor: Introduced a new mapper operator, latex_figure_context_extractor_mapper, designed to parse LaTeX source and extract detailed information about figures and their surrounding context.
  • Comprehensive Figure Parsing: The operator supports various LaTeX figure environments (figure, figure*, wrapfigure) and subfigure commands (subfigure environment, \subfigure, \subfloat). It accurately extracts captions, labels, and \includegraphics paths, handling nested structures and different caption variants.
  • Citation Context Extraction: It identifies prose paragraphs that cite each figure using common LaTeX reference commands such as \ref, \cref, \Cref, and \autoref, including support for comma-separated label lists.
  • Data Transformation and Filtering: The operator transforms a single input LaTeX document into multiple output rows, with each row representing a distinct figure or subfigure. Samples that do not contain any figures with images are automatically dropped.
  • Configurable Output Fields: Users can customize the output field names for extracted data like caption, label, citing paragraphs, and parent figure information, enhancing flexibility for downstream processing.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • data_juicer/config/config_all.yaml
    • Added configuration entry for the new latex_figure_context_extractor_mapper operator, including default parameters for citation commands, paragraph separation, and output keys.
  • data_juicer/ops/mapper/init.py
    • Imported LatexFigureContextExtractorMapper.
    • Registered LatexFigureContextExtractorMapper in the __all__ list to make it discoverable.
  • data_juicer/ops/mapper/latex_figure_context_extractor_mapper.py
    • Added the core implementation of the LatexFigureContextExtractorMapper class, including logic for parsing LaTeX figure environments, extracting metadata, identifying citing paragraphs, and fanning out data.
  • docs/operators/mapper/latex_figure_context_extractor_mapper.md
    • Added comprehensive documentation for the new latex_figure_context_extractor_mapper operator, detailing its functionality, parameters, output fields, and providing usage examples in both English and Chinese.
  • tests/ops/mapper/test_latex_figure_context_extractor_mapper.py
    • Added a new test file with 21 unit tests to thoroughly validate the functionality of the LatexFigureContextExtractorMapper, covering various figure and subfigure scenarios, citation types, and edge cases.
Activity
  • The author, liyuyi-2001, created this pull request to add a new feature.
  • No human review comments or activities have been recorded yet for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new latex_figure_context_extractor_mapper operator, which is a significant feature for processing LaTeX documents. The implementation is comprehensive, covering various figure and subfigure environments, and is accompanied by a thorough set of unit tests. My review identified a critical bug in the logic for parsing parent figure information when subfigures are present, which could lead to incorrect data extraction. I've provided a fix for this issue. Additionally, I've included a minor suggestion to improve code conciseness. Overall, this is a great contribution once the critical issue is addressed.

Comment on lines +368 to +379
text_outside = fig_text
all_matches = sorted(
subfig_env_matches + subfig_cmd_matches
+ subfig_nocap_matches,
key=lambda m: m.start(),
reverse=True,
)
for m in all_matches:
text_outside = (
text_outside[:m.start()]
+ text_outside[m.end():]
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There is a bug in how text_outside is constructed. The loop modifies text_outside while using indices (m.start(), m.end()) that are relative to the original, unmodified fig_text. This leads to incorrect results when there are multiple subfigure matches. A more robust approach is to build the new string from the parts of the original string that are not within any match.

            # Rebuild text_outside from parts of fig_text not in any match
            all_matches = sorted(
                subfig_env_matches + subfig_cmd_matches
                + subfig_nocap_matches,
                key=lambda m: m.start(),
            )

            parts = []
            last_end = 0
            for m in all_matches:
                parts.append(fig_text[last_end:m.start()])
                last_end = m.end()
            parts.append(fig_text[last_end:])
            text_outside = ''.join(parts)

Comment on lines +583 to +588
seen = set()
merged = []
for p in parent_citing + sf_citing:
if p not in seen:
seen.add(p)
merged.append(p)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This loop for deduplicating paragraphs while preserving order can be written more concisely using dict.fromkeys. This is a more Pythonic way to achieve ordered deduplication (guaranteed from Python 3.7+).

                        merged = list(dict.fromkeys(parent_citing + sf_citing))

Add a new mapper operator that extracts figures and their citing context
from LaTeX source. It parses figure/figure*/wrapfigure environments,
handles subfigure environments and \subfigure/\subfloat commands, and
finds prose paragraphs that cite each figure via \ref/\cref/\autoref.

One input paper row fans out into N output figure rows (one per figure
or subfigure). Samples without figures are dropped.

Output fields: images, caption, label, citing_paragraphs,
parent_caption, parent_label.

Includes:
- Operator implementation with recursive nested-brace regex support
- Config entry in config_all.yaml
- Registration in mapper __init__.py
- Comprehensive unit tests (21 test cases)
- Operator documentation (EN/CN)

Made-with: Cursor
Copy link
Collaborator

@yxdyc yxdyc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz resolve the gemini suggestion & my minor comment

others LGTM.

Note: this operator expects the full LaTeX source as a single
string. It does **not** resolve ``\\input`` or ``\\include``
directives. If your documents span multiple ``.tex`` files,
concatenate them into a single text field before applying this
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a dedicated op for this step? "If your documents span multiple .tex files,
concatenate them into a single text field before applying this"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants