我正在写博士论文,有很多图表。我想要的是:
- 使用外部化的 tikz,因为图表保存为 .dat,而且有很多,
- 将外部文件存储在明确的目录中,
- 能够轻松设置 tikz 图形大小,
- 能够切换到
\includegraphics
生成的pdf(因为我把它放在我的实验室sharelatex中,它不支持shell-escape)。
我的答案是:
- 使用 tikz/pgfplots 外部化库,
- 使用设置文件名
\tikzsetnextfilename
, - 使用包设置图像宽度
tikzscale
, - 制作我自己的
\includetikz
,如下所示(在序言内)。
% Tikz & co
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{tikzscale}
% Tikz libraries
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{patterns}
\usetikzlibrary{babel}
% Externalized tikz
\usepgfplotslibrary{external}
\tikzexternalize
\tikzexternaldisable
% Command to include and controll the tikz figure size
%
% Use it as \includetikz[width=0.2\textwidth]{/path/to/file}{myfile}
% with /path/to/file/myfile.tikz being a file.
%
\def\usepdf{}
\newcommand{\includetikz}[3][]{%
% Get file extension
\filename@parse{#2/#3}
%
% Check if tikz should be built or not.
\ifx\usepdf\empty
% Enable externalization.
\tikzexternalenable
% Set next file name.
\tikzsetnextfilename{#2/build/\filename@base}
% Include graphics
\includegraphics[#1]{#2/\[email protected]}
% Disable externalization.
\tikzexternaldisable
\else
\includegraphics[#1]{#2/build/\[email protected]}
\fi
}
\usepackage{style/tikz}
此命令在文件内部用作
\def\path{img/chapitre2}
\includetikz[width=0.8\textwidth, height=0.4\textwidth]{\path}{eels_spectra.tex}
替换eels_spectra.tikz
为:
\begin{tikzpicture}
\begin{axis}[]
\addplot [] table [x=eV, y=loc3] {\path/data/eels_spectra.dat};
\end{axis}
\end{tikzpicture}
最后,.dat 文件如下
eV loc1 loc2 loc3
463.57605489759953 1168.0 1127.0 1146.0
463.8996794786981 1292.0 1038.0 1285.0
464.22330405979665 1289.0 979.0 1155.0
464.5469286408952 1339.0 1088.0 1229.0
...
我注意到了如果将 .dat 中的文本放在命令中,则不会出现\addplot
问题,但是使用\path/data/eels_spectra.dat
,pdflatex 命令给我:
===== 'mode=convert with system call': Invoking 'pdflatex -shell-escape -halt-o
n-error -interaction=batchmode -jobname "img/chapitre2/build/eels_spectra" "\de
f\tikzexternalrealjob{main}\input{main}"' ========
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
\write18 enabled.
entering extended mode
system returned with code 256
! Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e
rror -interaction=batchmode -jobname "img/chapitre2/build/eels_spectra" "\def\t
ikzexternalrealjob{main}\input{main}"' did NOT result in a usable output file '
img/chapitre2/build/eels_spectra' (expected one of .pdf:.jpg:.jpeg:.png:). Plea
se 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 'img/chapitre2
/build/eels_spectra.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.47 \end{tikzpicture}
?
作业日志写道:
Package pdftex.def Info: img/chapitre2/eels_spectra_haadf.png used on input li
ne 86.
(pdftex.def) Requested size: 193.47581pt x 107.86781pt.
(./img/chapitre2/eels_spectra.tikz
PGFPlots: reading {/home/etienne/ownCloud/Manuscrit/eels_spectra.dat}
) (./img/chapitre2/eels_spectra.tikz
PGFPlots: reading {/home/etienne/ownCloud/Manuscrit/eels_spectra.dat}
) (./img/chapitre2/eels_spectra.tikz
PGFPlots: reading {/home/etienne/ownCloud/Manuscrit/eels_spectra.dat}
) (./img/chapitre2/eels_spectra.tikz
\openout4 = `img/chapitre2/build/eels_spectra.dpth'.
PGFPlots: reading {/home/etienne/ownCloud/Manuscrit/eels_spectra.dat}
! Argument of \path has an extra }.
<inserted text>
\par
l.35 ...enne/ownCloud/Manuscrit/eels_spectra.dat};
Here is how much of TeX's memory you used:
54569 strings out of 492167
1263128 string characters out of 6123477
2177993 words of memory out of 5000000
57937 multiletter control sequences out of 15000+600000
590919 words of font info for 79 fonts, out of 8000000 for 9000
1141 hyphenation exceptions out of 8191
89i,20n,125p,10125b,2476s stack positions out of 5000i,500n,10000p,200000b,80000s
! ==> Fatal error occurred, no output PDF file produced!
在这种情况下我该如何导入文件.dat
?导入 .tikz 中的 .dat 太重了!!!
编辑
我忘了说,如果\tikzsetnextfilename{#2/build/\filename@base}
在定义中删除了,.dat 文件就会被正确导入\includetikz
。即定义作业名称会导致 .dat 导入崩溃!
答案1
好吧,好吧,好吧...没有人被我的问题所启发。
这是我找到的答案。
听起来好像\tikzsetnextfilename
不需要指定目录。应该使用前缀。换句话说:
- 使用前缀指定应保存作业的目录,
- 用于
\tikzsetnextfilename
指定作业名称。
我所做的是定义以下命令
\newcommand{\setpath}[1]{% Input like /path/to/file
\graphicspath{{#1/}}% sets /path/to/file/
\def\tikzpath{#1}% sets /path/to/file
}
因此,在每一章的开头,我都用来\setpath{/path/to/chapter/images}
定义图像的当前路径(png 作为 tikz)。
命令 includetikz`变成:
\def\usepdf{}
\def\tikzpath{}
\newcommand{\includetikz}[2][]{%
% Get file extension
\filename@parse{\tikzpath/#2}
%
% Check if tikz should be built or not.
\ifx\usepdf\empty
% Enable externalization.
\tikzexternalenable
% Set file prefix
\tikzsetexternalprefix{\tikzpath/build/}
% Set next file name.
\tikzsetnextfilename{\filename@base}
% Include graphics
\includegraphics[#1]{\tikzpath/\[email protected]}
% Disable externalization.
\tikzexternaldisable
\else
\includegraphics[#1]{\tikzpath/build/\[email protected]}
\fi
}
我现在在每一章里面都包含了 tikz 图像\includetikz[width=0.8\textwidth]{my_image.tikz}
。
使用前缀管理外部化解决了我的.dat 文件导入问题。