0

I'm writing a paper for the journal PLOSone. They require that figure captions be placed in the article but not the figure itself (it's submitted separately). So I'm wondering how to get knitr to spit out the figure caption w/o the figure. Of course, this is not quite possible, since the figure number and hence caption isn't finalized until LaTeX has been run. So this question may need to be migrated eventually, but maybe someone has an idea about the necessary steps. Here is a MWE:

\documentclass[10pt, letterpaper]{article}
% Bold the 'Figure #' in the caption and separate it from the title/caption with a period
% Captions will be left justified
\usepackage[aboveskip = 1pt, labelfont = bf, labelsep = period, justification = raggedright, singlelinecheck = off]{caption}

\begin{document}

This is my document.  Here is a cool plot: Fig.~\ref{LabelA}.
<<  ChunkA, fig.cap = "\\label{LabelA}\\bf{A Figure.}" , echo = FALSE >>=
plot(1:10, 1:10)
currCap <- knitr::opts_current$get("fig.cap")
@

<<  ChunkB, results = "asis" , echo = FALSE >>=
cat("\n")
cat(currCap)
@

\end{document}

This isn't quite the right approach, for two reasons. First, LaTeX can process this, but the output from cat(currCap) creates a duplicate label which confuses it. Second, the line of text that I want in the final pdf is:

Figure 1. A Figure.

but the information that this is Figure 1 and not some other figure is not known prior to compiling in LaTeX. Here is a little piece of the tex file which shows the output from the above code where you can see how the duplicate label arises.

This is my document.  Here is a cool plot: Fig.~\ref{LabelA}.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{figure}
\includegraphics[width=\maxwidth]{figure/ChunkA-1} \caption{\label{LabelA}\bf{A Figure.}}\label{fig:ChunkA}
\end{figure}
\end{knitrout}
\label{LabelA}\bf{A Figure.}

Anyone know a way to make this work?

2
  • Though I haven't tested it, can you post-process the generated LaTeX file (e.g., sed) to replace all \includegraphics with a 1-pixel graphic or even remove the graphic altogether and keep the \caption? Here and here are alternatives.
    – r2evans
    Jul 17, 2015 at 15:22
  • @r2evans Ah, good idea. As it turned out, I solved the problem in the particular context I had as described in my answer below. It's a challenge sometimes to integrate all the nice tools that are out there now, at least until a more seamless approach comes along. Jul 17, 2015 at 15:27

1 Answer 1

1

Alright, here is a solution to my own question in case anyone else wants/needs to do this.

\documentclass[10pt, letterpaper]{article}
\usepackage[nomarkers, nolists]{endfloat}
\usepackage[aboveskip=1pt,labelfont=bf,labelsep=period,justification=raggedright,singlelinecheck=off]{caption}
\begin{document}

<< setUp, echo = FALSE >>=
# Function to report figure captions independent of figures, as required by PLOSone.
# Original idea from Yihui Xie but modified considerably.
# https://github.com/yihui/knitr-examples/blob/master/070-caption-num.Rmd
# Function captures figure caption
x <- NULL
ii <- 0 # Current figure number
fn <- function(x) {
    ii <<- ii + 1 # save updated figure counter into global environment
    figString <<- paste('\\textbf{Figure ', ii, '.} ', x, sep = '') # Current caption saved in global environment
    return(x) # Return original fig.cap so it can be processed normally in the LaTeX figure environment
  }
@

This is my document.  Here is a cool plot: Fig.~\ref{LabelA}.
<<  ChunkA, fig.cap =  fn("\\label{LabelA}\\textbf{A Figure.}"), echo = FALSE >>=
plot(1:10, 1:10)
@

<< figStringA, echo = FALSE , results = "asis" >>=
cat(figString)
@

<<  ChunkB, fig.cap = fn("\\label{LabelB}\\textbf{Another Figure.} Some details about the figure.") , echo = FALSE >>=
plot(1:10, 1:10)
@

<< figStringB, echo = FALSE , results = "asis" >>=
cat(figString)
@

The second figure is Fig.~\ref{LabelB}.  That's all folks!
\end{document}

If you have includegraphics environments you will have to include a little code to write out the figure caption and increment ii so that the figure numbering remains correct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .