4

I am generating a PDF document with Rmarkdown, within which I want to manually define figure numbers.

Below is an example of a chunk:

```{r chunk26, fig.cap = "Fig. 5.3 My figure caption"}
plot(1, 1)
```

I use a special numbering to follow the chapters of my document.

The problem is that when I knit the PDF, "Figure X:" is automatically added before each caption. As a result, my figure captions look like this example:

Example of problem with the figure captions

Note I have used the following parameters in the beginning of my rmarkdown file:

output:
  pdf_document:
    fig_caption: yes

My question therefore is:

is it possible to remove the automatic generation of "Figure X" before the figure caption, when generating a PDF using rmarkdown/knitr?

Boris Leroy
  • 460
  • 4
  • 14
  • 1
    How is Figure 26 generated? It's not a default, ie, if I copy and paste your code into a new rmarkdown, I don't get any automated numbering. I assume you have some extra knitr options set somewhere which are doing this. – rawr Sep 25 '14 at 14:04
  • Yes this is correct. I have added `fig_caption: yes`; otherwise I can't add captions to my figures. – Boris Leroy Sep 26 '14 at 14:37

3 Answers3

4

On the basis of the link published by kohske, I have finally managed to find a workaround, i.e. to define the figure number according to a template

Figure #section.#figure
For example: Figure 3.1

This is not what I initially wanted to do (i.e. remove the automatic numbering of figures), but it is a nice workaround.

How to do it

First of all, create a 'mystyle.sty' file located in the same directory as your rmarkdown file. Within this mystyle.sty file, put the following line of code:

\usepackage{chngcntr}

Then, in the header of your rmarkdown file, add the following information:

output:
  pdf_document:
    fig_caption: yes
    includes:
        in_header: mystyle.sty

The purpose of this was to make sure that rmarkdown asks latex to use a package allowing you to create an appropriate automatic numbering.

The next step is to add this at the beginning of the document:

\counterwithin{figure}{section}

so the figures will be numbered in each section.

And then, you can manually define the value of "section" and "figure" with \setcounter{section}{#}

Actually, what you have to do is simply to put the two following lines at the beginning of each section:

\setcounter{section}{1}
\setcounter{figure}{0}

If you are in section 3, change \setcounter{section}{1} to \setcounter{section}{3}.

And this works properly; for example the figure 3 of my section 5 is:

Proper caption

However, there is still another issue left: although this solves the knit PDF problem, this will not work for HTML. If you use the same document to generate PDF and HTML files, then your PDF will have good numbers, and your HTML won't have any number. I still haven't figured out how to do the same thing in HTML.

Boris Leroy
  • 460
  • 4
  • 14
2

This is tricky but you can do this by:

\setcounter{figure}{25}
```{r, fig.cap="hoge"}
plot(1)
```

If you want to get continuous numbering then here is some information: https://tex.stackexchange.com/questions/28333/continuous-v-per-chapter-section-numbering-of-figures-tables-and-other-docume

Community
  • 1
  • 1
kohske
  • 65,572
  • 8
  • 165
  • 155
0

You can use:

header-includes: 
\renewcommand{\caption}{Figure}

in the YAML header. Then, in the code chunk, use:

```{r, fig.cap=" 5.3: Response functions"}
...
```
RobertMyles
  • 2,673
  • 3
  • 30
  • 45