LaTeX export seems to work

This commit is contained in:
hneemann 2018-02-18 20:27:36 +01:00
parent a1ad048bda
commit bf46f84a36
11 changed files with 28 additions and 59 deletions

View File

@ -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;

View File

@ -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 " ";

View File

@ -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;

View File

@ -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 + '}';

View File

@ -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();

View File

@ -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();

View File

@ -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;

View File

@ -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();
} }

View File

@ -18,7 +18,7 @@ public class GraphicSVGLaTeXTest extends TestCase {
assertEquals("\\&amp;", gs.formatText("&", Style.NORMAL)); assertEquals("\\&amp;", 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));

View File

@ -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());
}
} }

View File

@ -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()));