![使用 knitr 生成密集的 TikZ 图形(14500 点散点图)时出现内存问题](https://linux22.com/image/294246/%E4%BD%BF%E7%94%A8%20knitr%20%E7%94%9F%E6%88%90%E5%AF%86%E9%9B%86%E7%9A%84%20TikZ%20%E5%9B%BE%E5%BD%A2%EF%BC%8814500%20%E7%82%B9%E6%95%A3%E7%82%B9%E5%9B%BE%EF%BC%89%E6%97%B6%E5%87%BA%E7%8E%B0%E5%86%85%E5%AD%98%E9%97%AE%E9%A2%98.png)
我的文档中有一系列图表,这些图表都可以用 knitr 正确绘制dev=tikz
。然而,图 7 的数据点密度比其他图表高得多,foo
但并非如此:
label: foo (with options)
List of 3
$ include : logi TRUE
$ fig.width : num 3
$ fig.height: num 3
Quitting from lines 142-149 (myfile.Rnw) # These are chunk's first and list line
Error: failed to compile figure/foo.tikz to PDF
Execution halted
删除所有附件文件、缓存和图形目录并重新运行knitr
并不能解决问题。
我听从了这个答案:
figure/foo.log
结论是
[...other stuff...]
Package microtype Info: Loading generic settings for font family
(microtype) `zplm' (encoding: OT1).
(microtype) For optimal results, create family-specific settings.
(microtype) See the microtype manual for details.
Package microtype Info: Loading generic settings for font family
(microtype) `cmss' (encoding: OT1).
(microtype) For optimal results, create family-specific settings.
(microtype) See the microtype manual for details.
\c__siunitx_mathtt_int=\count441
Package microtype Info: Loading generic settings for font family
(microtype) `cmtt' (encoding: OT1).
(microtype) For optimal results, create family-specific settings.
(microtype) See the microtype manual for details.
Runaway definition?
->
! TeX capacity exceeded, sorry [main memory size=5000000].
\pgf@sys@bp ...rrentprotocol {\the \pgfutil@toks@
}}
l.13807 ...=0.30] ( 49.08, 79.20) circle ( 2.13);
If you really absolutely need more capacity,
you can ask a wizard to enlarge me.
Here is how much of TeX's memory you used:
57490 strings out of 493734
1253899 string characters out of 6146348
5000001 words of memory out of 5000000
60062 multiletter control sequences out of 15000+600000
16802 words of font info for 45 fonts, out of 8000000 for 9000
1328 hyphenation exceptions out of 8191
59i,3n,92p,10362b,776s stack positions out of 5000i,500n,10000p,200000b,80000s
No pages of output.
我没发现什么问题figure/foo.tikz
运行后xelatex foo.tikz
我得到了类似的结论
[...other stuff...]
Runaway definition?
->
! TeX capacity exceeded, sorry [main memory size=5000000].
\pgf@sys@bp ...rrentprotocol {\the \pgfutil@toks@
}}
l.13807 ...=0.30] ( 49.08, 79.20) circle ( 2.13);
No pages of output.
问题似乎与内存有关(不是吗?)。那么我该如何修复它?
编辑:
我能够用 MWE 复制该错误
\documentclass[a4paper,12pt,twoside]{article}
\begin{document}
<<opt, include=FALSE, cache=TRUE, cache.lazy=FALSE>>=
library(ggplot2)
# Global options
options(tikzDefaultEngine='xetex')
opts_chunk$set(echo=FALSE, results=FALSE, message=FALSE, warning=FALSE, include=FALSE,
cache=TRUE, cache.lazy=FALSE,
dev='tikz', out.width='.90\\linewidth', fig.show='asis')
theme_set(theme_classic())
@
<<foo, include=TRUE, fig.width=3, fig.height=3>>=
set.seed(99)
data <- data.frame(vector1 = as.integer(rlnorm(14500, sdlog = 2)),
vector2 = as.integer(rnorm(14500)))
ggplot(data, aes(vector1, vector2)) + geom_point(alpha=.2)
@
\end{document}
vector1
通过将和的长度减少vector2
到 14,我得到了我的图,那么它显然是一个内存问题,特别是由产生的问题,dev=tikz
因为我可以在不使用 TikZ 设备的情况下生成具有相同 14500 长度向量的图。
答案1
在这种情况下,你基本上没运气。你不能使用tikz
R 中的设备绘制包含太多图形元素的图。你必须简化你的图,或者使用另一个图形设备(例如默认的pdf
;如果你真的非常关心字体系列,你可以看看这篇博文)。