[Tex/LaTex] svg from inkscape — doesn’t work in xelatex

inkscapesvgxetex

I want to make use of "svg in tex" technology.

Here's a tex:

\documentclass{article}
\usepackage{color}
\usepackage{graphicx}

\newcommand{\executeiffilenewer}[3]{%
    \ifnum\pdfstrcmp{\pdffilemoddate{#1}}%
        {\pdffilemoddate{#2}}>0{\immediate\write18{#3}}
    \fi}

\newcommand{\includesvg}[1]{%
    \executeiffilenewer{#1.svg}{#1.pdf}%
        {inkscape -z -D --file=#1.svg --export-pdf=#1.pdf --export-latex}%
    \input{#1.pdf_tex}}

\begin{document}
\begin{figure}
    \centering
    %\def\svgwidth{\columnwidth}
    \includesvg{svgfig}
    \caption{My SVG Image}
\end{figure}
\end{document}

One can take any svg image.

Then:

pdflatex -shell-escape tex-file

works, but

xelatex -shell-escape tex-file

and

xelatex -shell-escape -8bit tex-file

both give error:

! Undefined control sequence.
\executeiffilenewer #1#2#3->\ifnum \pdfstrcmp 
                                              {\pdffilemoddate {#1}}{\pdffil...
l.19     \includesvg{svgfig}

Edit:

Currently I prepare pdfs from svg before compiling then doc, so I do have "svgs in xetex". I even automated the thing in bash, so I'm pretty happy. Nevertheless, all problems should be solved, and all issues cleared — so if someone aware of XeTeX internals thinks something is wrong — it sould be reported and fixed.

Edit 2:

The script I'm using is too opinioned. Here's basic simple script which does the job:

#!/bin/bash
# to get description use the -h flag

# exit after a single error:
set -e


# ==========
# preambula:

PROGNAME=${0##*/}
PROGVERSION=1.12

TeX=`find . -maxdepth 1 -mindepth 1 -type f -name '*.tex' | sort | head -1`

usage()
{
cat << EO
Usage: $PROGNAME [OPTIONS...]

Script to build xelatex documents. It expects document following a particular rules.


Options
=======

EO

cat << EO | column -s\& -t

  -t, --tex & TeX file (default to first TeX in the dir, currently it is: $TeX)
  -i, --index & also build index: xelatex-(biber-xelatex)-makeindex-xelatex-xelatex
  -e, --extra & perform and extra XeLaTeX run
 
  -h, --help & show this output
  -v, --version & show version information
EO
}

SHORTOPTS="hvt:e"
LONGOPTS="help,version,tex:,extra"

ARGS=$(getopt -s bash --options $SHORTOPTS --longoptions $LONGOPTS --name $PROGNAME -- "$@")

eval set -- "$ARGS"

while true; do
    case $1 in

        -t|--tex)
            TeX=$2; shift;;
        -e|--extra)
            Extra=true;;

        -h|--help)
            usage; exit 0;;
        -v|--version)
            echo "$PROGVERSION"; exit 0;;
        --)
            shift; break;;
        *)
            shift; break;;
    esac
    shift
done


# ===========
## variables:

TexBaseName=`basename ${TeX%.tex}`


# =====================
## updating svg images:

find . -type f -name '*.svg' | while read svgFile; do
    svgBasename=`basename $svgFile`
    svgBasenameNoExt=${svgBasename%svg}
    svgDir=`dirname $svgFile`
    pdfFile=${svgDir}/${svgBasenameNoExt}pdf
    if [ ! -e $pdfFile -o $svgFile -nt $pdfFile ]; then
        echo -e "${PROGNAME}: $svgFile is newer!"
        inkscape -z -D --file=$svgFile --export-pdf=$pdfFile --export-latex
    fi
done


# ==========
## building:

# initial build
xelatex -shell-escape "${TeX}"

# bibliography: run biber + xelatex if there's a bcf file.
# And bcf file is there if you have loaded biblatex.
Bib=`find . -type f -name '*.bib'`
if [ -n "$Bib" ]; then
    biber `ls *.bcf` || echo $?
    # setting links:
    xelatex -shell-escape "${TeX}"
fi

# setting links:
xelatex -shell-escape "${TeX}"

# extra build?
if $Extra; then
    xelatex -shell-escape "${TeX}"
fi


# ===========
## reporting:

echo -e "${PROGNAME}: Done."

I called it bk_XeLaTeXs_simple.bash. In order to build a doc just run this one in the dir of your master tex file. This script assumes the first tex file in the dir to be a master tex file, but you can also set it with the -t/--tex option.

The script looks for a svg files, and compares each of them with a corresponding pdf file: if svg is newer (or pdf does not exists), then script uses inkscape to re-generate pdf file. So to load an svg file called x-y-sigma.svg one would write:

\begin{figure}
  \centering
  \def\svgwidth{0.45\columnwidth}
  \input{x-y-sigma.pdf_tex}
  \caption{Test svg image.\label{fig:x-y-sigma}}
\end{figure}

This script also calls biber on *bcf files if you have loaded biblatex in you sources.

Finally it has -e/--extra option for an extran xelatex build.

Here's a zip of the folder with all the files needed for a complete MWE with svg and bibliography.

Best Answer

There are two issues; the XeTeX string comparing primitive is called \strcmp not \pdfstrcmp, so that is easy to fix (a more portable way is to use \pdf@strcmp from pdftexcmds package, which handle engine differences).

The other problem is the lack of a XeTeX implementation of \pdffilemoddate. I opened a XeTeX ticket, for when I have time to look into it, but patches are welcomed of course.