一块内的编织图,标签和标题 [英] knitr plots, labels, and captions within one chunk

查看:68
本文介绍了一块内的编织图,标签和标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一个乳胶报告,该报告会在dlply调用中产生多个图. dlply调用当然是在单个块中,为了获得更改的标签和标题,我正在使用

I am producing a latex report which produces multiple plots in a dlply call. The dlply call is of course in a single chunk and in order to get labels and captions to change I am using a snippet from Steve Powell below. The approach works but it seems knitr doesnt quite format the output correctly. A simple example that demonstrates:

\documentclass{article}

\begin{document}
<startup,echo=FALSE,results='hide',message=FALSE,tidy=FALSE,warning=FALSE,fig.keep='all',comment=NA>>=
require(knitr)
require(ggplot2)
opts_knit$set(progress = F, verbose = F)
opts_chunk$set(comment=NA,
           tidy=FALSE,
           warning=FALSE, 
           message=FALSE, 
           echo=FALSE, 
           dpi=600,
           fig.width=6.75, fig.height=4, # Default figure widths
           dev=c("pdf",'tiff'),
           dev.args=list(pdf=list(NULL),tiff=list(compression='lzw')),
           error=FALSE)
@
<<plotloop,results='asis'>>=
for(x in seq(1,20)){
  x1<-data.frame(x=seq(1,10),y=seq(1,10))
  plt<-ggplot(data=x1,aes(x,y))+geom_point()
  figLabel=paste('Figure',x,sep='')
  capt<-paste('Caption for fig.',x)
  cat(knit(text=(paste("<<",figLabel,",fig.pos='ht',fig.cap='",capt,"'>>=\nplt\n@",sep=''))))
}
@
\end{document}

这几乎可行.麻烦的是,knitr将\ caption大括号放在\ label大括号之外,这可以从以下.tex文件的代码段中看到:

This almost works. The trouble is that knitr places the closing \caption brace outside the \label brace which can be seen in the snippet from the .tex file below:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}
\color{fgcolor}
\begin{figure}[ht]
\includegraphics[width=\maxwidth]{figure/Figure1} \caption[Caption for fig]{Caption for fig. 1\label{fig:Figure1}}
\end{figure}
\end{knitrout}

如果只有几个这样的图形,但是具有大量图形,

latex可以处理此问题,它会开始错误地放置它们. 我也尝试过

latex can handle this if there are only a few figures like this but with larger numbers of plots, it starts to place them incorrectly. I have also tried this with a

fig.cap=paste('testLoop',seq(1,20))

接近并获得相同的结果.

approach and get the same result.

进一步说明:我在维基百科的Latex/Floats ...上找到了此内容... 页面:

Further clarification: I found this on wikipedia's Latex/Floats... page:

如果要标记图形以便以后引用,则必须在标题后添加标签(在LaTeX 2e中似乎起作用),但要在浮动环境中.如果在外部声明,则会给出段号.

If you want to label a figure so that you can reference it later, you have to add the label after the caption (inside seems to work in LaTeX 2e) but inside the floating environment. If it is declared outside, it will give the section number.

内部似乎可以在LaTeX 2e中工作"部分引起了我的注意.看来这仅是因为错误被多次忽略了才行吗?我在用 LaTeX2e< 2005/12/01>. 我认为代码的一部分在hooks-latex.R的hook_plot_tex函数行120中:

The 'inside seems to work in LaTeX 2e' part caught my attention. It seems it works only because the error is ignored a number of times? I am using LaTeX2e <2005/12/01>. I think the bit of code is in hook_plot_tex function line 120 of hooks-latex.R:

fig2 = sprintf('\\caption%s{%s\\label{%s}}\n\\end{%s}\n', scap, cap,
                 paste(lab, if (mcap) fig.cur, sep = ''), options$fig.env)  

这会解决吗?

fig2 = sprintf('\\caption%s{%s}\\label{%s}\n\\end{%s}\n', scap, cap,
                 paste(lab, if (mcap) fig.cur, sep = ''), options$fig.env)

建议?我不熟悉github流程... 谢谢!

Suggestions? I am not familiar with the github process... Thanks!

推荐答案

简短的答案似乎是由\ includegraphics命令过多且没有分页符引起的LaTeX问题.在循环中用字幕和标签完成多个图形的功能(感谢史蒂夫·鲍威尔(Steve Powell)和艺辉):

Short answer is it seems to be a LaTeX issue caused by too many \includegraphics commands and no pagebreaks. Function to accomplish multiple figures with captions and labels from within loop (with credit to Steve Powell and Yihui):

plot.knit<-function(chunkLabel,#text for chunk label which is also used for figure file name
                capt,#text for caption
                plt)#plot object to be placed
   {
    cat(knit(text=(paste("<<",chunkLabel,",fig.pos='h',fig.cap='",capt,"'>>=\nplt\n@",sep=''))))
}
cat('\\newpage')#some sort of page break must be inserted along the way to keep latex from breaking.

可以对其进行修改,以添加您想要的任何块选项.

This can be modified to add any of chunk options you would like.

长答案: 这是我为使其正常工作所做的.我从github下载了knitr,在上面进行了建议的更改,然后编译并运行了示例.更改后的代码不会更改结果.对乳胶错误的进一步调查使我进入了 LaTeX常见问题解答,其中指出:

Long Answer: Here is what I did to get it to work. I downloaded knitr from github, made the suggested alteration above, compiled, and ran example. The altered code did not change the outcome. Further investigation of latex error took me to the LaTeX FAQ where it states:

在很长的浮动环境序列中,也没有中间文本,也会发生该错误.除非环境适合此处"(并且您允许它们进入此处"),否则不会出现分页符,因此LaTeX将永远不会有机会重新考虑放置位置. (当然,如果序列足够长,浮标就不能全部放在这里":页面填满后,LaTeX将不再放置浮标,从而导致错误.

The error also occurs in a long sequence of float environments, with no intervening text. Unless the environments will fit "here" (and you’ve allowed them to go "here"), there will never be a page break, and so there will never be an opportunity for LaTeX to reconsider placement. (Of course, the floats can’t all fit "here" if the sequence is sufficiently prolonged: once the page fills, LaTeX won’t place any more floats, leading to the error.

解决方法可能涉及使用float包的[H] float限定符重新定义float,但是如果不使用\ clearpage,您就不太可能摆脱困境.

Techniques for resolution may involve redefining the floats using the float package’s [H] float qualifier, but you are unlikely to get away without using \clearpage from time to time.

所以,我添加了

cat('\\clearpage')

在循环的每个步骤中生成图后

.这样就不会抛出任何错误,并且图形也位于正确的位置.还有,

after the plots are generated in each step of the loop. This resulted in no errors being thrown and the figures in correct locations. Also,

cat('\\newpage')

在我的实际文档中的页面上放置数字2时似乎可以做得更好.

works and seems to do a better job at placing the figures 2 on a page in my actual document.

工作代码:

\documentclass{article}

    \begin{document}
<<startup,echo=FALSE,results='hide',message=FALSE,tidy=FALSE,warning=FALSE,fig.keep='all',comment=NA>>=
require(knitr)
require(ggplot2)
opts_knit$set(progress = F, verbose = F)
opts_chunk$set(comment=NA,
           tidy=FALSE,
           warning=FALSE, 
           message=FALSE, 
           echo=FALSE, 
           dpi=600,
           fig.width=6.75, fig.height=4, # Default figure widths
           dev=c("pdf",'tiff'),
           dev.args=list(pdf=list(NULL),tiff=list(compression='lzw')),
           error=FALSE)
@
<<plotloop,results='asis'>>=
for(x in seq(1,20)){
  x1<-data.frame(x=seq(1,10),y=seq(1,10))
  plt<-ggplot(data=x1,aes(x,y))+geom_point()
  figLabel=paste('Figure',x,sep='')
  capt<-paste('Caption for fig.',x)
  cat(knit(text=(paste("<<",figLabel,",fig.pos='h',fig.cap='",capt,"'>>=\nplt\n@",sep=''))))
cat('\\newpage')
}
@

\end{document}

这篇关于一块内的编织图,标签和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆