Skip to content

Conversation

@Tom-van-Woudenberg
Copy link
Member

@Tom-van-Woudenberg Tom-van-Woudenberg commented Jan 15, 2026

Renamed branch name, replaces #48

claude and others added 10 commits December 23, 2025 12:31
Implements automatic BibTeX entry generation from figure metadata:

- Add configuration options for BibTeX generation:
  - generate_bib: Enable/disable BibTeX generation (default: false)
  - output_file: Output .bib file path (default: references.bib)
  - overwrite_existing: Overwrite existing entries (default: false)

- Implement _generate_bib_entry() function:
  - Converts figure metadata to @misc BibTeX format
  - Supports author, title, date/year, url/howpublished, license (in note), copyright
  - Handles URLs, markdown links, and plain text sources

- Implement _write_bib_entry() function:
  - Writes BibTeX entries to configured output file
  - Checks for existing entries to avoid duplicates
  - Supports overwriting existing entries when configured

- Integrate generation into MetadataFigure.run():
  - Generates BibTeX when :bib: option is specified with metadata
  - Extracts caption from figure node for title field
  - Only generates if metadata is present

- Update documentation:
  - README: Add BibTeX generation configuration and usage examples
  - Includes complete example showing metadata-to-BibTeX conversion

This completes Option 1 from Issue #16, complementing the existing
Option 3 (BibTeX extraction) from PR #34.
Updated the citation text format from ':cite:empty:`{bib_key}`' to '{cite:empty}`{bib_key}`' to ensure correct parsing when adding to the bibliography.
Implements the hybrid approach for BibTeX synchronization:

1. Cache injection: Injects generated entries directly into
   sphinxcontrib-bibtex's internal cache using pybtex, enabling
   same-build citation support without requiring a rebuild.

2. File persistence: Also writes entries to .bib file for
   future builds and version control.

3. Graceful fallback: If cache injection fails (API changes,
   pybtex unavailable), warns user and file is still written.

New configuration options:
- generate_bib: Enable BibTeX generation (default: false)
- output_file: Output .bib file path (default: references.bib)
- overwrite_existing: Overwrite existing entries (default: false)

This solves the "bibtex-sync challenge" from PR #38 review where
generated entries weren't recognized until a second build.
Resolves merge conflicts by incorporating:
- All features from main (page-level defaults, license translations, etc.)
- BibTeX generation with cache injection for single-build support

The cache injection solution addresses the reviewer feedback about
requiring a second build. Generated entries are now injected directly
into sphinxcontrib-bibtex's cache, enabling same-build citations.
Changed the priority order for resolving figure metadata fields so that BibTeX metadata is now checked before page-level defaults. Updated both the code logic and documentation to reflect this new precedence for license, date, author, copyright, and source fields.
Refines the logic for handling unrecognized BibTeX keys by checking the 'generate_bib' setting before displaying a message. Also includes minor whitespace and formatting adjustments for clarity.
…ie7Gv

Review project and investigate bibtext-sync issue
The cache injection was failing because it was looking for the wrong
attribute name. Changed from env.bibtex_bibfiles to env.bibtex_cache.bibfiles
to match the actual sphinxcontrib-bibtex cache structure.

Changes:
- Fix cache attribute: env.bibtex_cache instead of env.bibtex_bibfiles
- Access bibfiles through cache.bibfiles
- Add detailed debug logging to track cache access
- Improve error messages with actionable guidance
- Change successful injection logging from debug to info level
- Add traceback logging for exceptions

This enables single-build citation of auto-generated BibTeX entries.

Fixes the issue reported in PR #45 review where cache was never found.
@Tom-van-Woudenberg
Copy link
Member Author

@lkdmc ,

I've tested it in the manual, but it still gives a warning that it requires a rebuild: https://github.com/TeachBooks/manual/actions/runs/21023441945/job/60442569461#step:10:282 and the built book doesn't contain the bib entry: https://teachbooks.io/manual/bib/references.html (it only contains the entry used by another figure: https://teachbooks.io/manual/bib/_git/github.com_TeachBooks_Sphinx-Metadata-Figure/claude-add-bib-entry-support-R6c2I/MANUAL.html#example-11-metadata-with-bibtex-extraction

Do you see an option how this could work? If so, could you test it first in this manual page? Please let me know if you need help with that!

Furthermore, @douden , shouldn't the priority order be bib at 2? Now because of the bib entry we get the local page defaults although that's not desired: https://teachbooks.io/manual/bib/_git/github.com_TeachBooks_Sphinx-Metadata-Figure/claude-add-bib-entry-support-R6c2I/MANUAL.html#tb-logo-metadata13

@Tom-van-Woudenberg
Copy link
Member Author

If we really cannot fix this in a single build. would it be an idea to do this in the teachbooks package as part of pre-processing? @douden?

@Tom-van-Woudenberg
Copy link
Member Author

Tested here too: https://oit.tudelft.nl/CTB1310/test_bib/shear/instructie.html
Doesn't create a bib entry to show up in the references: https://oit.tudelft.nl/CTB1310/test_bib/references.html

@douden
Copy link
Member

douden commented Jan 15, 2026

Furthermore, @douden , shouldn't the priority order be bib at 2? Now because of the bib entry we get the local page defaults although that's not desired: https://teachbooks.io/manual/bib/_git/github.com_TeachBooks_Sphinx-Metadata-Figure/claude-add-bib-entry-support-R6c2I/MANUAL.html#tb-logo-metadata13

Yes, priority should be 2, which it is in the latest release. Already fixed that.

@douden
Copy link
Member

douden commented Jan 15, 2026

If we really cannot fix this in a single build. would it be an idea to do this in the teachbooks package as part of pre-processing? @douden?

Could be am option, but that means we have to write our own complete myst-parser, which can be very buggy. And this is not really a thing of TeachBooks, but of this extension.

If possible, it could be triggered on the source-read event, if that is before the bibtex load. But that should be researched.

@Tom-van-Woudenberg
Copy link
Member Author

Yes, priority should be 2, which it is in the latest release. Already fixed that.

Apparantely it isn't because we get the metadata of the local page instead of the bib entry.

I think there should be the option for the default-metadata-page to do bib creation and extraction too, although that should be overruled when a bib entry is used for creation or extraction on a single figure.

@douden
Copy link
Member

douden commented Jan 15, 2026

Apparantely it isn't because we get the metadata of the local page instead of the bib entry.

For a new bib entry that makes sense, as the key did not exist, so no information was extracted from the cache. For existing entries it works as it should.

I think there should be the option for the default-metadata-page to do bib creation and extraction too, although that should be overruled when a bib entry is used for creation or extraction on a single figure.

That is possible, but still the same restriction applies with the bibtex cache. So that should be fixed first.

@douden
Copy link
Member

douden commented Jan 15, 2026

@lkdmc : let me know using a mention when you have found a fix for the caching problem. I will review then.

@douden douden removed their request for review January 15, 2026 08:42
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.

5 participants