@@ -368,6 +368,7 @@ function pdfLatexPostProcessor(
368368 lineProcessors . push ( captionFootnoteLineProcessor ( ) ) ;
369369 lineProcessors . push ( codeAnnotationPostProcessor ( ) ) ;
370370 lineProcessors . push ( codeListAnnotationPostProcessor ( ) ) ;
371+ lineProcessors . push ( longTableSidenoteProcessor ( ) ) ;
371372
372373 await processLines ( output , lineProcessors , temp ) ;
373374 if ( Object . keys ( renderedCites ) . length > 0 ) {
@@ -619,6 +620,85 @@ const captionFootnoteLineProcessor = () => {
619620 } ;
620621} ;
621622
623+ const processLongTableSidenotes = ( latexLongTable : string ) => {
624+ const sideNoteMarker = "\\sidenote{\\footnotesize " ;
625+ let strProcessing = latexLongTable ;
626+ const strOutput : string [ ] = [ ] ;
627+ const sidenotes : string [ ] = [ ] ;
628+
629+ let sidenotePos = strProcessing . indexOf ( sideNoteMarker ) ;
630+ while ( sidenotePos > - 1 ) {
631+ strOutput . push ( strProcessing . substring ( 0 , sidenotePos ) ) ;
632+
633+ const remainingStr = strProcessing . substring (
634+ sidenotePos + sideNoteMarker . length ,
635+ ) ;
636+ let escaped = false ;
637+ let sideNoteEnd = - 1 ;
638+ for ( let i = 0 ; i < remainingStr . length ; i ++ ) {
639+ const ch = remainingStr [ i ] ;
640+ if ( ch === "\\" ) {
641+ escaped = true ;
642+ } else {
643+ if ( ! escaped && ch === "}" ) {
644+ sideNoteEnd = i ;
645+ break ;
646+ } else {
647+ escaped = false ;
648+ }
649+ }
650+ }
651+
652+ if ( sideNoteEnd > - 1 ) {
653+ strOutput . push ( "\\sidenotemark{}" ) ;
654+ const contents = remainingStr . substring ( 0 , sideNoteEnd ) ;
655+ sidenotes . push ( contents ) ;
656+ strProcessing = remainingStr . substring ( sideNoteEnd + 1 ) ;
657+ sidenotePos = strProcessing . indexOf ( sideNoteMarker ) ;
658+ } else {
659+ strOutput . push ( remainingStr ) ;
660+ }
661+ }
662+ strOutput . push ( strProcessing ) ;
663+
664+ for ( const note of sidenotes ) {
665+ strOutput . push ( `\\sidenotetext{${ note } }\n` ) ;
666+ }
667+
668+ return strOutput . join ( "" ) ;
669+ } ;
670+
671+ const longTableSidenoteProcessor = ( ) => {
672+ let state : "scanning" | "capturing" = "scanning" ;
673+ let capturedLines : string [ ] = [ ] ;
674+ return ( line : string ) : string | undefined => {
675+ switch ( state ) {
676+ case "scanning" :
677+ if ( line . match ( / ^ \\ b e g i n { longtable} .* $ / ) ) {
678+ state = "capturing" ;
679+ capturedLines = [ line ] ;
680+ return undefined ;
681+ } else {
682+ return line ;
683+ }
684+ case "capturing" :
685+ capturedLines . push ( line ) ;
686+ if ( line . match ( / ^ \\ e n d { longtable} .* $ / ) ) {
687+ state = "scanning" ;
688+
689+ // read the whole figure and clear any capture state
690+ const lines = capturedLines . join ( "\n" ) ;
691+ capturedLines = [ ] ;
692+
693+ // Process the captions and relocate footnotes
694+ return processLongTableSidenotes ( lines ) ;
695+ } else {
696+ return undefined ;
697+ }
698+ }
699+ } ;
700+ } ;
701+
622702const calloutFigureHoldLineProcessor = ( ) => {
623703 let state : "scanning" | "replacing" = "scanning" ;
624704 return ( line : string ) : string | undefined => {
0 commit comments