package tex
import (
"strconv"
"github.com/d2lang/mathjax-go/internal/mml"
)
type amsFlalignLayout struct {
name string
entries float64
padded bool
center bool
zeroLabel bool
maxColumns int
}
func newAMSFlalignLayout(name , count string ) *amsFlalignLayout {
layout := &amsFlalignLayout {name : name , center : !isAMSXAlignAt (name ), zeroLabel : isAMSXAlignAt (name )}
if isAMSXAlignAt (name ) {
layout .padded = name != "xxalignat"
if count != "" {
value , _ := strconv .ParseFloat (count , 64 )
layout .entries = 2 * value
}
}
return layout
}
func (layout *amsFlalignLayout ) endEntry (entries int ) error {
if layout .entries != 0 && float64 (entries ) > layout .entries {
return texError ("XalignOverflow" , "Extra & in row of %s" , layout .name )
}
return nil
}
func (layout *amsFlalignLayout ) endRow (row *mml .Node ) {
entries := append ([]*mml .Node (nil ), row .Children ...)
for float64 (len (entries )) < layout .entries {
entries = append (entries , node ("mtd" ))
}
cells := make ([]*mml .Node , 0 , len (entries )+len (entries )/2 +2 )
if layout .padded {
cells = append (cells , node ("mtd" ))
}
for len (entries ) != 0 {
cells = append (cells , entries [0 ])
entries = entries [1 :]
if len (entries ) != 0 {
cells = append (cells , entries [0 ])
entries = entries [1 :]
}
if len (entries ) != 0 || layout .padded {
cells = append (cells , node ("mtd" ))
}
}
row .SetChildren (cells )
if len (cells ) > layout .maxColumns {
layout .maxColumns = len (cells )
}
}
func (layout *amsFlalignLayout ) label (tag *mml .Node ) *mml .Node {
if layout .zeroLabel {
padded := node ("mpadded" , tag .Children ...)
padded .Attributes .Set ("width" , 0 )
padded .Attributes .Set ("lspace" , "-1width" )
tag .SetChildren ([]*mml .Node {forcedRow ([]*mml .Node {padded }, true )})
}
return tag
}
func (layout *amsFlalignLayout ) endTable (table *mml .Node ) {
align , width , labelSpacing := "right left center" , "auto auto fit" , "0.8em"
if layout .padded {
align , width = "center right left" , "fit auto auto"
}
if layout .zeroLabel {
labelSpacing = "0"
}
resetTableAttributes (table ,
"width" , "100%" ,
"displaystyle" , true ,
"columnalign" , repeatAMSEqnArrayDefinition (align , layout .maxColumns ),
"columnspacing" , "0em" ,
"columnwidth" , repeatAMSEqnArrayDefinition (width , layout .maxColumns ),
"rowspacing" , "3pt" ,
"side" , "right" ,
"minlabelspacing" , labelSpacing ,
"data-width-includes-label" , true ,
)
if layout .center && layout .maxColumns <= 2 {
table .Attributes .Explicit ().Delete ("width" )
}
}
func (p *parser ) amsFlalignEnvironment (name string ) ([]*mml .Node , error ) {
count := ""
if isAMSXAlignAt (name ) {
var err error
count , err = p .readEquationPairCount (name )
if err != nil {
return nil , err
}
}
if err := p .checkEquationEnvironment (); err != nil {
return nil , err
}
layout := newAMSFlalignLayout (name , count )
tags := p .amsTags ()
numbered := name == "flalign" || name == "xalignat"
tags .start (name , numbered , numbered )
defer tags .end ()
body , err := p .captureEnvironment (name )
if err != nil {
return nil , err
}
table := node ("mtable" )
var entries []*mml .Node
spacing := &arrayRowSpacing {}
err = p .parseArrayBody (body , arrayBodyOwner {
rules : newArrayRules (table ),
hasEntries : func () bool { return len (entries ) != 0 },
endEntry : func (children []*mml .Node , _ *arrayCellState ) error {
if len (entries ) != 0 {
children = fixInitialMO (children )
}
entries = append (entries , node ("mtd" , matrixCellContent (children )))
return layout .endEntry (len (entries ))
},
endRow : func () error {
row := node ("mtr" , entries ...)
layout .endRow (row )
tag , err := tags .getTag (p )
if err != nil {
return err
}
if tag != nil {
row = node ("mlabeledtr" , append ([]*mml .Node {layout .label (tag )}, row .Children ...)...)
}
tags .clearTag ()
table .AppendChild (row )
entries = nil
spacing .rows ++
return nil
},
addSpacing : spacing .add ,
})
if err != nil {
return nil , err
}
layout .endTable (table )
spacing .apply (table , "3pt" )
return []*mml .Node {finishArrayRules (table )}, nil
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .