SkillsGrove
SkillsGrove/문서 작업/libinyam/mathtranslations-skill/latex-bookmark-anchor-audit

latex-bookmark-anchor-audit

설치

$npx skills add https://github.com/libinyam/mathtranslations-skill --skill latex-bookmark-anchor-audit
$git clone --depth 1 https://github.com/libinyam/mathtranslations-skill.git /tmp/mathtranslations-skill && mkdir -p ~/.claude/skills && cp -r /tmp/mathtranslations-skill/skills/latex-bookmark-anchor-audit ~/.claude/skills/latex-bookmark-anchor-audit
이 Agent Skill을 설치해 주세요: https://github.com/libinyam/mathtranslations-skill/tree/8f4ebf796d7f29d88ca3438b6ff4926c615b733d/skills/latex-bookmark-anchor-audit — 먼저 SKILL.md와 스크립트를 읽고, 안전하다고 확인한 뒤에만 제 skills 폴더에 설치해 주세요.

요약

원문 설명 · MITAudit and repair the PDF bookmark / TOC hyperlink anchors of a LaTeX document, so that clicking an entry actually lands on that heading. Use when readers report that bookmarks or the table of contents "jump to the wrong place", when star-numbered headings (`\subsection*{...}`, `\section*{...}`, `\chapter*{...}`) are paired with `\addcontentsline`, when TOC entries appear in the wrong order or are missing, or when a translation must be checked for "every section has an exercises booklet entry". Covers the root cause (starred headings create NO hyperref anchor), the critical `\phantomsection` ordering trap that silently offsets destinations by one page, how to detect a borrowed anchor in `.toc`, the PyMuPDF verification loop, and the idempotent normalisation script for mixed legacy spellings.
아래 SKILL.md는 libinyam/mathtranslations-skill(커밋 8f4ebf7)에서 MIT 라이선스에 따라 게시한 것입니다. 본문은 변경하지 않았으며, 파일 상단의 메타데이터만 생략하고 이미지는 링크로 표시하며 매우 긴 파일은 일부만 표시합니다. Copyright 2026 MathTranslations and mathtranslations-skill contributors. 라이선스: LICENSE. 지침 파일만 표시되며 스크립트 등은 원본 저장소를 참고하세요.

Audit & repair PDF bookmark / TOC anchors

Symptom → root cause

Symptom. In the PDF, clicking a bookmark (or the same entry on the TOC page) jumps to an unrelated location — a theorem, a proof, the previous subsection — even though the page number printed in the TOC looks correct.

Root cause. A starred heading (\subsection*{习题}, \section*{Bibliographie}, …) is typeset with the \@sect-star variant: it calls neither \refstepcounter nor hyperref's anchor machinery, so it defines no destination. The \addcontentsline that follows therefore inherits whatever \@currentHref was set last — normally the anchor of the preceding theorem / proof / subsection. hyperref then writes that foreign anchor into the .toc as the 4th field of \contentsline:

\contentsline {subsection}{\numberline {}习题}{253}{theorem.5.20}     % <-- borrowed anchor

The printed page (253) is right because \addcontentsline samples \thepage at that moment; only the destination is wrong. This makes the defect easy to miss in a text-only review.

Fix

Give the heading its own destination with \phantomsection. Canonical three-line form:

\subsection*{习题}
\phantomsection
\addcontentsline{toc}{subsection}{习题}

★ The ordering trap (cost: 20 of 63 entries off by one page)

\phantomsection must come AFTER the starred heading, not before.

  • \phantomsection → \subsection*{...} → \addcontentsline : WRONG
  • \subsection*{...} → \phantomsection → \addcontentsline : correct

Why: \phantomsection records the current position immediately. If the subsequent \subsection* happens to trigger a page break (the heading is pushed to the next page), the anchor stays on the old page while \addcontentsline samples \thepage on the new page. Net effect: TOC page number correct, bookmark destination one page early — a maddening, intermittent, ~30 %-of-entries bug. Verified by rendering the target page and finding the heading there while the bookmark pointed at the previous page.

Detecting the bad state

.toc (or the chapters/*.toc written per chapter) is the ground truth. A healthy starred-heading entry ends with an anchor of the form section*.N (hyperref's \Hy@MakeCurrentHrefAuto{section*} counter). Anything else is borrowed:

grep 'contentsline {subsection}{习题}' main.toc

Bad anchors seen in the wild: theorem.*, proof.*, subsection.*, subparagraph*.*, paragraph*.*. Good: section*.N.

Normalising mixed legacy spellings (idempotent)

A real book migrates through several conventions. All of these must collapse to the canonical form — collect every heading block and rewrite it as a unit:

form what it looks like missing
A \subsection*{习题} + \addcontentsline anchor
B \subsection*{习题} alone anchor + TOC entry
C \begin{center}\textbf{习题}\end{center} anchor + TOC entry + consistent styling
D a bare \addcontentsline with no visible heading at all anchor + the heading itself
E \subsection*{习题}\addcontentsline{...} on one line anchor

Write one script that: (1) locates blocks over all five forms; (2) absorbs an immediately preceding \phantomsection into the block so re-running never doubles it; (3) emits the canonical three lines; (4) splits form E into three lines. Then assert idempotency by re-running in dry mode and expecting 0 rewritten.

Also cross-check the count: for a book where every section ends with an exercises booklet, #(exercises blocks) must equal #(\section) per chapter. A mismatch exposes a whole booklet whose heading was never typeset (check the original book for an Exercises line before assuming it is intentional).

Verification loop (PyMuPDF)

import pymupdf
doc = pymupdf.open('main.pdf')
toc = doc.get_toc(simple=False)      # [level, title, page, dest]
  • Compare each booklet bookmark against its .toc entry: the bookmark page must equal printed page + offset, where the offset is that book's constant (正文印刷页 = PDF 物理页 − 18 here). Report every mismatch with the expected value.
  • bookmarksnumbered=true means bookmark titles carry the number (7.7 Galois 理论应用简述), so match with in, not ==.
  • Levels should mirror the sectioning: chapter → 1, \section → 2, \subsection and the exercises booklet → 3.
  • The dest['to'] point is in PDF native coordinates (origin bottom-left); y ≈ 727 on A4 means ~114 pt from the top, i.e. the heading area — not the page bottom.

A clean run reads: N entries in .toc / N bookmarks / 0 mismatches / 100 % section*.N.

Environment gotchas (Windows / WorkBuddy)

  • Never inline LaTeX regexes in a bash -c "python -c ..." one-liner. Backslash collapsing silently turns \\section\{ into a non-matching pattern and the script reports "0 sections" instead of failing. Write the script to a file and run the file.
  • Preserve the source line ending when rewriting: read bytes, decide the separator from raw.count(b'\r\n'), join with it, then assert LF-only == 0 after writing. A helper that appends to memory/log files must take the separator from the target file too (some files in the same repo are CRLF, others LF).
  • Rewriting .toc-producing sources requires a full multi-pass rebuild (xelatex ×3 for the TOC/anchors to settle), then re-run the checker — a single pass will still show stale anchors.
  • If main.pdf is open in a reader, the final xdvipdfmx step fails with a write lock; close the viewer first.
라이선스 전문 보기(MIT)
MIT License

Copyright (c) 2026 MathTranslations and mathtranslations-skill contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
NOTICE 보기
# Attribution

This independent Codex skill was derived from the public workflow described in:

- MathTranslations, "Guide / 数学翻译指南"
- <https://mathtranslations.org/guide/>
- Accessed 2026-08-22

The repository paraphrases the workflow and adds an original skill structure,
review references, and a deterministic LaTeX audit script. It does not include
a copy of the site's live terminology database.

The MathTranslations template profile was also derived by inspecting the
user-supplied archive `MathTranslations-Template.c9598d4a8d56.zip` on
2026-08-22. Its SHA-256 was
`F74809A3B63F2AE1FD8C7554EF3B43D8AD8C7240969838DB928BD3CF529BF2EF`.
The archive contained `MathTranslations-Template.tex`,
`MathTranslations-Template.pdf`, and `logo.pdf`.

The MathTranslations founder and copyright holder authorized
`MathTranslations-Template.tex` and `logo.pdf` for inclusion and distribution
in this repository under the MIT License. The compiled example PDF is not
included because it is not required by the skill.

The official archive was renamed to `mathtranslations-translation-template.zip`
and re-inspected on 2026-08-23 (SHA-256
`4C2E5AD1C81646B4E8AD53F4B5FD5BAA7D477C749915E32CF52B5EE71F85F12E`). The
updated `assets/mathtranslations-translation-template.tex` derives from that
authorized archive under the same MIT-License authorization, and the earlier
`MathTranslations-Template.*` name refers to previous releases of the same
template.

MathTranslations and its website are not affiliated with or responsible for
this repository.