工作示例

工作示例

我将 PDFLaTeX 文档组织成文件夹。文件夹里面有.tex文件、.PDF图形文件。所有工作都使用包中的\import命令:\subimportImport

Main/
 |-- main.tex
 |-- work.svg
 |-- Chapter1/  
      |-- body.tex
      |-- notwork.svg
      |-- figure.PDF
      |-- figure.SVG

因此main.tex

...
\import{Chapter1/}{body.tex} %% OK
... 

body.tex

...
\includegraphics{figure.PDF} %% OK (so relative path is working)
...

现在,我想使用 SVG 图形和 InkScape 并按照说明自动执行该过程:

https://ctan.org/tex-archive/info/svg-inkscape/InkscapePDFLaTeX.pdf

它使用调用的\pdffilemoddate命令。\executeiffilenewer\includesvg

如果我改变body.tex

...
\includesvg{figure}
...

然后\executeiffilenewer宏不会调用 InkScape,因为\pdffilemoddate找不到文件(如果figure.SVGfigure.PDF位于主文件夹中它可以工作,所以是文件路径的问题)。

我该如何处理这个问题?

我想我需要访问由 构造的实际路径\import来修改\executeiffilenewer,但我不知道如何做。

工作示例

您需要另外 2 个 SVG 文件(work.SVG位于notwork.SVG上面的树中)

文件Main.TeX

\documentclass[10pt]{minimal}

\usepackage{import}
\usepackage[pdftex]{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 --export-area-drawing}
  \import{}{#1.pdf_tex}
}

%% This is a not working try
\newcommand{\includepathsvg}[2]{
  \executeiffilenewer{#1#2.svg}{#1#2.pdf}
    {inkscape -z -D --file=#2.svg --export-pdf=#2.pdf --export-latex --export-area-drawing}
  \import{#1}{#2.pdf_tex}
}

\begin{document}

%% Delete all PDF figure files in Main and Chapter1 folder and uncomment some lines to test:

%SVG: \includesvg{work}
%SVG: \includesvg{Chapter1/notwork} %% InkScape invoked but notwork.pdf_tex can't find notwork.pdf because unknown relative path
%SVG: \includepathsvg{Chapter1/}{notwork} %% InkScape not invoked becaude \pdffilemoddate can't find notwork.svg

File Body.tex:

\import{Chapter1/}{body.TeX}

\end{document}

Body.TeX文件夹中的文件Chapter1

%% Delete all PDF figure files in Main and Chapter1 folder and uncomment some lines to test:

%SVG: \includesvg{Chapter1/notwork} %% WORKS but I have to include the relative path 
%SVG: \includesvg{notwork} %% InkScape not invoked becaude \pdffilemoddate can't find notwork.svg
%SVG: \includepathsvg{Chapter1/}{notwork} %% Problem with path: Chapter1/notwork.svg when invoking InkScape

答案1

好的,我的问题已经解决了。以下是在示例中要做的更改:

\makeatletter
  \def\relativepath{\import@path}
\makeatother

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

相关内容