package com.singularsys.jeptests.system;
import java.text.NumberFormat;
import org.junit.Before;
import org.junit.Test;
import com.singularsys.jep.Jep;
import com.singularsys.jep.JepException;
import com.singularsys.jep.OperatorTableI;
import com.singularsys.jep.ParseException;
import com.singularsys.jep.PrintVisitor;
import com.singularsys.jep.PrintVisitor.PrintRulesI;
import com.singularsys.jep.parser.Node;
public class LatexTest {
Jep jep;
@Before
public void setUp() throws Exception {
jep = new Jep();
PrintVisitor pv = jep.getPrintVisitor();
OperatorTableI ot = jep.getOperatorTable();
pv.addSpecialRule(ot.getDivide(), new PrintRulesI() {
@Override
public void append(Node node, PrintVisitor pv) throws JepException {
pv.append("\\frac{");
node.jjtGetChild(0).jjtAccept(pv, null);
pv.append("}{");
node.jjtGetChild(1).jjtAccept(pv, null);
pv.append("}");
}});
pv.addSpecialRule(ot.getMultiply(), new PrintRulesI() {
@Override
public void append(Node node, PrintVisitor pv) throws JepException {
node.jjtGetChild(0).jjtAccept(pv, null);
pv.append(" ");
node.jjtGetChild(1).jjtAccept(pv, null);
}});
pv.addSpecialRule("sqrt",new PrintRulesI() {
@Override
public void append(Node node, PrintVisitor pv) throws JepException {
pv.append("\\sqrt{");
node.jjtGetChild(0).jjtAccept(pv, null);
pv.append("}");
}});
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(3);
format.setMinimumFractionDigits(0);
jep.getPrintVisitor().setNumberFormat(format);
}
@Test
public void test() throws ParseException {
Node n = jep.parse("(-b + sqrt (b ^ 2 - 4a c)) / (2a)");
jep.println(n);
}
}
public Object visit(ASTFunNode node, Object data) throws ParseException
You would first need to detect the functions you need and take the appropriate action. If the function does not match then call the superclass method
public class MyPrintVisitor extends PrintVisitor {
@Override
public Object visit(ASTFunNode node, Object data) throws ParseException {
if(node..getName().equals("sqrt")) {
...
} else {
super.visit(node,data);
}
}
}
pv.addSpecialRule("sqrt", new PrintRulesI() {
@Override
public void append(Node node, PrintVisitor pv) throws ParseException {
pv.append("\\sqrt{");
node.jjtGetChild(0).jjtAccept(pv, null);
pv.append("}");
}
});