New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Patch] Allow to set xcolor options #180

Open
tinloaf opened this Issue Feb 23, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@tinloaf

tinloaf commented Feb 23, 2018

Currently, it is not possible in the preamble to load the xcolor package with options (I need the table option, for example), because ipe loads the xcolor package right in the beginning, before colors are being defined.

This patch solves this problem as follows: If the user loads xcolor (i.e., if there is a string looking like \usepackage[someoptions]{xcolor} in the preamble), that string is removed from the preamble and replaces ipe's own xcolor loading.

The appended patch (sorry I needed to gzip it, because GitHub does not accept .patch files) is a patch against ipe 7.2.7.

xcolor.patch.gz

@tinloaf

This comment has been minimized.

tinloaf commented Feb 23, 2018

Here's the patch:

Only in ipe-7.2.7-patched/src/: description-pak
diff -U 2 -r ipe-7.2.7/src/include/ipelatex.h ipe-7.2.7-patched/src/include/ipelatex.h
--- ipe-7.2.7/src/include/ipelatex.h	2016-12-09 15:09:04.000000000 +0100
+++ ipe-7.2.7-patched/src/include/ipelatex.h	2018-02-23 15:10:45.137402708 +0100
@@ -33,4 +33,6 @@
 #define PDFLATEX_P_H
 
+#include <regex>
+
 #include "ipepage.h"
 #include "ipetext.h"
@@ -68,4 +70,14 @@
     };
 
+    //! A regular expression to extract xcolor loading from the preamble
+    const static std::regex xcolorRe; 
+
+    //! Retrieve and remove the xcolor loading from the preamble, if present
+    struct XcolorResult {
+      String remainingPreamble;
+      String xcolorLoad;
+    };
+    XcolorResult findXcolorLoad(String preamble);
+
     typedef std::list<SText> TextList;
     typedef std::list<Text::XForm *> XFormList;
Only in ipe-7.2.7-patched/src/include: ipelatex.h.gch
Only in ipe-7.2.7-patched/src/: ipe_7.2.7-1_amd64.deb
diff -U 2 -r ipe-7.2.7/src/ipelib/ipelatex.cpp ipe-7.2.7-patched/src/ipelib/ipelatex.cpp
--- ipe-7.2.7/src/ipelib/ipelatex.cpp	2016-12-09 15:09:04.000000000 +0100
+++ ipe-7.2.7-patched/src/ipelib/ipelatex.cpp	2018-02-23 15:15:15.166769706 +0100
@@ -47,4 +47,8 @@
 */
 
+const std::regex Latex::xcolorRe = std::regex(
+  R"(\\usepackage\[[[:alnum:][:space:],]*]\{\s*xcolor\s*})",
+  std::regex_constants::ECMAScript
+);
 //! Create a converter object.
 Latex::Latex(const Cascade *sheet, LatexType latexType)
@@ -143,4 +147,21 @@
 }
 
+Latex::XcolorResult Latex::findXcolorLoad(String preamble)
+{
+  XcolorResult res;
+
+  std::cmatch xcolorMatch;
+  if (std::regex_search(preamble.data(), xcolorMatch, Latex::xcolorRe)) {
+    res.remainingPreamble = String(std::regex_replace(preamble.data(),
+                                                      xcolorRe, "").c_str());
+    res.xcolorLoad = String(xcolorMatch.str().c_str());
+  } else {
+    res.remainingPreamble = preamble;
+    res.xcolorLoad = String("\\usepackage{xcolor}");
+  }
+
+  return res;
+}
+
 //! Create Text object to represent the page number of this view.
 void Latex::addPageNumber(int pno, int vno, int npages, int nviews)
@@ -184,4 +205,9 @@
   bool ancient = (getenv("IPEANCIENTPDFTEX") != 0);
   int count = 0;
+
+  // First, check if the user tried to load xcolor and if yes, modify the
+  // preamble accordingly
+  auto xcolorResult = this->findXcolorLoad(preamble);
+
   stream << "\\nonstopmode\n";
   if (!iXetex) {
@@ -198,5 +224,5 @@
   }
   stream << "\\documentclass{article}\n"
-	 << "\\usepackage{xcolor}\n"
+   << xcolorResult.xcolorLoad << "\n"
 	 << "\\newdimen\\ipefs\n"
 	 << "\\newcounter{ipePage}\\newcounter{ipeView}\n"
@@ -230,5 +256,5 @@
   }
   stream << iCascade->findPreamble() << "\n"
-	 << preamble << "\n"
+   << xcolorResult.remainingPreamble << "\n"
 	 << "\\pagestyle{empty}\n"
 	 << "\\newcount\\bigpoint\\dimen0=0.01bp\\bigpoint=\\dimen0\n"
@pesslovany

This comment has been minimized.

pesslovany commented Oct 2, 2018

Hi, I have no clue how to apply this, can someone help? Cant find it anywhere...

@tinloaf

This comment has been minimized.

tinloaf commented Oct 2, 2018

Hi @pesslovany - it should work roughly like this:

  • Get the Ipe 7.2.7 source code from ipe.otfried.org and unpack it somewhere
  • Download the patch (the xcolor.patch.gz file from the first post), and unzip it
  • change into the directory that you unpacked it into (it should contain a folder "src")
  • run patch -p1 < /path/to/the/downloaded/and/unzipped/xcolor.path

And then just build ipe from that source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment