|
| 1 | +// Beamer export module with TikZ arrows (presentation format) |
| 2 | +// Based on texample.net/tikz/examples/beamer-arrows/ pattern |
| 3 | + |
| 4 | +import type { ParsedContent } from '../parser'; |
| 5 | +import type { ColorScheme } from './types'; |
| 6 | +import { transformHtmlClass } from '../utils/latex-parser'; |
| 7 | +import { convertHtmlDescription } from '../utils/html-converter'; |
| 8 | +import { escapePreservingMath, escapeLaTeX } from '../utils/escape'; |
| 9 | +import { getTermColor } from '../utils/color-utils'; |
| 10 | + |
| 11 | +// Escape LaTeX text while preserving inline math ($...$) |
| 12 | +const escapeLatexPreservingMath = (text: string) => escapePreservingMath(text, escapeLaTeX); |
| 13 | + |
| 14 | +// Inject TikZ coordinate nodes into LaTeX equation |
| 15 | +function injectTikzNodesInLatex(latex: string): { latex: string; nodeCount: number } { |
| 16 | + let nodeCount = 0; |
| 17 | + const result = transformHtmlClass(latex, (className, content, index) => { |
| 18 | + nodeCount++; |
| 19 | + return `\\textcolor{term${className}}{${content}}\\tikz[baseline,remember picture,overlay] \\coordinate (n${index});`; |
| 20 | + }); |
| 21 | + return { latex: result, nodeCount }; |
| 22 | +} |
| 23 | + |
| 24 | +// Convert HTML description to LaTeX text |
| 25 | +function convertDescriptionToLatex(html: string): string { |
| 26 | + return convertHtmlDescription( |
| 27 | + html, |
| 28 | + escapeLaTeX, |
| 29 | + (className, content) => `\\textcolor{term${className}}{${escapeLaTeX(content)}}` |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Export to Beamer with TikZ arrows (presentation format) |
| 35 | + * Based on texample.net/tikz/examples/beamer-arrows/ pattern |
| 36 | + */ |
| 37 | +export function exportToBeamer( |
| 38 | + content: ParsedContent, |
| 39 | + colorScheme: ColorScheme |
| 40 | +): string { |
| 41 | + // Generate color definitions for preamble |
| 42 | + const colorDefinitions = content.termOrder |
| 43 | + .map((className) => { |
| 44 | + const color = getTermColor(className, content.termOrder, colorScheme); |
| 45 | + const latexColorName = `term${className}`; |
| 46 | + const hexColor = color.replace('#', '').toUpperCase(); |
| 47 | + return `\\definecolor{${latexColorName}}{HTML}{${hexColor}}`; |
| 48 | + }) |
| 49 | + .join('\n'); |
| 50 | + |
| 51 | + // Convert LaTeX with TikZ nodes at each term |
| 52 | + const { latex: equationWithNodes } = injectTikzNodesInLatex(content.latex); |
| 53 | + |
| 54 | + // Convert description |
| 55 | + const descriptionLatex = convertDescriptionToLatex(content.description); |
| 56 | + |
| 57 | + // Generate individual frames for each term with arrow |
| 58 | + const definitionFrames = Array.from(content.definitions.entries()) |
| 59 | + .map(([className, definition], index) => { |
| 60 | + const latexColorName = `term${className}`; |
| 61 | + const nodeId = `def${index}`; |
| 62 | + const equationNodeId = `n${index}`; |
| 63 | + const definitionLatex = escapeLatexPreservingMath(definition); |
| 64 | + |
| 65 | + return `\\begin{frame}<${index + 2}>[label=term${index}] |
| 66 | +\\frametitle{${escapeLaTeX(content.title!)}} |
| 67 | +
|
| 68 | +\\begin{equation*} |
| 69 | +${equationWithNodes} |
| 70 | +\\end{equation*} |
| 71 | +
|
| 72 | +\\vspace{0.5em} |
| 73 | +
|
| 74 | +\\begin{block}{} |
| 75 | +\\tikz[na] \\node[coordinate] (${nodeId}) {}; |
| 76 | +${definitionLatex} |
| 77 | +\\end{block} |
| 78 | +
|
| 79 | +% Draw clean arrow from term to definition (offset left to approximate center, down for margin) |
| 80 | +\\begin{tikzpicture}[overlay,remember picture] |
| 81 | + \\draw[->,${latexColorName},line width=1.5pt,rounded corners=5pt] ($(${equationNodeId})+(-0.3em,-0.2em)$) -- ++(0,-0.6) -| (${nodeId}); |
| 82 | +\\end{tikzpicture} |
| 83 | +
|
| 84 | +\\end{frame}`; |
| 85 | + }) |
| 86 | + .join('\n\n'); |
| 87 | + |
| 88 | + return `\\documentclass{beamer} |
| 89 | +
|
| 90 | +% Beamer theme |
| 91 | +\\usetheme{default} |
| 92 | +\\setbeamertemplate{navigation symbols}{} |
| 93 | +
|
| 94 | +% TikZ for arrows |
| 95 | +\\usepackage{tikz} |
| 96 | +\\usetikzlibrary{arrows,shapes,calc} |
| 97 | +\\tikzstyle{every picture}+=[remember picture] |
| 98 | +\\tikzstyle{na} = [baseline=-.5ex] |
| 99 | +
|
| 100 | +% Colors and math |
| 101 | +\\usepackage{amsmath} |
| 102 | +\\usepackage{xcolor} |
| 103 | +
|
| 104 | +% Define colors from scheme |
| 105 | +${colorDefinitions} |
| 106 | +
|
| 107 | +\\title{${escapeLaTeX(content.title!)}} |
| 108 | +\\date{} |
| 109 | +
|
| 110 | +\\begin{document} |
| 111 | +
|
| 112 | +\\begin{frame} |
| 113 | +\\titlepage |
| 114 | +\\end{frame} |
| 115 | +
|
| 116 | +\\begin{frame}[label=overview] |
| 117 | +\\frametitle{Equation} |
| 118 | +
|
| 119 | +\\begin{equation*} |
| 120 | +${equationWithNodes} |
| 121 | +\\end{equation*} |
| 122 | +
|
| 123 | +\\vspace{1em} |
| 124 | +
|
| 125 | +${descriptionLatex} |
| 126 | +
|
| 127 | +\\end{frame} |
| 128 | +
|
| 129 | +${definitionFrames} |
| 130 | +
|
| 131 | +\\end{document}`; |
| 132 | +} |
0 commit comments