使用 `-output-directory` 和输入时从 tikz-externalize 获取有效的 makefile

使用 `-output-directory` 和输入时从 tikz-externalize 获取有效的 makefile

以下 mwe 在与 选项一起使用时会产生无效的 makefile latexmk-output-directory(示例来自手册pgfplots

latexmk mwe.tex -output-directory=build

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{external}
\tikzexternalize[mode=list and make]

\begin{document}

\input{tikz/figure.tikz}

\end{document}

以及下面的文件tikz/figure.tikz

\begin{figure}
\begin{tikzpicture}
\begin{axis}
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\caption{Our first external graphics example}
\end{figure}

makefile 位于build/mwe.makefile

ALL_FIGURE_NAMES=$(shell cat mwe.figlist)
ALL_FIGURES=$(ALL_FIGURE_NAMES:%=%.pdf)

allimages: $(ALL_FIGURES)
    @echo All images exist now. Use make -B to re-generate them.

FORCEREMAKE:

include $(ALL_FIGURE_NAMES:%=%.dep)

%.dep:
    mkdir -p "$(dir $@)"
    touch "$@" # will be filled later.

mwe-figure0.pdf: 
    pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "mwe-figure0" "\def\tikzexternalrealjob{mwe}\input{mwe}"

mwe-figure0.pdf: mwe-figure0.md5

由于mwe.tex文件不在目录中,因此从目录build运行命令将不起作用;另外,从项目的“根”目录运行该命令也将不起作用,因为那里没有文件。makebuildmwe.figlist

答案1

这个解决方案是一种肮脏的黑客行为,所以我不会接受它,希望有人能提供更好的答案。

我目前采用的解决方法是修改用于创建 makefile 的命令;这个想法来自这里并修改命令,包括向上移动目录cd .. &&并设置输出目录--output-directory=./build如下:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{external}
\tikzexternalize[mode=list and make]

\tikzset{%
    external/system call={cd .. && pdflatex \tikzexternalcheckshellescape --halt-on-error --interaction=batchmode --output-directory=./build --jobname "\image" "\texsource"},
    /pgf/images/include external/.code={%
        \includegraphics{build/#1}%
    },
}
\begin{document}

\input{tikz/figure.tikz}

\end{document}

生成新的 makefile


ALL_FIGURE_NAMES=$(shell cat mwe.figlist)
ALL_FIGURES=$(ALL_FIGURE_NAMES:%=%.pdf)

allimages: $(ALL_FIGURES)
        @echo All images exist now. Use make -B to re-generate them.

FORCEREMAKE:

include $(ALL_FIGURE_NAMES:%=%.dep)

%.dep:
        mkdir -p "$(dir $@)"
        touch "$@" # will be filled later.

mwe-figure0.pdf: 
        cd .. && pdflatex -shell-escape --halt-on-error --interaction=batchmode --output-directory=./build --jobname "mwe-figure0" "\def\tikzexternalrealjob{mwe}\input{mwe}"

mwe-figure0.pdf: mwe-figure0.md5

现在可以从build目录中构建make -f mwe.makefile

相关内容