简体   繁体   English

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

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

I am using knitr to write my thesis and compiling it as a latex document.我正在使用 knitr 编写我的论文并将其编译为乳胶文档。 I have successfully included figures with the appropriately labeled caption;我已经成功地包含了带有适当标记标题的数字; however, in addition to the caption I would like to include additional lines of text directly below the caption and associated with the figure.但是,除了标题之外,我还想在标题下方直接添加额外的文本行并与图相关联。 For example, below the caption I would like to hove: "NOTE: These data are distributed with mu=0 and sd=1" and "SOURCE: These data were randomly generated in R."例如,在标题下方,我想说:“注意:这些数据以 mu=0 和 sd=1 分布”和“来源:这些数据是在 R 中随机生成的”。

EDIT 1 to question: I also need to have a list of figures page with just the caption information included not any of the NOTE or SOURCE information.编辑 1 提出问题:我还需要一个图表页面列表,其中仅包含标题信息,不包含任何注释或来源信息。 In addition the caption under the figure must be centered, while the NOTE and SOURCE are left hand justified.此外,图下方的标题必须居中,而 NOTE 和 SOURCE 左对齐。

I believe that many publications have figures with notes about the figure or information about the source of the data that is separate from the caption title;我相信许多出版物都有带有图表注释或与标题标题分开的数据来源信息; so, I believe this question is not just applicable to me.所以,我相信这个问题不仅仅适用于我。

EDIT 1 to code.编辑 1 进行编码。 Here is a synthesis of the code that @user29020 provided and the link they suggested, as well as the .pdf work around I have assembled from other posts.这是@user29020 提供的代码和他们建议的链接的综合,以及我从其他帖子中收集的 .pdf 工作。 The advantage of the "pure" knitr method provided by @user29020 is that the plot is beautiful with the text scaled consistently with the body of the text. @user29020 提供的“纯”knitr 方法的优点是,情节很漂亮,文字与正文的比例一致。 The disadvantage is a lack of control of the caption information: I would like to center the "Caption" element and left justify the and sub-caption lines and I do not know how to accomplish this;缺点是缺乏对字幕信息的控制:我想将“字幕”元素居中并左对齐和子字幕行,但我不知道如何实现; so, if anybody knows how to do this that would be great.所以,如果有人知道如何做到这一点,那就太好了。 The advantage of the pdf work around is that I have greater control over the captions. pdf 解决方案的优点是我可以更好地控制字幕。 In addition to not taking full advantage of knitr's ability to create and track figures, the disadvantage is that while the font matches, by using "Ghostscript" the scaling appears a little off and needs to be adjusted in an ad-hoc fashion in the R code.除了没有充分利用 knitr 创建和跟踪图形的能力之外,缺点是虽然字体匹配,但通过使用“Ghostscript”,缩放比例看起来有点偏,需要在 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}

I would like to have the "NOTE" and "SOURCE" lines below the caption line, EDIT 1: only the caption included in the \\listoffigures and the caption centered, while the NOTE and SOURCE are left justified (like the output from the pdf workaround):我希望在标题行下方有“NOTE”和“SOURCE”行,编辑 1:只有\\listoffigures包含的标题和居中的标题,而 NOTE 和 SOURCE 是左对齐的(如 pdf 的输出解决方法):

I have not asked a question on this forum before, although I have found many answers, so any meta-feedback on my question EDIT 1: or how I am editing is also appreciated.我以前没有在这个论坛上问过一个问题,虽然我找到了很多答案,所以对我的问题 EDIT 1: 或我如何编辑的任何元反馈也表示赞赏。

Thank You谢谢你

This answer assumes that multiline captions are sufficient for your purposes.此答案假定多行字幕足以满足您的目的。 I used your method of passing an R variable to the Knitr fig.cap= argument.我使用了您将 R 变量传递给fig.cap=参数的方法。 This lets us compose the caption ahead of time in other code chunks before use.这让我们可以在使用之前提前在其他代码块中组合标题。

The tricks were that A) we need to find out how to add line break to caption without using caption package , and B) know how to pass literal " \\ " (backslash) characters to the resulting Latex document.技巧是 A) 我们需要找出如何在不使用标题包的情况下向标题添加换行符,以及 B) 知道如何将文字“ \\ ”(反斜杠)字符传递给生成的 Latex 文档。

The reference about adding captions suggests adding \\\\\\hspace{\\textwidth} to start a new line within the caption (in the Latex file).有关添加标题的参考建议添加\\\\\\hspace{\\textwidth}以在标题内(在 Latex 文件中)开始新行。 So, we escape it in the Knitr file like this \\\\\\\\\\\\hspace{\\\\textwidth} .因此,我们在\\\\\\\\\\\\hspace{\\\\textwidth}文件中将其转义为\\\\\\\\\\\\hspace{\\\\textwidth}

Indeed, almost any Latex code you want to end up in the Latex file will have to be escaped this way.事实上,几乎所有你想要在 Latex 文件中结束的 Latex 代码都必须以这种方式进行转义。 When writing your thesis, you'll want to refer to your Figures, no doubt.毫无疑问,在撰写论文时,您会想要参考您的图表。 You can use the Latex references by putting the figure \\label into the caption and then using a \\ref{} elsewhere.您可以通过将图形\\label放入标题中,然后在其他地方使用\\ref{}来使用 Latex 引用。

See the modified version of your file below for multiline captions and figure references:有关多行标题和图形参考,请参阅以下文件的修改版本:

\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}

Results give this:结果给出了这个:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM