3

I am using knitr to write my thesis and compiling it as a latex document. 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."

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. In addition the caption under the figure must be centered, while the NOTE and SOURCE are left hand justified.

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. 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. 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. 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. 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.

\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):

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.

Thank You

camille
  • 16,432
  • 18
  • 38
  • 60
ssaltamon
  • 33
  • 1
  • 6
  • I haven't tried it yet, but maybe some newlines added to your fig.cap text will suffice. That is, if multiple paragraphs in a standard latex figure caption will suffice, then you need to figure out how to get newlines / new paragraphs in the knitr fig.cap line to make its way to the latex. – Kalin Mar 01 '15 at 20:27
  • Thank you, I did not search with those keywords in mind; so, I will do a new internet search based on those keywords and try it. – ssaltamon Mar 01 '15 at 20:42

1 Answers1

2

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. 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.

The reference about adding captions suggests adding \\\hspace{\textwidth} to start a new line within the caption (in the Latex file). So, we escape it in the Knitr file like this \\\\\\hspace{\\textwidth}.

Indeed, almost any Latex code you want to end up in the Latex file will have to be escaped this way. 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.

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:

enter image description here

Community
  • 1
  • 1
Kalin
  • 1,691
  • 2
  • 16
  • 22
  • Wow. I do need to refer to figures; however, I also need to have a "list of figures" page and when I use the latex command `\listoffigures,` with your example, the Caption, NOTE, SOURCE and EXTRA are all included; I would like only the Caption: information... to be included in the list of figures. I know beggars can't be choosers and I have employed a work around by writing the figure to a .pdf with Ghostscript to make the R font match the Latex, importing using `\graphicx` then using `\minipage` to construct the NOTE, SOURCE information separate from the caption. Thank You – ssaltamon Mar 03 '15 at 22:13
  • @ssaltamon Google, and [you shall find](http://stackoverflow.com/questions/27250635/short-caption-fig-scap-in-knitr-not-working)... (Note that you need Pandoc >= 1.13.1). – Kalin Mar 03 '15 at 23:48
  • I have edited my original question and provided code that synthesizes your answer and the link you provided, as well as the pdf work around "I" developed. I hope that I gave you due credit and proper reference. Thank you for your time. – ssaltamon Mar 04 '15 at 23:46