w2l: Some StarMath improvements - better tolerance with errors in formula, and fewer redundant braces
git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@129 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
parent
715f9b97e7
commit
50c5e4d2fa
2 changed files with 45 additions and 61 deletions
|
@ -2,6 +2,11 @@ Changelog for Writer2LaTeX version 1.0 -> 1.2
|
||||||
|
|
||||||
---------- version 1.1.9 ----------
|
---------- version 1.1.9 ----------
|
||||||
|
|
||||||
|
[w2l] The StarMath now avoids more redundant braces
|
||||||
|
|
||||||
|
[w2l] The StarMath converter is now a little more tolerant on errors like missing operands for a binary operator
|
||||||
|
(import from MS Word may create such errors)
|
||||||
|
|
||||||
[w2x] EPUB change: Missing heading levels are now treated more consistent in the toc. E.g. the sequence of heading levels
|
[w2x] EPUB change: Missing heading levels are now treated more consistent in the toc. E.g. the sequence of heading levels
|
||||||
2-2-1-3-3-2 is now exported as toc levels 1-1-1-2-2-2 (before it was 1-2-1-2-3-2)
|
2-2-1-3-3-2 is now exported as toc levels 1-1-1-2-2-2 (before it was 1-2-1-2-3-2)
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,9 @@
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||||
* MA 02111-1307 USA
|
* MA 02111-1307 USA
|
||||||
*
|
*
|
||||||
* Copyright: 2002-2009 by Henrik Just
|
* Copyright: 2002-2012 by Henrik Just
|
||||||
*
|
*
|
||||||
* Version 1.2 (2009-09-24)
|
* Version 1.2 (2012-02-22)
|
||||||
*
|
*
|
||||||
* All Rights Reserved.
|
* All Rights Reserved.
|
||||||
*/
|
*/
|
||||||
|
@ -1103,33 +1103,30 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
|
||||||
}
|
}
|
||||||
|
|
||||||
private String product(float fSize, Token eAlign){
|
private String product(float fSize, Token eAlign){
|
||||||
String sProduct=power(fSize,eAlign,true);
|
String sProduct=power(fSize,eAlign);
|
||||||
sProduct=sProduct.substring(1,sProduct.length()-1);
|
|
||||||
// a small hack to avoid double {}: Require {}, then remove them
|
|
||||||
// and add them below if they are needed.
|
|
||||||
while (tokenInGroup(TGroup.PRODUCT)){
|
while (tokenInGroup(TGroup.PRODUCT)){
|
||||||
if (curToken.eType==Token.OVER){
|
if (curToken.eType==Token.OVER){
|
||||||
nextToken();
|
nextToken();
|
||||||
sProduct="\\frac{"+sProduct+"}"+power(fSize,eAlign,true);
|
sProduct="\\frac{"+sProduct+"}{"+power(fSize,eAlign)+"}";
|
||||||
} else if (curToken.eType==Token.BOPER){
|
} else if (curToken.eType==Token.BOPER){
|
||||||
nextToken();
|
nextToken();
|
||||||
sProduct+=special()+power(fSize,eAlign,false);
|
sProduct+=special()+power(fSize,eAlign);
|
||||||
} else if (curToken.eType==Token.OVERBRACE){
|
} else if (curToken.eType==Token.OVERBRACE){
|
||||||
nextToken();
|
nextToken();
|
||||||
sProduct="\\overbrace{"+sProduct+"}^"+power(fSize,eAlign,true);
|
sProduct="\\overbrace{"+sProduct+"}^{"+power(fSize,eAlign)+"}";
|
||||||
} else if (curToken.eType==Token.UNDERBRACE){
|
} else if (curToken.eType==Token.UNDERBRACE){
|
||||||
nextToken();
|
nextToken();
|
||||||
sProduct="\\underbrace{"+sProduct+"}_"+power(fSize,eAlign,true);
|
sProduct="\\underbrace{"+sProduct+"}_{"+power(fSize,eAlign)+"}";
|
||||||
} else if (curToken.eType==Token.WIDESLASH){
|
} else if (curToken.eType==Token.WIDESLASH){
|
||||||
bWideslash=true;
|
bWideslash=true;
|
||||||
nextToken();
|
nextToken();
|
||||||
sProduct="\\wideslash{"+sProduct+"}"+power(fSize,eAlign,true);
|
sProduct="\\wideslash{"+sProduct+"}{"+power(fSize,eAlign)+"}";
|
||||||
} else if (curToken.eType==Token.WIDEBACKSLASH){
|
} else if (curToken.eType==Token.WIDEBACKSLASH){
|
||||||
bWidebslash=true;
|
bWidebslash=true;
|
||||||
nextToken();
|
nextToken();
|
||||||
sProduct="\\widebslash{"+sProduct+"}"+power(fSize,eAlign,true);
|
sProduct="\\widebslash{"+sProduct+"}{"+power(fSize,eAlign)+"}";
|
||||||
} else {
|
} else {
|
||||||
sProduct+=opsubsup(fSize,eAlign)+power(fSize,eAlign,false);
|
sProduct+=opsubsup(fSize,eAlign)+power(fSize,eAlign);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sProduct;
|
return sProduct;
|
||||||
|
@ -1155,12 +1152,12 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
|
||||||
nextToken();
|
nextToken();
|
||||||
if (eScriptType==Token.FROM) sCsub="{"+relation(fSize,eAlign)+"}";
|
if (eScriptType==Token.FROM) sCsub="{"+relation(fSize,eAlign)+"}";
|
||||||
else if (eScriptType==Token.TO) sCsup="{"+relation(fSize,eAlign)+"}";
|
else if (eScriptType==Token.TO) sCsup="{"+relation(fSize,eAlign)+"}";
|
||||||
else if (eScriptType==Token.LSUB) sLsub=term(fSize,eAlign,true);
|
else if (eScriptType==Token.LSUB) sLsub="{"+term(fSize,eAlign)+"}";
|
||||||
else if (eScriptType==Token.LSUP) sLsup=term(fSize,eAlign,true);
|
else if (eScriptType==Token.LSUP) sLsup="{"+term(fSize,eAlign)+"}";
|
||||||
else if (eScriptType==Token.CSUB) sCsub=term(fSize,eAlign,true);
|
else if (eScriptType==Token.CSUB) sCsub="{"+term(fSize,eAlign)+"}";
|
||||||
else if (eScriptType==Token.CSUP) sCsup=term(fSize,eAlign,true);
|
else if (eScriptType==Token.CSUP) sCsup="{"+term(fSize,eAlign)+"}";
|
||||||
else if (eScriptType==Token.RSUB) sRsub=term(fSize,eAlign,true);
|
else if (eScriptType==Token.RSUB) sRsub="{"+term(fSize,eAlign)+"}";
|
||||||
else if (eScriptType==Token.RSUP) sRsup=term(fSize,eAlign,true);
|
else if (eScriptType==Token.RSUP) sRsup="{"+term(fSize,eAlign)+"}";
|
||||||
}
|
}
|
||||||
if (sLsub==null && sLsup==null && sCsub==null && sCsup==null && sRsub==null && sRsup==null){
|
if (sLsub==null && sLsup==null && sCsub==null && sCsup==null && sRsub==null && sRsup==null){
|
||||||
return sBody;
|
return sBody;
|
||||||
|
@ -1210,19 +1207,8 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
|
||||||
return subsup(fSize,eAlign,sOperator,TGroup.POWER);
|
return subsup(fSize,eAlign,sOperator,TGroup.POWER);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String power(float fSize, Token eAlign,boolean bNeedGroup){
|
private String power(float fSize, Token eAlign){
|
||||||
// bNeedGroup is true, if the power needs to be enclosed in braces
|
return subsup(fSize,eAlign,term(fSize,eAlign),TGroup.POWER);
|
||||||
// Since we don't want to add unnecessary braces the responsibility
|
|
||||||
// is delegated to power - we need to look ahead to determine if {}
|
|
||||||
// should be added.
|
|
||||||
boolean bTermIsGroup=curToken.eType==Token.LGROUP;
|
|
||||||
String sTerm=term(fSize,eAlign);
|
|
||||||
if (bNeedGroup && (!bTermIsGroup || tokenInGroup(TGroup.POWER))){
|
|
||||||
return "{"+subsup(fSize,eAlign,sTerm,TGroup.POWER)+"}";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return subsup(fSize,eAlign,sTerm,TGroup.POWER);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String blank(){
|
private String blank(){
|
||||||
|
@ -1234,22 +1220,7 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
|
||||||
return bufBlank.toString();
|
return bufBlank.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String term(float fSize, Token eAlign, boolean bNeedGroup){
|
private String term(float fSize, Token eAlign) {
|
||||||
// Special version of term used to avoid double {{grouping}}
|
|
||||||
// if bNeedGroup=true we must return {term} in braces
|
|
||||||
if (bNeedGroup && !(curToken.eType==Token.LGROUP)){
|
|
||||||
return "{"+term(fSize,eAlign)+"}";
|
|
||||||
}
|
|
||||||
/*else if (!bNeedGroup && curToken.eType==Token.LGROUP){
|
|
||||||
String sTerm=term(fSize,eAlign);
|
|
||||||
return sTerm.substring(1,sTerm.length()-2); // renove unwanted {}
|
|
||||||
}*/
|
|
||||||
else {
|
|
||||||
return term(fSize,eAlign);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String term(float fSize, Token eAlign){
|
|
||||||
String sContent;
|
String sContent;
|
||||||
if (curToken.eType==Token.ESCAPE)
|
if (curToken.eType==Token.ESCAPE)
|
||||||
return escape();
|
return escape();
|
||||||
|
@ -1261,9 +1232,9 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
|
||||||
sContent=""; // empty group
|
sContent=""; // empty group
|
||||||
if (curToken.eType==Token.RGROUP)
|
if (curToken.eType==Token.RGROUP)
|
||||||
nextToken();
|
nextToken();
|
||||||
// otherwise there is an error in the formula
|
// otherwise there is an error in the formula, ignore this
|
||||||
// we close the group anyway to make us TeX'able.
|
// note that we do not keep the grouping; we add grouping where LaTeX needs it instead
|
||||||
return "{"+sContent+"}";
|
return sContent;
|
||||||
}
|
}
|
||||||
else if (curToken.eType==Token.LEFT)
|
else if (curToken.eType==Token.LEFT)
|
||||||
return scalebrace(fSize,eAlign);
|
return scalebrace(fSize,eAlign);
|
||||||
|
@ -1307,8 +1278,16 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
|
||||||
else if (tokenInGroup(TGroup.FUNCTION))
|
else if (tokenInGroup(TGroup.FUNCTION))
|
||||||
return function();
|
return function();
|
||||||
else { // error in formula
|
else { // error in formula
|
||||||
|
if (tokenInGroup(TGroup.RELATION) || tokenInGroup(TGroup.SUM) || tokenInGroup(TGroup.PRODUCT)) {
|
||||||
|
// Try to repair: At least these groups are mostly symbols that are quite acceptable as terms in LaTeX
|
||||||
|
sContent=curToken.sLaTeX;
|
||||||
|
if (sContent.length()==0) { sContent="?"; }
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sContent="?";
|
||||||
|
}
|
||||||
nextToken();
|
nextToken();
|
||||||
return "?";
|
return sContent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1338,10 +1317,10 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
|
||||||
String sOperator=oper();
|
String sOperator=oper();
|
||||||
if (tokenInGroup(TGroup.LIMIT) || tokenInGroup(TGroup.POWER)){
|
if (tokenInGroup(TGroup.LIMIT) || tokenInGroup(TGroup.POWER)){
|
||||||
// Note: TGroup.LIMIT and TGroup.POWER are always in eGroup1, so this is OK:
|
// Note: TGroup.LIMIT and TGroup.POWER are always in eGroup1, so this is OK:
|
||||||
return subsup(fSize,eAlign,sOperator,curToken.eGroup1)+power(fSize,eAlign,false);
|
return subsup(fSize,eAlign,sOperator,curToken.eGroup1)+power(fSize,eAlign);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return sOperator+power(fSize,eAlign,false);
|
return sOperator+power(fSize,eAlign);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1366,26 +1345,26 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
|
||||||
private String unoper(float fSize, Token eAlign){
|
private String unoper(float fSize, Token eAlign){
|
||||||
if (curToken.eType==Token.ABS){
|
if (curToken.eType==Token.ABS){
|
||||||
nextToken();
|
nextToken();
|
||||||
return "\\left|"+power(fSize,eAlign,false)+"\\right|";
|
return "\\left|"+power(fSize,eAlign)+"\\right|";
|
||||||
}
|
}
|
||||||
else if (curToken.eType==Token.SQRT){
|
else if (curToken.eType==Token.SQRT){
|
||||||
nextToken();
|
nextToken();
|
||||||
return "\\sqrt"+power(fSize,eAlign,true);
|
return "\\sqrt{"+power(fSize,eAlign)+"}";
|
||||||
}
|
}
|
||||||
else if (curToken.eType==Token.NROOT){
|
else if (curToken.eType==Token.NROOT){
|
||||||
nextToken();
|
nextToken();
|
||||||
return "\\sqrt["+power(fSize,eAlign,false)+"]"+power(fSize,eAlign,true);
|
return "\\sqrt["+power(fSize,eAlign)+"]{"+power(fSize,eAlign)+"}";
|
||||||
}
|
}
|
||||||
else if (curToken.eType==Token.UOPER){
|
else if (curToken.eType==Token.UOPER){
|
||||||
nextToken();
|
nextToken();
|
||||||
return special()+power(fSize,eAlign,false);
|
return special()+power(fSize,eAlign);
|
||||||
}
|
}
|
||||||
else if (curToken.eType==Token.FACT){
|
else if (curToken.eType==Token.FACT){
|
||||||
String sOperator=opsubsup(fSize,eAlign);
|
String sOperator=opsubsup(fSize,eAlign);
|
||||||
return power(fSize,eAlign,false)+sOperator;
|
return power(fSize,eAlign)+sOperator;
|
||||||
}
|
}
|
||||||
else { // must be PLUS, MINUS, PLUSMINUS, MINUSPLUS or NEG
|
else { // must be PLUS, MINUS, PLUSMINUS, MINUSPLUS or NEG
|
||||||
return opsubsup(fSize,eAlign)+power(fSize,eAlign,false);
|
return opsubsup(fSize,eAlign)+power(fSize,eAlign);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1408,7 +1387,7 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
|
||||||
sAttribute=curToken.sLaTeX; // the color name
|
sAttribute=curToken.sLaTeX; // the color name
|
||||||
nextToken();
|
nextToken();
|
||||||
if (bUseColor) {
|
if (bUseColor) {
|
||||||
return "\\textcolor{"+sAttribute+"}{"+term(fSize,eAlign)+"}";
|
return "\\textcolor{"+sAttribute+"}"+"{"+term(fSize,eAlign)+"}";
|
||||||
// note: despite the name, \textcolor also works in math mode!
|
// note: despite the name, \textcolor also works in math mode!
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue