Skip to content

Commit e8424fd

Browse files
committed
line segments
1 parent c7464f1 commit e8424fd

9 files changed

+40
-4
lines changed

lib/converters/ast-to-latex.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ const operators = {
167167
"vec": function (operands) {
168168
return "\\vec{" + operands[0] + "}";
169169
},
170+
"linesegment": function (operands) {
171+
return "\\overline{" + operands.join(" ") + "}";
172+
},
170173
"angle": function (operands, use_shorthand) {
171174
if (use_shorthand) {
172175
return "\\angle " + operands.join("");
@@ -565,7 +568,7 @@ class astToLatex {
565568
operator === 'list' ||
566569
operator === 'set' || operator === 'vector' || operator === 'altvector' ||
567570
operator === '|' || operator === ':' ||
568-
operator === 'binom' || operator === 'vec') {
571+
operator === 'binom' || operator === 'vec' || operator === 'linesegment') {
569572
return operators[operator](operands.map(function (v, i) {
570573
return this.statement(v);
571574
}.bind(this)));

lib/converters/ast-to-text.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const unicode_operators = {
7272
":": function (operands) { return operands[0] + " : " + operands[1]; },
7373
"binom": function (operands) { return "binom( " + operands[0] + ", " + operands[1] + " )"; },
7474
"vec": function (operands) { return "vec(" + operands[0] + ")"; },
75+
"linesegment": function (operands) { return "linesegment( " + operands.join(", ") + " )"; },
7576
"angle": function (operands, use_shorthand) {
7677
if (use_shorthand) {
7778
return "∠" + operands.join("");
@@ -132,6 +133,7 @@ const nonunicode_operators = {
132133
":": function (operands) { return operands[0] + " : " + operands[1]; },
133134
"binom": function (operands) { return "binom( " + operands[0] + ", " + operands[1] + " )"; },
134135
"vec": function (operands) { return "vec(" + operands[0] + ")"; },
136+
"linesegment": function (operands) { return "linesegment( " + operands.join(", ") + " )"; },
135137
"angle": function (operands, use_shorthand) {
136138
if (use_shorthand) {
137139
return "angle " + operands.join("");
@@ -571,7 +573,7 @@ class astToText {
571573
|| operator === 'list'
572574
|| operator === 'set' || operator === 'vector' || operator === 'altvector'
573575
|| operator === '|' || operator === ':'
574-
|| operator === 'binom' || operator === 'vec') {
576+
|| operator === 'binom' || operator === 'vec' || operator === 'linesegment') {
575577
return this.operators[operator](operands.map(function (v, i) {
576578
return this.statement(v);
577579
}.bind(this)));

lib/converters/latex-to-ast.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ const operatorSymbolsDefault = {
386386
'frac': { nargs: 2, substitute: "/" },
387387
'binom': { nargs: 2 },
388388
'vec': { nargs: 1 },
389+
'overline': { nargs: 1, substitute: "linesegment", remove_products: true },
389390
};
390391

391392
const unitsDefault = get_all_units();
@@ -1299,14 +1300,23 @@ class latexToAst {
12991300

13001301
let nargs = this.operatorSymbols[result].nargs;
13011302

1303+
let remove_products = this.operatorSymbols[result].remove_products;
1304+
13021305
let args = [];
13031306

13041307
for (let i = 0; i < nargs; i++) {
13051308

13061309
if (this.token.token_type === '{') {
13071310
this.advance();
13081311

1309-
args.push(this.statement({ parse_absolute_value: parse_absolute_value }));
1312+
let new_arg = this.statement({ parse_absolute_value: parse_absolute_value });
1313+
1314+
if (remove_products && Array.isArray(new_arg) && new_arg[0] === "*") {
1315+
args.push(...new_arg.slice(1))
1316+
} else {
1317+
args.push(new_arg);
1318+
}
1319+
13101320
if (this.token.token_type !== '}') {
13111321
throw new ParseError("Expecting }", this.lexer.location);
13121322
}

lib/converters/text-to-ast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ const appliedFunctionSymbolsDefault = [
390390
const functionSymbolsDefault = ['f', 'g'];
391391

392392
// operators must be given an argument
393-
const operatorSymbolsDefault = ['binom', 'vec'];
393+
const operatorSymbolsDefault = ['binom', 'vec', 'linesegment'];
394394

395395
const unitsDefault = get_all_units();
396396

spec/quick_ast-to-latex.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,14 @@ const objectsToTest = [
846846
'ast': ['vec', 'a'],
847847
'latex': '\\vec{a}',
848848
},
849+
{
850+
'ast': ['linesegment', 'A', 'B'],
851+
'latex': '\\overline{A B}',
852+
},
853+
{
854+
'ast': ['linesegment', ['prime', 'A'], ['prime', 'B']],
855+
'latex': '\\overline{A\' B\'}',
856+
},
849857
{
850858
'ast': ['apply', 'floor', 'a'],
851859
'latex': '\\left\\lfloor a \\right\\rfloor',

spec/quick_ast-to-text.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,14 @@ const objectsToTest = [
795795
'ast': ['vec', 'a'],
796796
'text': 'vec(a)',
797797
},
798+
{
799+
'ast': ['linesegment', 'A', 'B'],
800+
'text': 'linesegment( A, B )',
801+
},
802+
{
803+
'ast': ['linesegment', ['prime', 'A'], ['prime', 'B']],
804+
'text': 'linesegment( A\', B\' )',
805+
},
798806
{
799807
'ast': ['apply', 'floor', 'a'],
800808
'text': 'floor(a)',

spec/quick_latex-to-ast.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ var trees = {
489489
'\\vec ab': ["*", ["vec", "a"], "b"],
490490
'\\vec2': ["vec", 2],
491491
'\\vec2a': ["*", ["vec", 2], "a"],
492+
'\\overline{AB}': ["linesegment", "A", "B"],
493+
'\\overline{A\'B\'}': ["linesegment", ["prime", "A"], ["prime", "B"]],
492494
'\\lfloor a \\rfloor': ["apply", "floor", "a"],
493495
'\\lfloor1.5\\rfloor2': ["*", ["apply", "floor", 1.5], 2],
494496
'\\lceil a \\rceil': ["apply", "ceil", "a"],

spec/quick_text-to-ast-to-text.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ var inputs = [
304304
'nPr(x,y)',
305305
'binom(x,y)',
306306
'vec(a)',
307+
'linesegment(A,B)',
307308
'floor(a)',
308309
'ceil(a)',
309310
'round(a)',

spec/quick_text-to-ast.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ var trees = {
445445
'nCr(x,y)': ["apply", "nCr", ["tuple", "x", "y"]],
446446
'binom(x,y)': ["binom", "x", "y"],
447447
'vec(a)': ["vec", "a"],
448+
'linesegment(A,B)': ["linesegment", "A", "B"],
449+
'linesegment(A\',B\')': ["linesegment", ["prime", "A"], ["prime", "B"]],
448450
'floor(a)': ["apply", "floor", "a"],
449451
'ceil(a)': ["apply", "ceil", "a"],
450452
'round(a)': ["apply", "round", "a"],

0 commit comments

Comments
 (0)