Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace the automatic numbering of captions inrmarkdown / knitr / pdf

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?

like image 912
Boris Leroy Avatar asked Oct 31 '25 03:10

Boris Leroy


1 Answers

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.

like image 82
Boris Leroy Avatar answered Nov 01 '25 18:11

Boris Leroy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!