如何阻止相同的图号出现在我的RMarkdown文档中

huangapple go评论69阅读模式
英文:

How to stop the same figure numbers appearing in my rmarkdown document

问题

I generated several figures using different kinds of commands such as tikz, rmarkdown chunk, ggplot2 like the following. Unfortunately, they all are generated with the same figure number as "Figure 1". Do you have any suggestion to resolve the issue?
Here in the following I generate a reproducible example. Please refer to the following codes for your reference.

---
title: "demo"
author:
output:
  pdf_document:
    number_sections: true
in_header: preamble.tex
keep_tex: yes
fontsize: 12pt
linestretch: 1.2
subparagraph: true
number_sections: true
geometry: margin=1in
header-includes:
- \usepackage{amsmath}
- \usepackage{amsthm}
- \usepackage{bbm}
- \numberwithin{equation}{section}
- \usepackage{makecell}
- \usepackage{indentfirst}
- \usepackage{geometry}
- \usepackage{graphicx}
- \usepackage{tikz}
- \AtEndPreamble{\usepackage{cleveref}}
---
```{r fig.align="center", fig.cap="\\label{fig:figure4}Trends", warning=FALSE, echo=FALSE, message=FALSE}
library(ggplot2)
    
options(scipen=999)  # turn-off scientific notation like 1e+48
library(ggplot2)
theme_set(theme_bw())  # pre-set the bw theme.
data("midwest", package = "ggplot2")
gg <- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state, size=popdensity)) + 
  geom_smooth(method="loess", se=FALSE) + 
  xlim(c(0, 0.1)) + 
  ylim(c(0, 500000)) + 
  labs(subtitle="Area Vs Population", 
       y="Population", 
       x="Area", 
       title="Scatterplot", 
       caption = "Source: midwest")

plot(gg)

```r
```{r fig.align="center", fig.cap="\\label{fig:figure1} figure 1 "}
library(datasets)
library(tseries)
data("AirPassengers")
passengers <- AirPassengers
plot(passengers, main = "Monthly Air Passenger Traffic")

```latex
\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ; 
\end{tikzpicture}
\caption{\footnotesize Flow Diagram 1}
\label{fig:figure2}
\end{figure}

\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}} 
\begin{tikzpicture[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
\end{tikzpicture}
\caption{\footnotesize Flow Diagram2}
\label{fig:figure3}
\end{figure}

Look at \cref{fig:figure1}, \cref{fig:figure2}, \cref{fig:figure3}, and \cref{fig:figure4}.


<details>
<summary>英文:</summary>

I generated several figures using different kinds of commands such as tikz, rmarkdown chunck, ggplot2 like the following. Unfortunately, they all are generated with the same figure number as &quot;Figure 1&quot;. Do you have any suggestion to resolve the issue?
Here in the following I generate a reproducible example. Please refer to the following codes for your reference. 
~~~
---
title: &quot;demo&quot; 
author: 
output: 
  pdf_document:
    number_sections: true
in_header: preamble.tex
keep_tex: yes
fontsize: 12pt
linestretch: 1.2
subparagraph: true
number_sections: true
geometry: margin=1in
header-includes:
- \usepackage{amsmath}
- \usepackage{amsthm}
- \usepackage{bbm}
- \numberwithin{equation}{section}
- \usepackage{makecell}
- \usepackage{indentfirst}
- \usepackage{geometry}
- \usepackage{graphicx}
- \usepackage{tikz}
- \AtEndPreamble{\usepackage{cleveref}}
---
```{r fig.align=&quot;center&quot;, fig.cap=&quot;\\label{fig:figure4}Trends&quot;, warning=FALSE, echo=FALSE, message=FALSE}
library(ggplot2)
    
options(scipen=999)  # turn-off scientific notation like 1e+48
library(ggplot2)
theme_set(theme_bw())  # pre-set the bw theme.
data(&quot;midwest&quot;, package = &quot;ggplot2&quot;)
gg &lt;- ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state, size=popdensity)) + 
  geom_smooth(method=&quot;loess&quot;, se=F) + 
  xlim(c(0, 0.1)) + 
  ylim(c(0, 500000)) + 
  labs(subtitle=&quot;Area Vs Population&quot;, 
       y=&quot;Population&quot;, 
       x=&quot;Area&quot;, 
       title=&quot;Scatterplot&quot;, 
       caption = &quot;Source: midwest&quot;)

plot(gg)
library(datasets)
library(tseries)
data(&quot;AirPassengers&quot;)
passengers &lt;- AirPassengers
plot(passengers, main = &quot;Monthly Air Passenger Traffic&quot;)

\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
\end{tikzpicture}
\caption{\footnotesize Flow Diagram 1}
\label{fig:figure2}
\end{figure}

\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}}
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
\end{tikzpicture}
\caption{\footnotesize Flow Diagram2}
\label{fig:figure3}
\end{figure}


Look at \cref{fig:figure1}, \cref{fig:figure2}, \cref{fig:figure3} and \cref{fig:figure4}.

~~~

</details>


# 答案1
**得分**: 1

- 你缺少了`cleveref`包。
- `fig:figuare1`中有一个拼写错误。
- 根据https://stackoverflow.com/questions/33517026/error-trying-to-install-package-for-iris-dataset,没有`iris`库。

[![enter image description here][1]][1]

[1]: https://i.stack.imgur.com/6CO7X.png

<details>
<summary>英文:</summary>

- you are missing the `cleveref` package

- there is a typo in `fig:figuare1`

- and according to https://stackoverflow.com/questions/33517026/error-trying-to-install-package-for-iris-dataset , there is no `iris` library

----

    ---
    title: &quot;demo&quot; 
    author: 
    output: 
      pdf_document:
        number_sections: true
    link-citations: yes
    toc: false
    biblio-style: apalike    
    in_header: preamble.tex
    keep_tex: yes
    fontsize: 12pt
    linestretch: 1.2
    subparagraph: true
    number_sections: true
    geometry: margin=1in
    header-includes:
    - \usepackage{amsmath}
    - \usepackage{amsthm}
    - \usepackage{bbm}
    - \numberwithin{equation}{section}
    - \usepackage{makecell}
    - \usepackage{indentfirst}
    - \usepackage{geometry}
    - \usepackage{graphicx}
    - \usepackage{tikz}
    - \AtEndPreamble{\usepackage{cleveref}}
    ---
    
    ```{r fig.align=&quot;center&quot;, fig.cap=&quot;\\label{fig:figure1} figure 1 &quot;}
    library(datasets)
    library(tseries)
    data(&quot;AirPassengers&quot;)
    passengers &lt;- AirPassengers
    plot(passengers, main = &quot;Monthly Air Passenger Traffic&quot;)
    ```
    
    \begin{figure}
    \tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        
    \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
    \draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
    \draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ; 
    \end{tikzpicture}
    \caption{\footnotesize Flow Diagram 1}
    \label{fig:figure2}
    \end{figure}
    
    \begin{figure}
    \tikzset{every picture/.style={line width=0.75pt}} 
    \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
    \draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
    \draw  [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
    \end{tikzpicture}
    \caption{\footnotesize Flow Diagram2}
    \label{fig:figure3}
    \end{figure}
    
    ```{r fig.align=&quot;center&quot;, fig.cap=&quot;\\label{fig:figure4}Trends&quot;, warning=FALSE, echo=FALSE, message=FALSE}
    library(ggplot2)
    
    qplot(Sepal.Length, data = iris, size = Petal.Width)
    ```
    
    Look at \cref{fig:figure1}, \cref{fig:figure2}, \cref{fig:figure3} and \cref{fig:figure4}.

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/6CO7X.png

</details>



huangapple
  • 本文由 发表于 2023年3月7日 20:13:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定