如果 Knitr 在创作 tikz 图形时意外退出,如何调试

如果 Knitr 在创作 tikz 图形时意外退出,如何调试

我有一个.Rnw文件,Knitr在我后端数据集发生变化之前,它编译得非常好。可能那时图表的大小也发生了变化。由于我不明白的原因,knitr现在突然停止编译并LaTex开始编译.texKnitr 生成的未完成文件。我猜这是发生了什么,因为我在活动监视器(ESS 缓冲区)中看到日志被knitr替换为日志LaTex。接下来我粘贴了日志的最后一部分,其中正常knitr过程的最后几行,该过程加载了一个 R 脚本来生成 ggplot,被替换为LaTex...

  grid.arrange(gp2,gp1)

   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
##------ Thu Oct 10 18:39:42 2013 ------##

\documentclass[nohyper]{tufte-handout}

\nonstopmode

\usepackage{tikz}

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.26 \usepackage
                {tikz} 
Here is how much of TeX's memory you used:
 15456 strings out of 493311
 278701 string characters out of 6137850
 379859 words of memory out of 5000000
 18583 multiletter control sequences out of 15000+600000
 4403 words of font info for 15 fonts, out of 8000000 for 9000
 957 hyphenation exceptions out of 8191
 56i,0n,92p,470b,151s stack positions out of 5000i,500n,10000p,200000b,80000s

!  ==> Fatal error occurred, no output PDF file produced!


Quitting from lines 1-81 (my/path/time_series.R) 
Quitting from lines 84-NA (my/path/time_series.R) 
Error in getMetricsFromLatex(TeXMetrics) (from #91) : 
TeX was unable to calculate metrics for the following string
or character:

    Label

Common reasons for failure include:
  * The string contains a character which is special to LaTeX unless
    escaped properly, such as % or $.
  * The string makes use of LaTeX commands provided by a package and
    the tikzDevice was not told to load the package.

The contents of the LaTeX log of the aborted run have been printed above,
it may contain additional details as to why the metric calculation failed.

我无法弄清楚问题出在哪里,因为我在knitr日志中没有看到任何线索。显然,阻止编译的.RnwtikzDevice用这些行生成的图表:

\begin{figure*}
  \centering
<<child-time_series, child='figure/time_series.R',dev='tikz',fig.width=6,fig.height=3,out.width='.95\\textwidth',message=FALSE,echo=FALSE>>=
@

      \label{fig:TimeSeries}
\end{figure*}

我猜关键是以下LaTex错误(也在上面)

Error in getMetricsFromLatex(TeXMetrics) (from #91) : 
    TeX was unable to calculate metrics for the following string
    or character:

        Label

Label是我的情节的标签。

答案1

对于名为 的块fooknitr首先在图形设备为 时figure/foo.tikz使用包生成。然后通过(或根据您的设置使用其他 LaTeX 引擎)将此文件编译为 PDF 。如果此过程中出现任何问题,您可以tikzDevicetikzknitr.tikzpdflatex

  • 检查日志文件figure/foo.log中的错误消息
  • 打开figure/foo.tikz(它应该是一个完整的 LaTeX 文档),看看是否能发现任何错误
  • cd figure/; pdflatex foo.tikz在终端中尝试,看看pdflatex说什么(你最好\nonstopmode删除foo.tikz

从您发布的错误日志来看,中的 LaTeX 序言似乎有问题foo.tikz

相关内容