commit 10c88aa58f9d93dd7e56c8a2cbd300c5b3524313
Author: Martin Vermeer <mverm...@aalto.fi>
Date:   Sat Sep 3 17:17:45 2016 +0300

    Initial commit of Inkscape SVG LaTeX + PDF support
    
    Based on Daniel Groger's work of five years ago, with minor changes
    
    http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg169820.html
    
    This extends the support for Xfig LaTeX + PDF to a more modern and
    actively developed vector graphics editor. Embedded Objects manual
    updated, also to include a workaround for an Inkscape 0.91 bug.
---
 lib/Makefile.am             |    2 +
 lib/configure.py            |    6 ++-
 lib/doc/EmbeddedObjects.lyx |  117 ++++++++++++++++++++++++++++++++++++++++++-
 lib/doc/workaround.png      |  Bin 0 -> 17450 bytes
 lib/external_templates      |   42 +++++++++++++++
 lib/scripts/svg2pdftex.py   |   57 +++++++++++++++++++++
 lib/scripts/svg2pstex.py    |   49 ++++++++++++++++++
 7 files changed, 269 insertions(+), 4 deletions(-)

diff --git a/lib/Makefile.am b/lib/Makefile.am
index 1da5b97..99d9fe4 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -2316,6 +2316,8 @@ dist_scripts_DATA += \
        scripts/fen2ascii.py \
        scripts/fig2pdftex.py \
        scripts/fig2pstex.py \
+       scripts/svg2pdftex.py \
+       scripts/svg2pstex.py \
        scripts/fig_copy.py \
        scripts/html2latexwrapper.py \
        scripts/include_bib.py \
diff --git a/lib/configure.py b/lib/configure.py
index 3e60d20..a11050f 100644
--- a/lib/configure.py
+++ b/lib/configure.py
@@ -712,9 +712,9 @@ def checkFormatEntries(dtl_tools):
 \Format jlyx       cjklyx "CJK LyX 1.4.x (euc-jp)" "" ""       ""      
"document"      ""
 \Format klyx       cjklyx "CJK LyX 1.4.x (euc-kr)" "" ""       ""      
"document"      ""
 \Format lyxpreview lyxpreview "LyX Preview"       "" ""        ""      ""      
""
-\Format pdftex     pdftex_t PDFTEX                "" ""        ""      ""      
""
+\Format pdftex     "pdftex_t, pdf_tex" PDFTEX                "" ""     ""      
""      ""
 \Format program    ""      Program                "" ""        ""      ""      
""
-\Format pstex      pstex_t PSTEX                  "" ""        ""      ""      
""
+\Format pstex      "pstex_t, ps_tex" PSTEX                  "" ""      ""      
""      ""
 \Format wmf        wmf    "Windows Metafile"      "" ""        ""      
"vector"        "image/x-wmf"
 \Format emf        emf    "Enhanced Metafile"     "" ""        ""      
"vector"        "image/x-emf"
 \Format wordhtml  "html, htm" "HTML (MS Word)"    "" "" ""     "document"      
""
@@ -934,6 +934,8 @@ def checkConverterEntries():
 \converter fig        ppm        "fig2dev -L ppm $$i $$o"      ""
 \converter fig        svg        "fig2dev -L svg $$i $$o"      ""
 \converter fig        png        "fig2dev -L png $$i $$o"      ""
+\converter svg        pdftex     "python -tt $$s/scripts/svg2pdftex.py $$i 
$$o" ""
+\converter svg        pstex      "python -tt $$s/scripts/svg2pstex.py $$i $$o" 
""
 \converter fig        pdftex     "python -tt $$s/scripts/fig2pdftex.py $$i 
$$o"        ""
 \converter fig        pstex      "python -tt $$s/scripts/fig2pstex.py $$i $$o" 
""''')
     #
diff --git a/lib/doc/EmbeddedObjects.lyx b/lib/doc/EmbeddedObjects.lyx
index 713eab1..eb10ca3 100644
--- a/lib/doc/EmbeddedObjects.lyx
+++ b/lib/doc/EmbeddedObjects.lyx
@@ -1,5 +1,5 @@
-#LyX 2.2 created this file. For more info see http://www.lyx.org/
-\lyxformat 508
+#LyX 2.3 created this file. For more info see http://www.lyx.org/
+\lyxformat 512
 \begin_document
 \begin_header
 \save_transient_properties true
@@ -171,6 +171,7 @@ graphicboxes
 \font_osf false
 \font_sf_scale 100 100
 \font_tt_scale 100 100
+\use_microtype false
 \graphics default
 \default_output_format pdf2
 \output_sync 0
@@ -38609,6 +38610,118 @@ target "http://en.wikipedia.org/wiki/Xfig";
 .
 \end_layout
 
+\begin_layout Description
+Inkscape
+\begin_inset space ~
+\end_inset
+
+figure This experimental template supports images created with the program
+ 
+\begin_inset CommandInset href
+LatexCommand href
+name "Inkscape"
+target "https://fi.wikipedia.org/wiki/Inkscape";
+
+\end_inset
+
+.
+ It works very similarly to Xfig support, except that Inkscape is a more
+ modern image editor than Xfig, which uses Scalable Vector Graphics (SVG)
+ as its native format.
+\end_layout
+
+\begin_deeper
+\begin_layout Standard
+LyX will process the image file in much the same way as it does an Xfig
+ image, extracting the text into a separate TeX file for the text strings,
+ and a PDF file for the rest.
+\end_layout
+
+\begin_layout Description
+Warning: Unfortunately the latest version of Inkscape, 0.91, contains a bug:
+ with multi-page PDFs, the TeX file will sometimes reference more pages
+ than the PDF file contains.
+ This is the version that ships with Ubuntu 16.04.
+ Its use with LyX can therefore not be recommended, until the bug is fixed
+\begin_inset Foot
+status open
+
+\begin_layout Plain Layout
+A workaround for those who have already SVG figures in their document is,
+ to manually do the LaTeX+PDF output from Inkscape, edit the 
+\family typewriter
+.pdf_tex
+\family default
+ file to remove the erroneous references, and 
+\family typewriter
+
+\backslash
+input
+\family default
+ it directly into LyX, i.e., not use the external-template mechanism:
+\end_layout
+
+\begin_layout Plain Layout
+\begin_inset Graphics
+       filename workaround.png
+       scale 50
+
+\end_inset
+
+
+\end_layout
+
+\begin_layout Plain Layout
+Here, the child document input inset (
+\family sans
+Insert\SpecialChar menuseparator
+File\SpecialChar menuseparator
+Child Document
+\family default
+) was used, which allows you do directly edit the 
+\family typewriter
+.pdf_tex
+\family default
+ file in a text editor.
+ The LaTeX output  is based on this edited snippet as re-generation from
+ the SVG file has been commented out.
+\end_layout
+
+\end_inset
+
+.
+\end_layout
+
+\begin_deeper
+\begin_layout Standard
+The reason why we would use this complicated process at all, like in the
+ case of Xfig, is that it makes the text strings inside the figure of the
+ same font type and attributes as the surrounding text, which looks so much
+ more professional.
+ With Inkscape, even the 
+\emph on
+size
+\emph default
+ of the font is taken from the surrounding LaTeX document – in Xfig, you
+ have to set it by hand to be the same.
+ Of course you can always embed LaTeX modifiers like 
+\family typewriter
+
+\backslash
+Large
+\family default
+, 
+\family typewriter
+
+\backslash
+small
+\family default
+, etc.
+ into your figure text strings.
+\end_layout
+
+\end_deeper
+\end_deeper
 \begin_layout Standard
 \begin_inset Float table
 placement !h
diff --git a/lib/doc/workaround.png b/lib/doc/workaround.png
new file mode 100644
index 0000000..9e568b0
Binary files /dev/null and b/lib/doc/workaround.png differ
diff --git a/lib/external_templates b/lib/external_templates
index bacaf3e..61a910b 100644
--- a/lib/external_templates
+++ b/lib/external_templates
@@ -290,6 +290,48 @@ Template XFig
 TemplateEnd
 
 
+Template Inkscape
+       GuiName "Inkscape figure"
+       HelpText
+               An Inkscape figure.
+               This template should work flawlessly with Inkscape 0.48. 
+               For Inkscape 0.91, there is a bug in the LaTeX + PDF output
+               which makes a workaround necessary, see Embedded Objects manual.
+       HelpTextEnd
+       InputFormat svg
+       FileFilter "*.svg"
+       AutomaticProduction true
+       Transform Rotate
+       Preview InstantPreview
+       Format PDFLaTeX
+               TransformCommand Rotate RotationLatexCommand
+               # Resize is not desirable here, as fonts are sized to 
surrounding LaTeX font
+               Product 
"$$RotateFront\\input{$$AbsOrRelPathMaster$$Basename.pdf_tex}$$RotateBack"
+               UpdateFormat pdftex
+               UpdateResult "$$AbsPath$$Basename.pdf_tex"
+               Requirement "color"
+               Requirement "graphicx"
+               # Preamble WarnNotFound
+               # Preamble InputOrWarn
+               ReferencedFile pdflatex "$$AbsOrRelPathMaster$$Basename.pdf_tex"
+               ReferencedFile pdflatex "$$AbsPath$$Basename.pdf"
+       FormatEnd
+       Format LaTeX
+               TransformCommand Rotate RotationLatexCommand
+               Product 
"$$RotateFront\\input{$$AbsOrRelPathMaster$$Basename.eps_tex}$$RotateBack"
+               UpdateFormat pstex
+               UpdateResult "$$AbsPath$$Basename.eps_tex"
+               Requirement "color"
+               Requirement "graphicx"
+               # Preamble WarnNotFound
+               # Preamble InputOrWarn
+               ReferencedFile latex "$$AbsOrRelPathMaster$$Basename.eps_tex"
+               ReferencedFile latex "$$AbsPath$$Basename.eps"
+               ReferencedFile dvi   "$$AbsPath$$Basename.eps"
+       FormatEnd
+TemplateEnd
+
+
 Template ChessDiagram
        GuiName "Chess diagram"
        HelpText
diff --git a/lib/scripts/svg2pdftex.py b/lib/scripts/svg2pdftex.py
new file mode 100644
index 0000000..cd9a484
--- /dev/null
+++ b/lib/scripts/svg2pdftex.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# file svg2pdftex.py
+#
+# This script converts an SVG image to something that pdflatex can process
+# into high quality PDF.
+
+# Usage:
+#   python svg2pdftex.py ${base}.svg ${base}.pdft
+# This command generates
+#   ${base}.pdf  the converted pdf file
+#   ${base}.pdft a tex file that can be included in your latex document
+#       using '\input{${base}.pdft}'
+#
+# Note:
+#   Do not use this command as
+#     python svg2pdftex.py file.svg file.pdf
+#   the real pdf file will be overwritten by a tex file named file.pdf.
+#
+
+
+
+import os, sys, re
+
+
+def runCommand(cmd):
+    ''' Utility function:
+        run a command, quit if fails
+    '''
+    if os.system(cmd) != 0:
+        print "Command '%s' fails." % cmd
+        sys.exit(1)
+
+
+# We expect two args, the names of the input and output files.
+if len(sys.argv) != 3:
+    sys.exit(1)
+
+input, output = sys.argv[1:]
+
+# Fail silently if the file doesn't exist
+if not os.path.isfile(input):
+    sys.exit(0)
+
+# Strip the extension from ${output}
+outbase = os.path.splitext(output)[0]
+
+
+
+# Inkscape 0.48 can output the image as a PDF file ${base}.pdf and place the 
text 
+# in a LaTeX file ${base}.pdf_tex, which is renamed to ${output}, for 
typesetting 
+# by pdflatex itself. 
+runCommand('inkscape --file=%s --export-pdf=%s.pdf --export-latex' % (input, 
outbase))
+
+os.rename('%s.pdf_tex' % outbase, output)
+
diff --git a/lib/scripts/svg2pstex.py b/lib/scripts/svg2pstex.py
new file mode 100644
index 0000000..72d3074
--- /dev/null
+++ b/lib/scripts/svg2pstex.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# file svg2pstex.py
+#
+# This script converts an SVG image to something that latex can process
+# into high quality PostScript.
+
+# Usage:
+#   python svg2pstex.py ${base}.fig ${base}.pstex
+# This command generates
+#   ${base}.eps    the converted eps file
+#   ${base}.pstex  a tex file that can be included in your latex document
+#       using '\input{${output}}'.
+#
+# Note:
+#   Do not use this command as
+#     python svg2pstex.py file.fig file.eps
+#   the real eps file will be overwritten by a tex file named file.eps.
+#
+
+import os, sys
+
+def runCommand(cmd):
+    ''' Utility function:
+        run a command, quit if fails
+    '''
+    if os.system(cmd) != 0:
+        print "Command '%s' fails." % cmd
+        sys.exit(1)
+
+# We expect two args, the names of the input and output files.
+if len(sys.argv) != 3:
+    sys.exit(1)
+
+input, output = sys.argv[1:]
+
+# Fail silently if the file doesn't exist
+if not os.path.isfile(input):
+    sys.exit(0)
+
+# Strip the extension from ${output}
+outbase = os.path.splitext(output)[0]
+
+# Inkscape 0.48 can output the image as a EPS file ${base}.pdf and place the 
text 
+# in a LaTeX file ${base}.eps_tex, which is renamed to ${output}, for 
typesetting 
+# by latex itself. 
+runCommand('inkscape --file=%s --export-eps=%s.eps --export-latex' % (input, 
outbase))
+os.rename('%s.eps_tex' % outbase, output)

Reply via email to