fixed a bug in the LaTeX index formatter

This commit is contained in:
hneemann 2018-01-17 18:42:35 +01:00
parent d4f4c96c68
commit 2d4050456b
3 changed files with 28 additions and 5 deletions

View File

@ -172,8 +172,13 @@ public class ElementAttributes {
* @return the clean name
*/
public static String cleanLabel(String name) {
if (name.length() > 2 && name.charAt(0) == '$' && name.charAt(name.length() - 1) == '$')
if (name.length() > 2 && name.charAt(0) == '$' && name.charAt(name.length() - 1) == '$') {
name = name.substring(1, name.length() - 1);
name = name
.replace("{", "")
.replace("}", "")
.replace("^", "");
}
return name;
}

View File

@ -137,9 +137,11 @@ public class GraphicSVGLaTeX extends GraphicSVG {
}
private String formatIndex(String text) {
int p = text.lastIndexOf("_");
if (p > 0) {
text = text.substring(0, p) + "$_{" + text.substring(p + 1) + "}$";
if (text.length() > 0 && text.charAt(0) != '$') {
int p = text.lastIndexOf("_");
if (p > 0) {
text = text.substring(0, p) + "$_{" + text.substring(p + 1) + "}$";
}
}
return text;
}

View File

@ -1,15 +1,19 @@
package de.neemann.digital.draw.graphics;
import de.neemann.digital.core.element.ElementAttributes;
import junit.framework.TestCase;
import java.io.IOException;
/**
* @author hneemann
*/
public class GraphicSVGLaTeXTest extends TestCase {
public void testFormatText() throws Exception {
GraphicSVGLaTeX gs = new GraphicSVGLaTeX(System.out, null, 30);
gs.setBoundingBox(new Vector(0, 0), new Vector(30, 30));
assertEquals("$Z_0$", gs.formatText("$Z_0$", Style.NORMAL.getFontSize()));
assertEquals("$Z_{in}$", gs.formatText("$Z_{in}$", Style.NORMAL.getFontSize()));
assertEquals("Z$_{0}$", gs.formatText("Z_0", Style.NORMAL.getFontSize()));
assertEquals("\\&", gs.formatText("&", Style.NORMAL.getFontSize()));
assertEquals("$\\geq\\!\\!{}$1", gs.formatText("\u22651", Style.NORMAL.getFontSize()));
@ -26,4 +30,16 @@ public class GraphicSVGLaTeXTest extends TestCase {
assertEquals("{\\tiny Z$_{0}$}", gs.formatText("Z_0", Style.WIRE_BITS.getFontSize()));
}
public void testCleanLabel() throws IOException {
check("$Z_0$", "Z_0", "$Z_0$");
check("$Z_{in}$", "Z_in", "$Z_{in}$");
check("Z_0", "Z_0", "Z$_{0}$");
check("$Z_0^n$", "Z_0n", "$Z_0^n$");
}
private void check(String orig, String clean, String LaTeX) throws IOException {
GraphicSVGLaTeX gs = new GraphicSVGLaTeX(System.out, null, 30);
assertEquals(LaTeX, gs.formatText(orig, Style.NORMAL.getFontSize()));
assertEquals(clean, ElementAttributes.cleanLabel(orig));
}
}