@@ -10,10 +10,11 @@ import findtextindex from './findtextindex.js';
1010export default function latexwrap ( lines ) {
1111 const output = [ `\\[\\begin{align*}` ] ;
1212 for ( let str of lines ) {
13+ str = str . trim ( ) ;
1314 // 3. Find first occurrence of \text{ and bump that to the next column.
1415 const matchtxt = findtextindex ( str , [ '\\text{' ] , [ `\\text{or}` , `\\text{and}` , `\\text{if}` ] ) ;
1516 if ( matchtxt ) {
16- str = str . slice ( 0 , matchtxt ) + ' & &' + str . slice ( matchtxt ) ;
17+ str = str . slice ( 0 , matchtxt ) + " & &" + str . slice ( matchtxt ) ;
1718 }
1819 // 2. Find first occurrence of equals, inequality etc. and bump rest to the next column.
1920 const bracest = [
@@ -26,18 +27,25 @@ export default function latexwrap(lines) {
2627 const matcheq = findtextindex ( str , braces ) ;
2728 // Zero vs false issue.
2829 if ( matcheq !== false ) {
29- str = str . slice ( 0 , matcheq ) + '&' + str . slice ( matcheq ) ;
30+ str = str . slice ( 0 , matcheq ) + "&" + str . slice ( matcheq ) ;
3031 } else {
31- str = str + `&` ;
32+ str = str + "&" ;
3233 }
3334 // 1. If we _start_ with an implies, therefore, etc. put it in col 1.
35+ // If we have a "\\displaystyle" at the start then it kills step (1) below.
36+ str = str . trim ( ) ;
37+ var displaystyle = "" ;
38+ if ( str . startsWith ( "\\displaystyle" ) ) {
39+ str = str . slice ( "\\displaystyle" . length ) ;
40+ displaystyle = "\\displaystyle" ;
41+ }
3442 const imptxt = [ `Rightarrow` , `Leftarrow` , `Leftrightarrow` , `therefore` , `because` ] ;
35- const imptxttk = imptxt . flatMap ( token => [ `\\${ token } {` , `\\${ token } ` ] ) ;
43+ const imptxttk = imptxt . flatMap ( token => [ `\\${ token } {` , `\\${ token } ` , `\\ ${ token } \\` ] ) ;
3644 const matchimp = imptxttk . find ( token => str . startsWith ( token ) ) ;
3745 if ( matchimp === undefined ) {
38- str = ` & & ` + str ;
46+ str = " & &" + displaystyle + str ;
3947 } else {
40- str = str . slice ( 0 , matchimp . length ) + ' & &' + str . slice ( matchimp . length ) ;
48+ str = str . slice ( 0 , matchimp . length - 1 ) + " & &" + displaystyle + str . slice ( matchimp . length - 1 ) ;
4149 }
4250 str += `\\\\` ;
4351 output . push ( str ) ;
0 commit comments