mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-18 17:34:43 -04:00
LaTeX export seems to work
This commit is contained in:
parent
a1ad048bda
commit
bf46f84a36
@ -18,7 +18,7 @@ public final class LaTeXFormatter {
|
|||||||
* @return the formatted string
|
* @return the formatted string
|
||||||
*/
|
*/
|
||||||
public static String format(Text text) {
|
public static String format(Text text) {
|
||||||
return format(text.enforceMath(), false);
|
return format(text, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String format(Text text, boolean mathMode) {
|
private static String format(Text text, boolean mathMode) {
|
||||||
@ -37,18 +37,29 @@ public final class LaTeXFormatter {
|
|||||||
else
|
else
|
||||||
return "$" + format(d.getContent(), true) + "$";
|
return "$" + format(d.getContent(), true) + "$";
|
||||||
case OVERLINE:
|
case OVERLINE:
|
||||||
return "\\overline{" + format(d.getContent(), mathMode) + "}";
|
if (mathMode)
|
||||||
|
return "\\overline{" + format(d.getContent(), true) + "}";
|
||||||
|
else {
|
||||||
|
final Text c = d.getContent();
|
||||||
|
if (c instanceof Index)
|
||||||
|
return "$\\overline{" + format(c, true) + "}$";
|
||||||
|
else
|
||||||
|
return "$\\overline{\\mbox{" + format(c, false) + "}}$";
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return format(d.getContent(), mathMode);
|
return format(d.getContent(), mathMode);
|
||||||
}
|
}
|
||||||
} else if (text instanceof Index) {
|
} else if (text instanceof Index) {
|
||||||
Index i = (Index) text;
|
Index i = (Index) text;
|
||||||
String str = format(i.getVar(), mathMode);
|
String str = format(i.getVar(), true);
|
||||||
if (i.getSuperScript() != null)
|
if (i.getSuperScript() != null)
|
||||||
str += '^' + brace(format(i.getSuperScript(), mathMode));
|
str += '^' + brace(format(i.getSuperScript(), true));
|
||||||
if (i.getSubScript() != null)
|
if (i.getSubScript() != null)
|
||||||
str += '_' + brace(format(i.getSubScript(), mathMode));
|
str += '_' + brace(format(i.getSubScript(), true));
|
||||||
return str;
|
if (mathMode)
|
||||||
|
return str;
|
||||||
|
else
|
||||||
|
return "$" + str + "$";
|
||||||
} else if (text instanceof Sentence) {
|
} else if (text instanceof Sentence) {
|
||||||
Sentence s = (Sentence) text;
|
Sentence s = (Sentence) text;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@ -61,9 +72,15 @@ public final class LaTeXFormatter {
|
|||||||
private static String character(char aChar, boolean inMath) {
|
private static String character(char aChar, boolean inMath) {
|
||||||
switch (aChar) {
|
switch (aChar) {
|
||||||
case '\u00AC':
|
case '\u00AC':
|
||||||
return "\\neg{}";
|
if (inMath)
|
||||||
|
return "\\neg{}";
|
||||||
|
else
|
||||||
|
return "$\\neg{}$";
|
||||||
case '\u2265':
|
case '\u2265':
|
||||||
return "\\geq\\!\\!{}";
|
if (inMath)
|
||||||
|
return "\\geq\\!\\!{}";
|
||||||
|
else
|
||||||
|
return "$\\geq\\!\\!{}$";
|
||||||
case '<':
|
case '<':
|
||||||
if (inMath)
|
if (inMath)
|
||||||
return "" + aChar;
|
return "" + aChar;
|
||||||
|
@ -17,11 +17,6 @@ public final class Blank implements Text {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Text enforceMath() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return " ";
|
return " ";
|
||||||
|
@ -20,14 +20,6 @@ public class Character implements Text {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Text enforceMath() {
|
|
||||||
if (aChar == '≥' || aChar == '¬')
|
|
||||||
return new Decorate(this, Decorate.Style.MATH);
|
|
||||||
else
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "" + aChar;
|
return "" + aChar;
|
||||||
|
@ -58,14 +58,6 @@ public class Decorate implements Text {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Text enforceMath() {
|
|
||||||
if (!style.equals(Style.OVERLINE))
|
|
||||||
return this;
|
|
||||||
else
|
|
||||||
return new Decorate(this, Style.MATH);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Decorate{" + content + ", " + style + '}';
|
return "Decorate{" + content + ", " + style + '}';
|
||||||
|
@ -48,11 +48,6 @@ public class Index implements Text {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Text enforceMath() {
|
|
||||||
return new Decorate(this, Decorate.Style.MATH);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = var.toString();
|
String s = var.toString();
|
||||||
|
@ -38,12 +38,6 @@ public class Sentence implements Text, Iterable<Text> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Text enforceMath() {
|
|
||||||
list.replaceAll(Text::enforceMath);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
@ -31,11 +31,6 @@ public class Simple implements Text {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Text enforceMath() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return text;
|
return text;
|
||||||
|
@ -8,9 +8,4 @@ public interface Text {
|
|||||||
* @return a simplified text
|
* @return a simplified text
|
||||||
*/
|
*/
|
||||||
Text simplify();
|
Text simplify();
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the text fragment in math mode if necessary
|
|
||||||
*/
|
|
||||||
Text enforceMath();
|
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ public class GraphicSVGLaTeXTest extends TestCase {
|
|||||||
assertEquals("\\&", gs.formatText("&", Style.NORMAL));
|
assertEquals("\\&", gs.formatText("&", Style.NORMAL));
|
||||||
assertEquals("$\\geq\\!\\!{}$1", gs.formatText("\u22651", Style.NORMAL));
|
assertEquals("$\\geq\\!\\!{}$1", gs.formatText("\u22651", Style.NORMAL));
|
||||||
assertEquals("$\\geq\\!\\!{}1$", gs.formatText("$\u22651$", Style.NORMAL));
|
assertEquals("$\\geq\\!\\!{}1$", gs.formatText("$\u22651$", Style.NORMAL));
|
||||||
assertEquals("$\\overline{Q}$", gs.formatText("~Q", Style.NORMAL));
|
assertEquals("$\\overline{\\mbox{Q}}$", gs.formatText("~Q", Style.NORMAL));
|
||||||
assertEquals("$\\overline{Q}$", gs.formatText("$~Q$", Style.NORMAL));
|
assertEquals("$\\overline{Q}$", gs.formatText("$~Q$", Style.NORMAL));
|
||||||
assertEquals("\\textless{}a\\textgreater{}", gs.formatText("<a>", Style.NORMAL));
|
assertEquals("\\textless{}a\\textgreater{}", gs.formatText("<a>", Style.NORMAL));
|
||||||
assertEquals("Grün", gs.formatText("Grün", Style.NORMAL));
|
assertEquals("Grün", gs.formatText("Grün", Style.NORMAL));
|
||||||
|
@ -21,13 +21,7 @@ public class ParserTest extends TestCase {
|
|||||||
assertEquals("≥1", new Parser("≥1").parse().toString());
|
assertEquals("≥1", new Parser("≥1").parse().toString());
|
||||||
|
|
||||||
assertEquals("Decorate{≥1, MATH}", new Parser("$≥1$").parse().toString());
|
assertEquals("Decorate{≥1, MATH}", new Parser("$≥1$").parse().toString());
|
||||||
|
assertEquals("Decorate{MR, OVERLINE}", new Parser("~MR").parse().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEnforceMath() throws ParseException {
|
|
||||||
assertEquals("Decorate{Decorate{Q, OVERLINE}, MATH}", new Parser("$~Q$").parse().enforceMath().toString());
|
|
||||||
assertEquals("Decorate{Decorate{Q, OVERLINE}, MATH}", new Parser("~Q").parse().enforceMath().toString());
|
|
||||||
assertEquals("Decorate{Q_{i}, MATH}", new Parser("Q_i").parse().enforceMath().toString());
|
|
||||||
assertEquals("hello Decorate{Q_{i}, MATH} world", new Parser("hello Q_i world").parse().enforceMath().toString());
|
|
||||||
assertEquals("hello Decorate{Q_{i}, MATH} world", new Parser("hello Q_i world").parse().enforceMath().enforceMath().toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -13,7 +13,7 @@ public class LaTeXFormatterTest extends TestCase {
|
|||||||
assertEquals("$Q^i$", LaTeXFormatter.format(new Parser("Q^i").parse()));
|
assertEquals("$Q^i$", LaTeXFormatter.format(new Parser("Q^i").parse()));
|
||||||
assertEquals("$Q^i$", LaTeXFormatter.format(new Parser("Q^{i}").parse()));
|
assertEquals("$Q^i$", LaTeXFormatter.format(new Parser("Q^{i}").parse()));
|
||||||
assertEquals("$Q^{in}$", LaTeXFormatter.format(new Parser("Q^{in}").parse()));
|
assertEquals("$Q^{in}$", LaTeXFormatter.format(new Parser("Q^{in}").parse()));
|
||||||
assertEquals("$\\overline{Q}$", LaTeXFormatter.format(new Parser("~Q").parse()));
|
assertEquals("$\\overline{\\mbox{Q}}$", LaTeXFormatter.format(new Parser("~Q").parse()));
|
||||||
assertEquals("$\\overline{Q}_i$", LaTeXFormatter.format(new Parser("~Q_i").parse()));
|
assertEquals("$\\overline{Q}_i$", LaTeXFormatter.format(new Parser("~Q_i").parse()));
|
||||||
assertEquals("$\\overline{Q_i}$", LaTeXFormatter.format(new Parser("~{Q_i}").parse()));
|
assertEquals("$\\overline{Q_i}$", LaTeXFormatter.format(new Parser("~{Q_i}").parse()));
|
||||||
assertEquals("Hello World", LaTeXFormatter.format(new Parser("Hello World").parse()));
|
assertEquals("Hello World", LaTeXFormatter.format(new Parser("Hello World").parse()));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user