From a33602398306caf8c28e198e06001008941c018e Mon Sep 17 00:00:00 2001
From: henrikjust <henrikjust@f0f2a975-2e09-46c8-9428-3b39399b9f3c>
Date: Thu, 4 Sep 2014 17:47:49 +0000
Subject: [PATCH] A few LaTeX bugfixes

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@169 f0f2a975-2e09-46c8-9428-3b39399b9f3c
---
 source/distro/Readme.txt                      |  2 +-
 source/distro/changelog.txt                   |  8 +++-
 .../writer2latex/api/ConverterFactory.java    |  4 +-
 .../base/BinaryGraphicsDocument.java          |  4 +-
 .../writer2latex/base/ImageConverter.java     |  2 +-
 .../writer2latex/latex/DrawConverter.java     |  2 +-
 .../latex/LaTeXDocumentPortion.java           | 48 +++++++++++++------
 .../writer2latex/latex/StarMathConverter.java | 33 ++++++++++---
 .../writer2latex/latex/TableConverter.java    | 34 ++++++++-----
 .../writer2latex/util/SimpleXMLParser.java    |  2 +-
 source/readme-source.txt                      |  2 +-
 11 files changed, 99 insertions(+), 42 deletions(-)

diff --git a/source/distro/Readme.txt b/source/distro/Readme.txt
index ed71e9b..e2f2916 100644
--- a/source/distro/Readme.txt
+++ b/source/distro/Readme.txt
@@ -14,5 +14,5 @@ Bugs and feature requests should be reported to
   henrikjust (at) openoffice.org
   
 
-August 2014
+September 2014
 Henrik Just
diff --git a/source/distro/changelog.txt b/source/distro/changelog.txt
index c6f7095..9dc7d0b 100644
--- a/source/distro/changelog.txt
+++ b/source/distro/changelog.txt
@@ -2,9 +2,15 @@ Changelog for Writer2LaTeX version 1.2 -> 1.4
 
 ---------- version 1.3.2 alpha ----------
 
+[w2l] Bugfix (StarMath conversion): Protect the character [ after \\ in gather and matrix environments
+
+[w2l] Bugfix: Protect the character [ after \\ in tables
+
+[w2l] Bugfix (StarMath conversion): Usage of \multiscripts and \mathoverstrike now loads the required calc.sty
+
 [w2l] Bugfix (StarMath conversion): Do not create display equations in table cells 
 
-[w2l] Bugfix (StarMath conversion): Use array instead of matrix if there is more than 10 columns
+[w2l] Bugfix (StarMath conversion): Set the counter MaxMatrixCols if there are matrices with more than 10 columns
  
 [w2l] Bugfix (StarMath conversion): Add braces if the argument to a command is a space, e.g. \text{ }
 
diff --git a/source/java/writer2latex/api/ConverterFactory.java b/source/java/writer2latex/api/ConverterFactory.java
index af29092..2772c9d 100644
--- a/source/java/writer2latex/api/ConverterFactory.java
+++ b/source/java/writer2latex/api/ConverterFactory.java
@@ -20,7 +20,7 @@
  *
  *  All Rights Reserved.
  * 
- *  Version 1.4 (2012-08-28)
+ *  Version 1.4 (2014-09-03)
  *
  */
  
@@ -33,7 +33,7 @@ public class ConverterFactory {
 
     // Version information
     private static final String VERSION = "1.3.2";
-    private static final String DATE = "2014-08-28";
+    private static final String DATE = "2014-09-03";
 	
     /** Return the Writer2LaTeX version in the form
      *  (major version).(minor version).(patch level)<br/>
diff --git a/source/java/writer2latex/base/BinaryGraphicsDocument.java b/source/java/writer2latex/base/BinaryGraphicsDocument.java
index aafebc4..2af92c3 100644
--- a/source/java/writer2latex/base/BinaryGraphicsDocument.java
+++ b/source/java/writer2latex/base/BinaryGraphicsDocument.java
@@ -62,10 +62,10 @@ public class BinaryGraphicsDocument implements OutputFile {
      * @param sFileExtension the file extension
      * @param sMimeType the MIME type of the document
      */
-    public BinaryGraphicsDocument(String name, String sFileExtension, String sMimeType) {
+    public BinaryGraphicsDocument(String sName, String sFileExtension, String sMimeType) {
         this.sFileExtension = sFileExtension;
         this.sMimeType = sMimeType;
-        sFileName = Misc.trimDocumentName(name, sFileExtension);
+        sFileName = Misc.trimDocumentName(sName, sFileExtension);
     }
         
     /** Set image contents to a byte array
diff --git a/source/java/writer2latex/base/ImageConverter.java b/source/java/writer2latex/base/ImageConverter.java
index 0759f0f..8ffe4bd 100644
--- a/source/java/writer2latex/base/ImageConverter.java
+++ b/source/java/writer2latex/base/ImageConverter.java
@@ -72,7 +72,7 @@ public final class ImageConverter {
 
     /** Construct a new <code>ImageConverter</code> referring to a specific document
      * 
-     * @param doc the office document used
+     * @param ofr the office reader to use
      * @param bExtractEPS set true if EPS content should be extracted from SVM files
      */
     public ImageConverter(OfficeReader ofr, boolean bDestructive, boolean bExtractEPS) {
diff --git a/source/java/writer2latex/latex/DrawConverter.java b/source/java/writer2latex/latex/DrawConverter.java
index 8f67ce6..5c34eb8 100644
--- a/source/java/writer2latex/latex/DrawConverter.java
+++ b/source/java/writer2latex/latex/DrawConverter.java
@@ -166,7 +166,7 @@ public class DrawConverter extends ConverterHelper {
                         }
 	                }
                     else { // unsupported object
-                    	System.out.println("Unsupported "+sHref);
+                    	//System.out.println("Unsupported "+sHref);
                         boolean bIgnore = true;
                         if (ofr.isOpenDocument()) { // look for replacement image
                             Element replacementImage = Misc.getChildByTagName(getFrame(node),XMLString.DRAW_IMAGE);
diff --git a/source/java/writer2latex/latex/LaTeXDocumentPortion.java b/source/java/writer2latex/latex/LaTeXDocumentPortion.java
index 5f4ec33..7023583 100644
--- a/source/java/writer2latex/latex/LaTeXDocumentPortion.java
+++ b/source/java/writer2latex/latex/LaTeXDocumentPortion.java
@@ -16,11 +16,11 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  *  MA  02111-1307  USA
  *
- *  Copyright: 2002-2006 by Henrik Just
+ *  Copyright: 2002-2014 by Henrik Just
  *
  *  All Rights Reserved.
  * 
- *  Version 1.0 (2007-10-02)
+ *  Version 1.4 (2014-09-03)
  *
  */
 
@@ -43,6 +43,10 @@ public class LaTeXDocumentPortion {
 
     private boolean bWrap; // Do we allow line wrap in this portion?
     
+    /** Construct a new empty <code>LaTeXDocumentPortion</code>
+     * 
+     * @param bWrap set to true if lines may be wrapped on writing
+     */
     public LaTeXDocumentPortion(boolean bWrap){
         this.bWrap = bWrap;
         nodes = new Vector<Object>();
@@ -50,7 +54,11 @@ public class LaTeXDocumentPortion {
         bEmpty = true;
     }
 	
-    /** Add another portion to the end of this portion */
+    /** Add another portion to the end of this portion
+     * 
+     * @param ldp The <code>LaTeXDocuemtPortion</code> to add
+     * @return a reference to this <code>LaTeXDocumentPortion</code> (not the appended one)
+     */
     public LaTeXDocumentPortion append(LaTeXDocumentPortion ldp) {
         if (!bEmpty) {
             // add the current node to the node list and create new current node
@@ -62,14 +70,21 @@ public class LaTeXDocumentPortion {
         return this;
     }
     
-    /** Add a string to the end of this portion */
+    /** Add a string to the end of this portion
+     * 
+     * @param s the string to add
+     * @return a reference to this <code>LaTeXDocumentPortion</code>
+     */
     public LaTeXDocumentPortion append(String s){
         curText.append(s);
         bEmpty = false; // even if this is the empty string!
         return this;
     }
     
-    /** Add a newline to the end of this portion */
+    /** Add a newline to the end of this portion
+     * 
+     * @return a reference to this <code>LaTeXDocumentPortion</code>
+     */
     public LaTeXDocumentPortion nl(){
         curText.append("\n");
         bEmpty = false;
@@ -164,7 +179,13 @@ public class LaTeXDocumentPortion {
         }
     }
 
-    /** Write this portion to the output (note: nLineLen=0 means no wrap) */
+    /** Write this portion to the output
+     * 
+     * @param osw an <code>OutputStreamWriter</code> to write to
+     * @param nLineLen the line length after which automatic line breaks should occur if allowed (nLineLen=0 means no wrap)
+     * @param sNewline the newline character(s) to use
+     * @throws IOException if an exception occurs writing to to osw
+     */
     public void write(OutputStreamWriter osw, int nLineLen, String sNewline) throws IOException {
         int n = nodes.size();
         for (int i=0; i<n; i++) {
@@ -188,7 +209,10 @@ public class LaTeXDocumentPortion {
         }
     }
 	
-    /** Return the content of this LaTeXDocumentStream as a string */
+    /** Return the content of this LaTeXDocumentPortion as a string
+     * 
+     *  @return a string representation of the <code>LaTeXDocumentPortion</code>
+     */
     public String toString() {
         StringBuffer buf = new StringBuffer();
         int n = nodes.size();
@@ -197,7 +221,7 @@ public class LaTeXDocumentPortion {
                 buf.append(((LaTeXDocumentPortion) nodes.get(i)).toString());
             }
             else {
-                buf.append(((StringBuffer) nodes.get(i)).toString());
+                buf.append((StringBuffer) nodes.get(i));
             }
         }
         if (!bEmpty) { // write current node as well
@@ -205,10 +229,4 @@ public class LaTeXDocumentPortion {
         }
         return buf.toString();
     }
-	
-    
-
-} // end class LaTeXDocumentPortion
-
-// TO DO: consider StringBuffer->ByteArrayOutputStream (performance??)
-
+}
diff --git a/source/java/writer2latex/latex/StarMathConverter.java b/source/java/writer2latex/latex/StarMathConverter.java
index 3685427..6302ad2 100644
--- a/source/java/writer2latex/latex/StarMathConverter.java
+++ b/source/java/writer2latex/latex/StarMathConverter.java
@@ -772,6 +772,9 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
     }
 
     public void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) {
+        if (bMultiscripts || bMathoverstrike) {
+        	pack.append("\\usepackage{calc}").nl();
+        }
         if (config.useOoomath()) {
             pack.append("\\usepackage{ooomath}").nl();
         }
@@ -1054,7 +1057,12 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
             bufTable.append("\\begin{gathered}").append(sLine);
             while (curToken.eType==Token.NEWLINE){
                 nextToken();
-                bufTable.append("\\\\").append(line(fSize,eAlign,false));
+                bufTable.append("\\\\");
+                sLine = line(fSize,eAlign,false);
+                if (sLine.length()>0 && sLine.charAt(0)=='[') { // Protect [ after \\
+                	bufTable.append("{}");
+                }
+                bufTable.append(sLine);
             }
             return bufTable.append("\\end{gathered}").toString();
         }
@@ -1599,11 +1607,17 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
         nextToken();
         if (curToken.eType==Token.LGROUP){
             StringBuffer bufStack=new StringBuffer().append("\\begin{matrix}");
-            do {
+            nextToken();
+            bufStack.append(align(fSize,eAlign,true,true));
+            while (curToken.eType==Token.POUND) {
+                bufStack.append("\\\\");
                 nextToken();
-                bufStack.append(align(fSize,eAlign,true,true));
-                if (curToken.eType==Token.POUND) bufStack.append("\\\\");
-            } while (curToken.eType==Token.POUND);
+                String sAlign = align(fSize,eAlign,true,true);
+                if (sAlign.length()>0 && sAlign.charAt(0)=='[') { // Protect [ after \\
+                	bufStack.append("{}");
+                }
+            	bufStack.append(sAlign);
+            } 
             if (curToken.eType==Token.RGROUP) nextToken(); // otherwise error in formula - ignore
             return bufStack.append("\\end{matrix}").toString();
         }
@@ -1617,17 +1631,24 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
         if (curToken.eType==Token.LGROUP){
             StringBuffer bufMatrix = new StringBuffer().append("\\begin{matrix}");
             int nCols = 1;
+            boolean bProtect = false;
             do {
                 nextToken();
-                bufMatrix.append(align(fSize,eAlign,true,true));
+                String sAlign = align(fSize,eAlign,true,true);
+                if (bProtect && sAlign.length()>0 && sAlign.charAt(0)=='[') { // Protect [ after \\
+                	bufMatrix.append("{}");
+                }
+                bufMatrix.append(sAlign);
                 if (curToken.eType==Token.POUND) {
                 	bufMatrix.append("&");
                 	nCols++;
+                	bProtect = false;
                 }
                 else if (curToken.eType==Token.DPOUND) { 
                 	bufMatrix.append("\\\\");
                 	nMaxMatrixCols = Math.max(nCols, nMaxMatrixCols);
                 	nCols = 1;
+                	bProtect = true;
                 }
             } while (curToken.eType==Token.POUND || curToken.eType==Token.DPOUND);
             if (curToken.eType==Token.RGROUP) nextToken(); // otherwise error in formula- ignore
diff --git a/source/java/writer2latex/latex/TableConverter.java b/source/java/writer2latex/latex/TableConverter.java
index fbf3e20..88b2cb7 100644
--- a/source/java/writer2latex/latex/TableConverter.java
+++ b/source/java/writer2latex/latex/TableConverter.java
@@ -16,11 +16,11 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  *  MA  02111-1307  USA
  *
- *  Copyright: 2002-2011 by Henrik Just
+ *  Copyright: 2002-2014 by Henrik Just
  *
  *  All Rights Reserved.
  * 
- *  Version 1.2 (2011-04-20)
+ *  Version 1.4 (2014-09-03)
  *
  */
 
@@ -414,12 +414,15 @@ public class TableConverter extends ConverterHelper {
             int nRowCount = table.getRowCount();
             int nColCount = table.getColCount();
             boolean bFirst = true;
+            boolean bProtect = false; // Do we need to protect '['?
             int nPreviousRow = -1;
             for (int nRow=0; nRow<nRowCount; nRow++) {
             	if (rowTypes[nRow]==rowType) {
             		// Add interrow material from previous row, if any
             		if (nPreviousRow>-1) {
-            			ldp.append(formatter.getInterrowMaterial(nPreviousRow+1)).nl();
+            			String sInterRowMaterial = formatter.getInterrowMaterial(nPreviousRow+1); 
+            			ldp.append(sInterRowMaterial).nl();
+            			if (sInterRowMaterial.length()>0) { bProtect=false; }
             		}
             		nPreviousRow = nRow;
 
@@ -430,12 +433,13 @@ public class TableConverter extends ConverterHelper {
             			bFirst=false;
             		}
             		// Export columns in this row
+            		LaTeXDocumentPortion rowLdp = new LaTeXDocumentPortion(true);
             		Context icRow = (Context) oc.clone();
             		BeforeAfter baRow = new BeforeAfter();
             		formatter.applyRowStyle(nRow,baRow,icRow);
             		if (!baRow.isEmpty()) {
-            			ldp.append(baRow.getBefore());
-            			if (!formatter.isSimple()) { ldp.nl(); }
+            			rowLdp.append(baRow.getBefore());
+            			if (!formatter.isSimple()) { rowLdp.nl(); }
             		}   
             		int nCol = 0;
             		while (nCol<nColCount) {
@@ -445,18 +449,18 @@ public class TableConverter extends ConverterHelper {
             					Context icCell = (Context) icRow.clone();
             					BeforeAfter baCell = new BeforeAfter();
             					formatter.applyCellStyle(nRow,nCol,baCell,icCell);
-            					ldp.append(baCell.getBefore());
+            					rowLdp.append(baCell.getBefore());
             					if (nCol==nColCount-1) { icCell.setInLastTableColumn(true); }
-            					palette.getBlockCv().traverseBlockText(cell,ldp,icCell);
-            					ldp.append(baCell.getAfter());
+            					palette.getBlockCv().traverseBlockText(cell,rowLdp,icCell);
+            					rowLdp.append(baCell.getAfter());
             				}
             				// Otherwise ignore; the cell is covered by a \multicolumn entry.
             				// (table:covered-table-cell)
             				int nColSpan = Misc.getPosInteger(cell.getAttribute(
             						XMLString.TABLE_NUMBER_COLUMNS_SPANNED),1);
             				if (nCol+nColSpan<nColCount) {
-            					if (formatter.isSimple()) { ldp.append(" & "); }
-            					else { ldp.append(" &").nl(); }
+            					if (formatter.isSimple()) { rowLdp.append(" & "); }
+            					else { rowLdp.append(" &").nl(); }
             				}
             				nCol+=nColSpan;
             			}
@@ -464,7 +468,15 @@ public class TableConverter extends ConverterHelper {
             				nCol++;
             			}
             		}
-            		ldp.append("\\\\");
+            		rowLdp.append("\\\\");
+            		// We have to translate the row to a string to avoid extra newlines and to see the first characters 
+            		String sRowLdp = rowLdp.toString();
+            		// Protect leading [
+            		if (bProtect && ((sRowLdp.length()>0 && sRowLdp.charAt(0)=='[') || sRowLdp.startsWith("\n["))) {
+            			ldp.append("{}");
+            		}
+            		ldp.append(sRowLdp);
+            		bProtect = true;
             	}
             }
     		// Add interrow material from last row, if required
diff --git a/source/java/writer2latex/util/SimpleXMLParser.java b/source/java/writer2latex/util/SimpleXMLParser.java
index 31f4fce..69bfdf8 100644
--- a/source/java/writer2latex/util/SimpleXMLParser.java
+++ b/source/java/writer2latex/util/SimpleXMLParser.java
@@ -78,7 +78,7 @@ public class SimpleXMLParser extends DefaultHandler {
 	
 	// We don't need - and in fact should avoid - any external entities
 	@Override public InputSource resolveEntity(String publicID, String systemID) throws SAXException {
-		System.out.println("resolveEntity "+publicID+" "+systemID);
+		//System.out.println("resolveEntity "+publicID+" "+systemID);
 		return new InputSource(new StringReader(""));
 	}
 	
diff --git a/source/readme-source.txt b/source/readme-source.txt
index 63acf9b..860ab90 100644
--- a/source/readme-source.txt
+++ b/source/readme-source.txt
@@ -95,7 +95,7 @@ In addition to oxt, the build file supports the following targets:
     clean
 
 
-Henrik Just, August 2014
+Henrik Just, September 2014
 
 
 Thanks to Michael Niedermair for writing the original ant build file