使用 lualatex 結合自動 inkscape 導入進行 tikz 外部化

使用 lualatex 結合自動 inkscape 導入進行 tikz 外部化

我有一份包含大量數字的龐大文件。對於繪圖,我使用 matlab 並使用 matlab2tikz 將繪圖轉換為 tikz(然後進行一些修改),對於大多數其他圖形,我更喜歡 Inkscape 及其 pdf_tex 導出。對於 tikz 和 Inkscape 來說,如果圖形中確實發生了變化,我喜歡自動重新生成圖形。我有兩個自訂指令,\includetikz 和 \includesvg。一切都運行得很好,直到我不得不將 tikz-figures 的 pdflatex 更改為 lualatex。我有一些圖,我無法進一步減少數據,我沒有設法用 pdflatex 排版這些數字(也增加了記憶體設定)。對於 lualatex,這效果非常好。現在的問題是,帶有 lualatex 的 \includetikz 命令以某種方式乾擾了我的 \includesvg 命令。

我有以下 MWE:

\documentclass[11pt,a4paper,english]{scrbook}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\RequirePackage{luatex85}

% inkscape
\usepackage{import}
\newcommand{\executeiffilenewer}[3]{%
    \ifnum\pdfstrcmp{\pdffilemoddate{#1}}%
    {\pdffilemoddate{#2}}>0%
    {#3}\fi%
}
\newcommand{\includesvg}[2]{%
    \immediate\write18{if not exist #1.svg_external/ (mkdir "#1.svg_external/")}%
    \executeiffilenewer{#1#2.svg}{#1.svg_external/#2.pdf}{%
        \immediate\write18{"C:/Program Files (x86)/Inkscape/inkscape.exe" -z -C --file=#1#2.svg --export-pdf=#1.svg_external/#2.pdf --export-latex}}%
    \import{#1.svg_external/}{#2.pdf_tex}%
}

\usepackage{filemod}
\newlength{\figheight}
\newlength{\figwidth}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usepgfplotslibrary{external}
\pgfplotsset{compat=1.14}
\tikzexternalize % activate externalization
\tikzset{external/system call={lualatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}}
\tikzset{external/figure list=true}
\tikzset{external/up to date check=simple}
\newcommand{\tikzcustomremake}[2]{\tikzset{external/remake next}}
\newcommand{\includetikz}[2]{%
    \tikzsetnextfilename{#1.tikz_external/#2}%
    \filemodCmp{#1#2.tikz}{#1.tikz_external/#2.pdf}%
    {\tikzcustomremake{#1}{#2}}{}%
    \input{#1#2.tikz}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{document}

\figwidth=5cm
\figheight=5cm
\includetikz{fig/ch1/}{plot2}

%\footnotesize\includesvg{fig/ch1/}{inkscape1}

\end{document}

不幸的是,Inkscape 命令包含絕對路徑,因此您可能需要更改它。 plot2.tikz 的程式碼如下:

\begin{tikzpicture}
\begin{axis}[%
width=\figwidth,
height=\figheight,
xmin=0,xmax=10,
xlabel=\pgfactualjobname,
ymin=-1,ymax=5,
axis background/.style={fill=white}
]
\addplot [color=red,solid,forget plot]
  table[row sep=crcr]{%
0   0\\
10  1\\
};
\end{axis}
\end{tikzpicture}%

檔案 inkscape1.svg 可以是任何 svg,例如:https://de.wikipedia.org/wiki/LaTeX#/media/File:LaTeX_logo.svg

我現在的問題是:如果我按原樣運行程式碼,則一切正常。 svg 被註解掉,產生的 pdf 僅包含繪圖。之後,再次刪除 \includesvg-command 的註釋,一切正常。 pdf_tex 已生成,並且在 pdf 中有兩個數字。但是,如果我在 tikz 檔案中進行一些更改,以致必須重新生成圖形,則 lualatex 會失敗。

日誌檔對我沒有任何幫助。在主文檔的日誌檔案中,錯誤部分如下:

Writing 'fig/ch1/.tikz_external/plot2' to 'diss01lua.figlist'.
\openout3 = `diss01lua.auxlock'.

===== 'mode=convert with system call': Invoking 'lualatex -shell-escape -halt-o
n-error -interaction=batchmode -jobname "fig/ch1/.tikz_external/plot2" "\def\ti
kzexternalrealjob{diss01lua}\input{diss01lua}"' ========
runsystem(lualatex -shell-escape -halt-on-error -interaction=batchmode -jobname
 "fig/ch1/.tikz_external/plot2" "\def\tikzexternalrealjob{diss01lua}\input{diss
01lua}")...executed.

\openout3 = `diss01lua.auxlock'.


! Package tikz Error: Sorry, the system call 'lualatex -shell-escape -halt-on-e
rror -interaction=batchmode -jobname "fig/ch1/.tikz_external/plot2" "\def\tikze
xternalrealjob{diss01lua}\input{diss01lua}"' did NOT result in a usable output 
file 'fig/ch1/.tikz_external/plot2' (expected one of .pdf:.jpg:.jpeg:.png:). Pl
ease verify that you have enabled system calls. For pdflatex, this is 'pdflatex
 -shell-escape'. Sometimes it is also named 'write 18' or something like that. 
Or maybe the command simply failed? Error messages can be found in 'fig/ch1/.ti
kz_external/plot2.log'. If you continue now, I'll try to typeset the picture.

See the tikz package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.16 \end{tikzpicture}
                      %
This error message was generated by an \errmessage
command, so I can't give any explicit help.
Pretend that you're Hercule Poirot: Examine all clues,
and deduce the truth by order and method.

plot2 日誌檔的最後幾行是:

LaTeX Font Info:    External font `lmex10' loaded for size
(Font)              <10.95> on input line 15.
LaTeX Font Info:    External font `lmex10' loaded for size
(Font)              <8> on input line 15.
LaTeX Font Info:    External font `lmex10' loaded for size
(Font)              <6> on input line 15.
[1

{C:/ProgramData/MiKTeX/2.9/pdftex/config/pdftex.map}])
if not exist fig/ch1/.svg_external/ (mkdir "fig/ch1/.svg_external/")
! Undefined control sequence.
\executeiffilenewer #1#2#3->\ifnum \pdfstrcmp 
                                              {\pdffilemoddate {#1}}{\pdffil...
l.51 \footnotesize\includesvg{fig/ch1/}{inkscape1}


Here is how much of LuaTeX's memory you used:
 25909 strings out of 494568
 100000,953878 words of node,token memory allocated
 1158 words of node memory still in use:
   4 hlist, 1 rule, 7 disc, 1 local_par, 1 dir, 23 glue, 4 kern, 2 penalty, 85 g
lyph, 7 attribute, 47 glue_spec, 7 attribute_list, 1 if_stack nodes
   avail lists: 2:739,3:212,4:5,5:13,7:182,8:2,9:78,10:1
 29028 multiletter control sequences out of 65536+200000
 27 fonts using 1573183 bytes
 65i,11n,111p,8876b,1869s stack positions out of 5000i,500n,10000p,200000b,50000s
!  ==> Fatal error occurred, no output PDF file produced!

我發現有趣的是,lualatex 以某種方式看到了 inkscape 的東西,但我不明白。

我在這裡上傳了完整的日誌文件,如果它們有任何幫助的話:http://www.uni-ulm.de/~pqa62/tex/

有人有想法並知道如何解決這種行為嗎?我真的很喜歡我進行外部化和自動重建的方式,特別是因為在我的包含命令的完全實現中涵蓋了一些更高級的東西,而且因為一切都有效,直到我不得不切換到 lualatex。

編輯:

我做了更多測試,發現自訂命令\executeiffilenewer顯然是問題所在,為什麼會發生錯誤。不知何故,lualatex(應該只排版 tikz-plot)也會查看以下\includesvg命令並嘗試執行它。在這裡,它不知道這個自訂命令,因為它沒有在其呼叫中定義。如果我\includesvg像這樣修改命令:

\newcommand{\includesvg}[2]{%
    \immediate\write18{"C:/Program Files (x86)/Inkscape/inkscape.exe" -z -C --file=#1#2.svg --export-pdf=#1.svg_external/#2.pdf --export-latex}%
    \import{#1.svg_external/}{#2.pdf_tex}%
}

我不再收到錯誤了。問題是,它總是執行 Inkscape。儘管如此,我還是不明白為什麼 lualatex 在 tikz-plot 完成後不會停止。

答案1

如果您更改-interaction=batchmode為並在文件-interaction=scrollmode中添加一些錯誤或\show命令,您可以看到 luatex 運行編譯了整個 tex 檔案。在此期間它擴展了命令——它能夠在 \includetikz 中找到圖片,所以自然它也必須在 \includesvg 中找到。這意味著 luatex 不知道的命令可能會導致問題。

\pdfstrcmpluatex 中沒有定義。您可以載入 pdftexcmds 套件來取得實現https://tex.stackexchange.com/a/158612/2388。注意:我沒有檢查 pdftexcmds 是否已適應 luatex 0.95/1.0,但在我的精簡示例中它似乎可以工作。

\documentclass[11pt,a4paper,english]{scrbook}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{ifluatex}
\ifluatex
\RequirePackage{luatex85}
\usepackage{pdftexcmds}
  \makeatletter
  \let\pdfstrcmp\pdf@strcmp
  \let\pdffilemoddate\pdf@filemoddate
  \makeatother
\fi

\newcommand{\includesvg}{\pdfstrcmp{1}{2}}

\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize % activate externalization
\tikzset{external/system call={lualatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}}
\tikzset{external/force remake}

\newcommand{\includetikz}{%
 \begin{tikzpicture}
  \draw[red](0,0)--(1,1);
 \end{tikzpicture}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{document}

blub 

\includesvg

\includetikz

\end{document}

相關內容