繁体   English   中英

除了标题之外,如何为乳胶输出的 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 提供的代码和他们建议的链接的综合,以及我从其他帖子中收集的 .pdf 工作。 @user29020 提供的“纯”knitr 方法的优点是,情节很漂亮,文字与正文的比例一致。 缺点是缺乏对字幕信息的控制:我想将“字幕”元素居中并左对齐和子字幕行,但我不知道如何实现; 所以,如果有人知道如何做到这一点,那就太好了。 pdf 解决方案的优点是我可以更好地控制字幕。 除了没有充分利用 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}

我希望在标题行下方有“NOTE”和“SOURCE”行,编辑 1:只有\\listoffigures包含的标题和居中的标题,而 NOTE 和 SOURCE 是左对齐的(如 pdf 的输出解决方法):

我以前没有在这个论坛上问过一个问题,虽然我找到了很多答案,所以对我的问题 EDIT 1: 或我如何编辑的任何元反馈也表示赞赏。

谢谢你

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

技巧是 A) 我们需要找出如何在不使用标题包的情况下向标题添加换行符,以及 B) 知道如何将文字“ \\ ”(反斜杠)字符传递给生成的 Latex 文档。

有关添加标题的参考建议添加\\\\\\hspace{\\textwidth}以在标题内(在 Latex 文件中)开始新行。 因此,我们在\\\\\\\\\\\\hspace{\\\\textwidth}文件中将其转义为\\\\\\\\\\\\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}

结果给出了这个:

在此处输入图片说明

暂无
暂无

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

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