除了标题之外,如何为乳胶输出的 knitr 图添加注释或文本?

How do I add a note or text in addition to the caption to a knitr figure for latex output?

我正在使用 knitr 撰写论文并将其编译为乳胶文档。我已经成功地包含了带有适当标签标题的图形;但是,除了标题之外,我还想在标题正下方添加额外的文本行,并与图相关联。例如,在标题下方,我想添加:“注意:这些数据以 mu=0 和 sd=1 分发”和“来源:这些数据是在 R 中随机生成的。”

编辑 1 问题:我还需要一个图表页面列表,其中只包含标题信息,不包含任何注释或来源信息。另外图下的标题必须居中,而NOTE和SOURCE左对齐。

我相信许多出版物的图片都带有关于图片的注释或与标题标题分开的数据来源信息;所以,我相信这个问题不仅仅适用于我。

编辑 1 进行编码。这是 @user29020 提供的代码和他们建议的 link 的综合,以及我从其他帖子中收集的 .pdf 解决方法。 @user29020 提供的“纯”knitr 方法的优点是情节优美,文本缩放比例与文本的 body 一致。缺点是缺乏对标题信息的控制:我想将“Caption”元素居中并左对齐和 sub-caption 行,但我不知道如何实现;所以,如果有人 body 知道如何做到这一点,那就太好了。 pdf 解决方法的优点是我可以更好地控制字幕。除了没有充分利用 knitr 创建和跟踪图形的能力外,缺点是当字体匹配时,通过使用“Ghostscript”,缩放比例看起来有点偏差,需要以 ad-hoc 的方式进行调整R代码。

\documentclass{article}
\usepackage{caption}
\usepackage{tikz}%Draw Graphs
\begin{document}

\listoffigures
\newpage
%THIS IS A SYNTHESIS OF user29020 CODE AND THE LINK THEY PROVIDED IN THEIR COMMENTS
<<cap-setup1,include=FALSE>>=
scap="This is a Caption."
NOTE="NOTE: Here is a note with one linebreak from above."
SOURCE="SOURCE: Here is my source with an extra linebreak to separate it."
EXTRA="{\tiny EXTRA: Here is smaller text. You could explore more text sizing in \LaTeX.} And bigger again."
hspace="\\\hspace{\textwidth}"
cap<- paste(
     scap, 
    hspace, 
    NOTE, 
    hspace, 
     SOURCE, 
    hspace, 
    EXTRA)

@


In Figure \ref {fig:fig1}, we see a scatter plot.    

<<fig1, echo=FALSE,fig.cap=cap, fig.scap=scap,fig.pos="!h", >>=
r<-rnorm(100)
plot(r)
@  


%THIS IS THE PDF WORK AROUND

%Loading R Packages
<<loading,include=FALSE>>=
require(extrafont)
require(graphicx)
font_install("fontcm") #this gets the cm roman family from package extrafont for pdf output in r to match latex
@

<<fig-pdf,echo=FALSE, include=FALSE,fig.cap=paste("This is a Caption"),fig.pos="!h", >>=
Sys.setenv(R_GSCMD="C:/Program Files/gs/gs9.15/bin/gswin64c.exe") #this needs to go inside every r code chunk to call GhostScript for embedding latex font "computer modern roman" (so R font in pdfs matches latex document font)
pdf("tikz-plot-pdf.pdf", family="CM Roman") 
set.seed(2015)
r<-rnorm(100)
 plot(r)
dev.off()
embed_fonts("tikz-plot-pdf.pdf") #this embeds the pdf font so that when graphicx calls the pdf the fonts from R to latex match
@  
\newpage
In Figure \ref{fig:scatter}, we see a scatter plot.
\begin{figure}[!h]
\centering
\includegraphics[width=\textwidth]{tikz-plot-pdf}
\caption{This is also a Caption}\label{fig:scatter}
\begin{minipage}{\textwidth}
NOTE: Here is a note with one linebreak from above.\
SOURCE: Here is my source with an extra linebreak to separate it.\
EXTRA:{\tiny EXTRA: Here is smaller text. You could explore more text sizing in \LaTeX.} And bigger again.
\end{minipage}
\end{figure}


\end{document}

我想在标题行下方添加“NOTE”和“SOURCE”行,编辑 1:只有 \listoffigures 中包含的标题和居中的标题,而留下 NOTE 和 SOURCE合理的(如 pdf 解决方法的输出):

我以前没有在这个论坛上问过问题,虽然我找到了很多答案,所以任何 meta-feedback 关于我的问题编辑 1: 或我如何编辑的也很感激。

谢谢

此答案假定多行字幕足以满足您的目的。我使用了您将 R 变量传递给 Knitr fig.cap= 参数的方法。这让我们可以在使用前提前在其他代码块中编写标题。

技巧是 A) 我们需要找出 how to add line break to caption without using caption package,以及 B) 知道如何将文字“\”(反斜杠)字符传递给生成的 Latex 文档。

关于添加字幕的参考建议添加 \\hspace{\textwidth} 以在字幕(在 Latex 文件中)中开始一个新行。因此,我们在 Knitr 文件中将其转义为 \\\hspace{\textwidth}

事实上,几乎所有您想在 Latex 文件中结束的 Latex 代码都必须通过这种方式进行转义。毫无疑问,在撰写论文时,您会想要参考您的图表。您可以通过将数字 \label 放入标题中然后在其他地方使用 \ref{} 来使用 Latex 参考。

请参阅下面的文件修改版本以获取多行标题和图形参考:

\documentclass[12pt]{article}%12 Pt font, article document class
\usepackage{caption}
\usepackage{tikz}%Draw Graphs

\begin{document}

<<>>=
x <- paste(
    "This is a Caption.", 
    "\\\hspace{\textwidth}", 
    "NOTE: Here is a note with one linebreak from above.", 
    "\\\hspace{\textwidth}", 
    "\\\hspace{\textwidth}", 
    "SOURCE: Here is my source with an extra linebreak to separate it.", 
    "\\\hspace{\textwidth}", 
    "{\tiny EXTRA: Here is smaller text. You could explore more text sizing in \LaTeX.} And bigger again.",
    "\label{figure:demo-of-multiline-captions}",  
    "\n" )
cat(x)
@

We can also show the variable "x" in the "asis" formate here:

<<results="asis">>=
cat(x)
@

This text right here (now) is a new paragraph, so it is indented. 

Below, in \textbf{Figure~\ref{figure:demo-of-multiline-captions}}, we use the "x" variable in the "fig.cap=" argument to Knitr.    

<<fig1, echo=FALSE,fig.cap=x,fig.pos="!h", >>=
r<-rnorm(100)
plot(r)
@  

\end{document}

结果如下: