@@ -75,7 +75,10 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
75
75
public _complete_request ?: number ;
76
76
public store : JupyterStore ;
77
77
public syncdb : SyncDB ;
78
- private labels ?: { [ label : string ] : { tag : string ; id : string } } ;
78
+ private labels ?: {
79
+ math : { [ label : string ] : { tag : string ; id : string } } ;
80
+ fig : { [ label : string ] : { tag : string ; id : string } } ;
81
+ } ;
79
82
80
83
public _init (
81
84
project_id : string ,
@@ -2733,8 +2736,9 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
2733
2736
value = latexEnvs ( value ) ;
2734
2737
2735
2738
const labelRegExp = / \s * \\ l a b e l \{ .* ?\} \s * / g;
2739
+ const figLabelRegExp = / \s * \\ f i g l a b e l \{ .* ?\} \s * / g;
2736
2740
if ( this . labels == null ) {
2737
- const labels = ( this . labels = { } ) ;
2741
+ const labels = ( this . labels = { math : { } , fig : { } } ) ;
2738
2742
// do initial full document scan
2739
2743
if ( this . store == null ) {
2740
2744
return ;
@@ -2743,15 +2747,23 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
2743
2747
if ( cells == null ) {
2744
2748
return ;
2745
2749
}
2746
- let n = 0 ;
2750
+ let mathN = 0 ;
2751
+ let figN = 0 ;
2747
2752
for ( const id of this . store . get_cell_ids_list ( ) ) {
2748
2753
const cell = cells . get ( id ) ;
2749
2754
if ( cell ?. get ( "cell_type" ) == "markdown" ) {
2750
- const value = cell . get ( "input" ) ?? "" ;
2755
+ const value = latexEnvs ( cell . get ( "input" ) ?? "" ) ;
2751
2756
value . replace ( labelRegExp , ( labelContent ) => {
2752
2757
const label = extractLabel ( labelContent ) ;
2753
- n += 1 ;
2754
- labels [ label ] = { tag : `${ n } ` , id } ;
2758
+ mathN += 1 ;
2759
+ labels . math [ label ] = { tag : `${ mathN } ` , id } ;
2760
+ return "" ;
2761
+ } ) ;
2762
+ value . replace ( figLabelRegExp , ( labelContent ) => {
2763
+ const label = extractLabel ( labelContent ) ;
2764
+ figN += 1 ;
2765
+ labels . fig [ label ] = { tag : `${ figN } ` , id } ;
2766
+ return "" ;
2755
2767
} ) ;
2756
2768
}
2757
2769
}
@@ -2760,28 +2772,38 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
2760
2772
if ( labels == null ) {
2761
2773
throw Error ( "bug" ) ;
2762
2774
}
2763
- const noLabels = value . replace ( labelRegExp , ( labelContent ) => {
2775
+ value = value . replace ( labelRegExp , ( labelContent ) => {
2776
+ const label = extractLabel ( labelContent ) ;
2777
+ if ( labels . math [ label ] == null ) {
2778
+ labels . math [ label ] = { tag : `${ misc . len ( labels . math ) + 1 } ` , id } ;
2779
+ } else {
2780
+ // in case it moved to a different cell due to cut/paste
2781
+ labels . math [ label ] . id = id ;
2782
+ }
2783
+ return `\\tag{${ labels . math [ label ] . tag } }` ;
2784
+ } ) ;
2785
+ value = value . replace ( figLabelRegExp , ( labelContent ) => {
2764
2786
const label = extractLabel ( labelContent ) ;
2765
- if ( labels [ label ] == null ) {
2766
- labels [ label ] = { tag : `${ misc . len ( labels ) + 1 } ` , id } ;
2787
+ if ( labels . fig [ label ] == null ) {
2788
+ labels . fig [ label ] = { tag : `${ misc . len ( labels . fig ) + 1 } ` , id } ;
2767
2789
} else {
2768
2790
// in case it moved to a different cell due to cut/paste
2769
- labels [ label ] . id = id ;
2791
+ labels . fig [ label ] . id = id ;
2770
2792
}
2771
- return `\\tag{ ${ labels [ label ] . tag } }` ;
2793
+ return ` ${ labels . fig [ label ] . tag ?? "?" } ` ;
2772
2794
} ) ;
2773
2795
const refRegExp = / \\ r e f \{ .* ?\} / g;
2774
- const noRefs = noLabels . replace ( refRegExp , ( refContent ) => {
2796
+ value = value . replace ( refRegExp , ( refContent ) => {
2775
2797
const label = extractLabel ( refContent ) ;
2776
- if ( labels [ label ] == null ) {
2777
- // nothing to do but strip it
2778
- return "" ;
2798
+ if ( labels . fig [ label ] == null && labels . math [ label ] == null ) {
2799
+ // do not know the label
2800
+ return "? " ;
2779
2801
}
2780
- const { tag, id } = labels [ label ] ;
2802
+ const { tag, id } = labels . fig [ label ] ?? labels . math [ label ] ;
2781
2803
return `[${ tag } ](#id=${ id } )` ;
2782
2804
} ) ;
2783
2805
2784
- return noRefs ;
2806
+ return value ;
2785
2807
} ;
2786
2808
}
2787
2809
0 commit comments