简体   繁体   中英

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:

图形标题问题示例

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?

On the basis of the link published by kohske, I have finally managed to find a workaround, ie 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 (ie 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:

适当的字幕

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.

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

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"}
...
```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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