MCPcopy Create free account
hub / github.com/KaTeX/KaTeX / alignedHandler

Function alignedHandler

src/environments/array.ts:701–786  ·  view source on GitHub ↗
(context: EnvContextLike, args: AnyParseNode[])

Source from the content-addressed store, hash-verified

699
700// Convenience function for align, align*, aligned, alignat, alignat*, alignedat.
701const alignedHandler = function(context: EnvContextLike, args: AnyParseNode[]) {
702 if (!context.envName.includes("ed")) {
703 validateAmsEnvironmentContext(context);
704 }
705 const cols: AlignSpec[] = [];
706 const isSplit = context.envName === "split";
707 const res = parseArray(context.parser,
708 {
709 cols,
710 addJot: true,
711 autoTag: isSplit ? undefined : getAutoTag(context.envName),
712 emptySingleRow: true,
713 colSeparationType: context.envName.includes("at") ? "alignat" : "align",
714 maxNumCols: isSplit ? 2 : undefined,
715 leqno: context.parser.settings.leqno,
716 },
717 "display"
718 );
719
720 // Determining number of columns.
721 // 1. If the first argument is given, we use it as a number of columns,
722 // and makes sure that each row doesn't exceed that number.
723 // 2. Otherwise, just count number of columns = maximum number
724 // of cells in each row ("aligned" mode -- isAligned will be true).
725 //
726 // At the same time, prepend empty group {} at beginning of every second
727 // cell in each row (starting with second cell) so that operators become
728 // binary. This behavior is implemented in amsmath's \start@aligned.
729 let numMaths = 0;
730 let numCols = 0;
731 const emptyGroup: ParseNode<"ordgroup"> = {
732 type: "ordgroup",
733 mode: context.mode,
734 body: [],
735 };
736 if (args[0] && args[0].type === "ordgroup") {
737 let arg0 = "";
738 for (let i = 0; i < args[0].body.length; i++) {
739 const textord = assertNodeType(args[0].body[i], "textord");
740 arg0 += textord.text;
741 }
742 numMaths = Number(arg0);
743 numCols = numMaths * 2;
744 }
745 const isAligned = !numCols;
746 res.body.forEach(function(row) {
747 for (let i = 1; i < row.length; i += 2) {
748 // Modify ordgroup node within styling node
749 const styling = assertNodeType(row[i], "styling");
750 const ordgroup = assertNodeType(styling.body[0], "ordgroup");
751 ordgroup.body.unshift(emptyGroup);
752 }
753 if (!isAligned) { // Case 1
754 const curMaths = row.length / 2;
755 if (numMaths < curMaths) {
756 throw new ParseError(
757 "Too many math in a row: " +
758 `expected ${numMaths}, but got ${curMaths}`,

Callers

nothing calls this directly

Calls 4

assertNodeTypeFunction · 0.90
parseArrayFunction · 0.85
getAutoTagFunction · 0.85

Tested by

no test coverage detected