Cross-reference problem relevant to figures only

From the link you posted:

To use cross-references, you will need:

  • A bookdown output format : Cross-referencing is not provided directly within the base rmarkdown package, but is provided as an extension in bookdown (Xie 2020c). We must therefore use an output format from bookdown (e.g., html_document2 , pdf_document2 , and word_document2 , etc.) in the YAML output field.
  • A caption to your figure (or table) : Figures without a caption will be included directly as images and will therefore not be a numbered figure.
  • A labeled code chunk : This provides the identifier for referencing the figure generated by the chunk.

After these conditions are met, we can make cross-references within the text using the syntax \@ref(type:label) , where label is the chunk label and type is the environment being referenced (e.g. tab , fig , or eqn ). An example is provided below:

If I had to guess what the issue is it's that you have,

output: pdf_document

In your YAML header rather than something like,

output:  bookdown::pdf_document2

Try using this as the body of a minimally working example Rmd file:

---
output:  bookdown::pdf_document2
---

```{r a, fig.cap="The cars data.", echo=FALSE}
plot(cars)
```

See Figure \@ref(fig:a).

If this does not fix your issue a good next step in debugging would be to change the YAML header to,

---
output:
  bookdown::pdf_document2:
    keep_tex: true
---

So you can examing the \LaTeX code being generated by the above.

Thanks :smiley:.
I did use

output:  bookdown::pdf_document2

originally and I have tried

---
output:
  bookdown::pdf_document2:
    keep_tex: true
---

yet neither of the two methods work :thinking:.
It is weird because the cross-referencing just works well for tables.
Does this phenomenon have something to do with the LaTeX engine I use?
My complete YAML header is:


title: "Try Cross-referencing"
author: "Silent Hill"
date: "2020/8/20"
output:
bookdown::pdf_document2:
latex_engine: xelatex

If you remove the latex_engine directive and use the minimal example, does it still fail to work?

Thanks. I remove the latex_engine directive but it still fails :thinking:.

Post the contents of the .tex file.

Here is the content:

This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/W32TeX) (preloaded format=pdflatex 2020.9.2)  7 SEP 2020 14:57
entering extended mode
 restricted \write18 enabled.
 %&-line parsing enabled.
**XYH.tex
(./XYH.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-08-07> (c:/Users/lenovo/AppData/Roaming/TinyTeX/texm
f-dist/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2019/12/20 v1.4l Standard LaTeX file (size option)
)
\c@part=\count168
\c@section=\count169
\c@subsection=\count170
\c@subsubsection=\count171
\c@paragraph=\count172
\c@subparagraph=\count173
\c@figure=\count174
\c@table=\count175
\abovecaptionskip=\skip47
\belowcaptionskip=\skip48
\bibindent=\dimen134
) (c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/lm/lmodern.sty
Package: lmodern 2009/10/30 v1.6 Latin Modern Fonts
LaTeX Font Info:    Overwriting symbol font `operators' in version `normal'
(Font)                  OT1/cmr/m/n --> OT1/lmr/m/n on input line 22.
LaTeX Font Info:    Overwriting symbol font `letters' in version `normal'
(Font)                  OML/cmm/m/it --> OML/lmm/m/it on input line 23.
LaTeX Font Info:    Overwriting symbol font `symbols' in version `normal'
(Font)                  OMS/cmsy/m/n --> OMS/lmsy/m/n on input line 24.
LaTeX Font Info:    Overwriting symbol font `largesymbols' in version `normal'
(Font)                  OMX/cmex/m/n --> OMX/lmex/m/n on input line 25.
LaTeX Font Info:    Overwriting symbol font `operators' in version `bold'
(Font)                  OT1/cmr/bx/n --> OT1/lmr/bx/n on input line 26.
LaTeX Font Info:    Overwriting symbol font `letters' in version `bold'
(Font)                  OML/cmm/b/it --> OML/lmm/b/it on input line 27.
LaTeX Font Info:    Overwriting symbol font `symbols' in version `bold'
(Font)                  OMS/cmsy/b/n --> OMS/lmsy/b/n on input line 28.
LaTeX Font Info:    Overwriting symbol font `largesymbols' in version `bold'
(Font)                  OMX/cmex/m/n --> OMX/lmex/m/n on input line 29.
LaTeX Font Info:    Overwriting math alphabet `\mathbf' in version `normal'
(Font)                  OT1/cmr/bx/n --> OT1/lmr/bx/n on input line 31.
LaTeX Font Info:    Overwriting math alphabet `\mathsf' in version `normal'
(Font)                  OT1/cmss/m/n --> OT1/lmss/m/n on input line 32.
LaTeX Font Info:    Overwriting math alphabet `\mathit' in version `normal'
(Font)                  OT1/cmr/m/it --> OT1/lmr/m/it on input line 33.
LaTeX Font Info:    Overwriting math alphabet `\mathtt' in version `normal'
(Font)                  OT1/cmtt/m/n --> OT1/lmtt/m/n on input line 34.
LaTeX Font Info:    Overwriting math alphabet `\mathbf' in version `bold'
(Font)                  OT1/cmr/bx/n --> OT1/lmr/bx/n on input line 35.
LaTeX Font Info:    Overwriting math alphabet `\mathsf' in version `bold'
(Font)                  OT1/cmss/bx/n --> OT1/lmss/bx/n on input line 36.
LaTeX Font Info:    Overwriting math alphabet `\mathit' in version `bold'
(Font)                  OT1/cmr/bx/it --> OT1/lmr/bx/it on input line 37.
LaTeX Font Info:    Overwriting math alphabet `\mathtt' in version `bold'
(Font)                  OT1/cmtt/m/n --> OT1/lmtt/m/n on input line 38.
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsfonts/amssymb.
sty
Package: amssymb 2013/01/14 v3.01 AMS font symbols

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsfonts/amsfonts
.sty
Package: amsfonts 2013/01/14 v3.01 Basic AMSFonts support
\@emptytoks=\toks15
\symAMSa=\mathgroup4
\symAMSb=\mathgroup5
LaTeX Font Info:    Redeclaring math symbol \hbar on input line 98.
LaTeX Font Info:    Overwriting math alphabet `\mathfrak' in version `bold'
(Font)                  U/euf/m/n --> U/euf/b/n on input line 106.
))
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amsmath.s
ty
Package: amsmath 2020/01/20 v2.17e AMS math features
\@mathmargin=\skip49
For additional information on amsmath, use the `?' option.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amstext.s
ty
Package: amstext 2000/06/29 v2.01 AMS text

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amsgen.st
y
File: amsgen.sty 1999/11/30 v2.0 generic functions
\@emptytoks=\toks16
\ex@=\dimen135
))
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amsbsy.st
y
Package: amsbsy 1999/11/29 v1.2d Bold Symbols
\pmbraise@=\dimen136
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amsopn.st
y
Package: amsopn 2016/03/08 v2.02 operator names
)
\inf@bad=\count176
LaTeX Info: Redefining \frac on input line 227.
\uproot@=\count177
\leftroot@=\count178
LaTeX Info: Redefining \overline on input line 389.
\classnum@=\count179
\DOTSCASE@=\count180
LaTeX Info: Redefining \ldots on input line 486.
LaTeX Info: Redefining \dots on input line 489.
LaTeX Info: Redefining \cdots on input line 610.
\Mathstrutbox@=\box45
\strutbox@=\box46
\big@size=\dimen137
LaTeX Font Info:    Redeclaring font encoding OML on input line 733.
LaTeX Font Info:    Redeclaring font encoding OMS on input line 734.
\macc@depth=\count181
\c@MaxMatrixCols=\count182
\dotsspace@=\muskip16
\c@parentequation=\count183
\dspbrk@lvl=\count184
\tag@help=\toks17
\row@=\count185
\column@=\count186
\maxfields@=\count187
\andhelp@=\toks18
\eqnshift@=\dimen138
\alignsep@=\dimen139
\tagshift@=\dimen140
\tagwidth@=\dimen141
\totwidth@=\dimen142
\lineht@=\dimen143
\@envbody=\toks19
\multlinegap=\skip50
\multlinetaggap=\skip51
\mathdisplay@stack=\toks20
LaTeX Info: Redefining \[ on input line 2859.
LaTeX Info: Redefining \] on input line 2860.
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/iftex/ifxetex.s
ty
Package: ifxetex 2019/10/25 v0.7 ifxetex legacy package. Use iftex instead.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/iftex/iftex.sty
Package: iftex 2020/03/06 v1.0d TeX engine tests
))
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/iftex/ifluatex.
sty
Package: ifluatex 2019/10/25 v1.5 ifluatex legacy package. Use iftex instead.
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/base/fixltx2e.sty
Package: fixltx2e 2016/12/29 v2.1a fixes to LaTeX (obsolete)
Applying: [2015/01/01] Old fixltx2e package on input line 46.

Package fixltx2e Warning: fixltx2e is not required with releases after 2015
(fixltx2e)                All fixes are now in the LaTeX kernel.
(fixltx2e)                See the latexrelease package for details.

Already applied: [0000/00/00] Old fixltx2e package on input line 53.
) (c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/base/fontenc.st
y
Package: fontenc 2020/02/11 v2.0o Standard LaTeX package
LaTeX Font Info:    Trying to load font information for T1+lmr on input line 11
2.
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/lm/t1lmr.fd
File: t1lmr.fd 2009/10/30 v1.6 Font defs for Latin Modern
))
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/base/inputenc.sty
Package: inputenc 2018/08/11 v1.3c Input encoding file
\inpenc@prehook=\toks21
\inpenc@posthook=\toks22
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/microtype/microty
pe.sty
Package: microtype 2019/11/18 v2.7d Micro-typographical refinements (RS)

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics/keyval.s
ty
Package: keyval 2014/10/28 v1.15 key=value parser (DPC)
\KV@toks@=\toks23
)
\MT@toks=\toks24
\MT@count=\count188
LaTeX Info: Redefining \textls on input line 790.
\MT@outer@kern=\dimen144
LaTeX Info: Redefining \textmicrotypecontext on input line 1354.
\MT@listname@count=\count189

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/microtype/microty
pe-pdftex.def
File: microtype-pdftex.def 2019/11/18 v2.7d Definitions specific to pdftex (RS)

LaTeX Info: Redefining \lsstyle on input line 914.
LaTeX Info: Redefining \lslig on input line 914.
\MT@outer@space=\skip52
)
Package microtype Info: Loading configuration file microtype.cfg.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/microtype/microty
pe.cfg
File: microtype.cfg 2019/11/18 v2.7d microtype main configuration file (RS)
))
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/hyperref/hyperref
.sty
Package: hyperref 2020-05-15 v7.00e Hypertext links for LaTeX

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/ltxcmds/ltxcmds
.sty
Package: ltxcmds 2019/12/15 v1.24 LaTeX kernel commands for general use (HO)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/pdftexcmds/pdft
excmds.sty
Package: pdftexcmds 2020-06-27 v0.33 Utility functions of pdfTeX for LuaTeX (HO
)

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/infwarerr/infwa
rerr.sty
Package: infwarerr 2019/12/03 v1.5 Providing info/warning/error messages (HO)
)
Package pdftexcmds Info: \pdf@primitive is available.
Package pdftexcmds Info: \pdf@ifprimitive is available.
Package pdftexcmds Info: \pdfdraftmode found.
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/kvsetkeys/kvset
keys.sty
Package: kvsetkeys 2019/12/15 v1.18 Key value parser (HO)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/kvdefinekeys/kv
definekeys.sty
Package: kvdefinekeys 2019-12-19 v1.6 Define keys (HO)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/pdfescape/pdfes
cape.sty
Package: pdfescape 2019/12/09 v1.15 Implements pdfTeX's escape features (HO)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/hycolor/hycolor.s
ty
Package: hycolor 2020-01-27 v1.10 Color options for hyperref/bookmark (HO)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/letltxmacro/letlt
xmacro.sty
Package: letltxmacro 2019/12/03 v1.6 Let assignment for LaTeX macros (HO)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/auxhook/auxhook.s
ty
Package: auxhook 2019-12-17 v1.6 Hooks for auxiliary files (HO)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/kvoptions/kvoptio
ns.sty
Package: kvoptions 2019/11/29 v3.13 Key value format for package options (HO)
)
\@linkdim=\dimen145
\Hy@linkcounter=\count190
\Hy@pagecounter=\count191

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/hyperref/pd1enc.d
ef
File: pd1enc.def 2020-05-15 v7.00e Hyperref: PDFDocEncoding definition (HO)
Now handling font encoding PD1 ...
... no UTF-8 mapping file for font encoding PD1
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/intcalc/intcalc
.sty
Package: intcalc 2019/12/15 v1.3 Expandable calculations with integers (HO)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/etexcmds/etexcm
ds.sty
Package: etexcmds 2019/12/15 v1.7 Avoid name clashes with e-TeX commands (HO)
)
\Hy@SavedSpaceFactor=\count192
Package hyperref Info: Option `unicode' set `true' on input line 4338.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/hyperref/puenc.de
f
File: puenc.def 2020-05-15 v7.00e Hyperref: PDF Unicode definition (HO)
Now handling font encoding PU ...
... no UTF-8 mapping file for font encoding PU
)
Package hyperref Info: Hyper figures OFF on input line 4464.
Package hyperref Info: Link nesting OFF on input line 4469.
Package hyperref Info: Hyper index ON on input line 4472.
Package hyperref Info: Plain pages OFF on input line 4479.
Package hyperref Info: Backreferencing OFF on input line 4484.
Package hyperref Info: Implicit mode ON; LaTeX internals redefined.
Package hyperref Info: Bookmarks ON on input line 4717.
\c@Hy@tempcnt=\count193
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/url/url.sty
\Urlmuskip=\muskip17
Package: url 2013/09/16  ver 3.4  Verb mode for urls, etc.
)
LaTeX Info: Redefining \url on input line 5076.
\XeTeXLinkMargin=\dimen146

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/bitset/bitset.s
ty
Package: bitset 2019/12/09 v1.3 Handle bit-vector datatype (HO)

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/bigintcalc/bigi
ntcalc.sty
Package: bigintcalc 2019/12/15 v1.5 Expandable calculations on big integers (HO
)
))
\Fld@menulength=\count194
\Field@Width=\dimen147
\Fld@charsize=\dimen148
Package hyperref Info: Hyper figures OFF on input line 6347.
Package hyperref Info: Link nesting OFF on input line 6352.
Package hyperref Info: Hyper index ON on input line 6355.
Package hyperref Info: backreferencing OFF on input line 6362.
Package hyperref Info: Link coloring OFF on input line 6367.
Package hyperref Info: Link coloring with OCG OFF on input line 6372.
Package hyperref Info: PDF/A mode OFF on input line 6377.
LaTeX Info: Redefining \ref on input line 6417.
LaTeX Info: Redefining \pageref on input line 6421.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/atbegshi/atbegs
hi.sty
Package: atbegshi 2019/12/05 v1.19 At begin shipout hook (HO)
)
\Hy@abspage=\count195
\c@Item=\count196
\c@Hfootnote=\count197
)
Package hyperref Info: Driver (autodetected): hpdftex.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/hyperref/hpdftex.
def
File: hpdftex.def 2020-05-15 v7.00e Hyperref driver for pdfTeX

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/atveryend/atverye
nd.sty
Package: atveryend 2019-12-11 v1.11 Hooks at the very end of document (HO)
Package atveryend Info: \enddocument detected (standard20110627).
)
\Fld@listcount=\count198
\c@bookmark@seq@number=\count199

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/rerunfilecheck/re
runfilecheck.sty
Package: rerunfilecheck 2019/12/05 v1.9 Rerun checks for auxiliary files (HO)

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/uniquecounter/u
niquecounter.sty
Package: uniquecounter 2019/12/15 v1.4 Provide unlimited unique counter (HO)
)
Package uniquecounter Info: New unique counter `rerunfilecheck' on input line 2
86.
)
\Hy@SectionHShift=\skip53
)
Package hyperref Info: Option `breaklinks' set `true' on input line 30.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/geometry/geometry
.sty
Package: geometry 2020/01/02 v5.9 Page Geometry

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/iftex/ifvtex.st
y
Package: ifvtex 2019/10/25 v1.7 ifvtex legacy package. Use iftex instead.
)
\Gm@cnth=\count266
\Gm@cntv=\count267
\c@Gm@tempcnt=\count268
\Gm@bindingoffset=\dimen149
\Gm@wd@mp=\dimen150
\Gm@odd@mp=\dimen151
\Gm@even@mp=\dimen152
\Gm@layoutwidth=\dimen153
\Gm@layoutheight=\dimen154
\Gm@layouthoffset=\dimen155
\Gm@layoutvoffset=\dimen156
\Gm@dimlist=\toks25
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/tools/longtable.s
ty
Package: longtable 2020/01/07 v4.13 Multi-page Table package (DPC)
\LTleft=\skip54
\LTright=\skip55
\LTpre=\skip56
\LTpost=\skip57
\LTchunksize=\count269
\LTcapwidth=\dimen157
\LT@head=\box47
\LT@firsthead=\box48
\LT@foot=\box49
\LT@lastfoot=\box50
\LT@cols=\count270
\LT@rows=\count271
\c@LT@tables=\count272
\c@LT@chunks=\count273
\LT@p@ftn=\toks26
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/booktabs/booktabs
.sty
Package: booktabs 2020/01/12 v1.61803398 Publication quality tables
\heavyrulewidth=\dimen158
\lightrulewidth=\dimen159
\cmidrulewidth=\dimen160
\belowrulesep=\dimen161
\belowbottomsep=\dimen162
\aboverulesep=\dimen163
\abovetopsep=\dimen164
\cmidrulesep=\dimen165
\cmidrulekern=\dimen166
\defaultaddspace=\dimen167
\@cmidla=\count274
\@cmidlb=\count275
\@aboverulesep=\dimen168
\@belowrulesep=\dimen169
\@thisruleclass=\count276
\@lastruleclass=\count277
\@thisrulewidth=\dimen170
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/mdwtools/footnote
.sty
Package: footnote 1997/01/28 1.13 Save footnotes around boxes
\fn@notes=\box51
\fn@width=\dimen171
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics/graphicx
.sty
Package: graphicx 2019/11/30 v1.2a Enhanced LaTeX Graphics (DPC,SPQR)

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics/graphics
.sty
Package: graphics 2019/11/30 v1.4a Standard LaTeX Graphics (DPC,SPQR)

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics/trig.sty
Package: trig 2016/01/03 v1.10 sin cos tan (DPC)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics-cfg/grap
hics.cfg
File: graphics.cfg 2016/06/04 v1.11 sample graphics configuration
)
Package graphics Info: Driver file: pdftex.def on input line 105.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics-def/pdft
ex.def
File: pdftex.def 2020/08/26 v1.2 Graphics/color driver for pdftex
))
\Gin@req@height=\dimen172
\Gin@req@width=\dimen173
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/grffile/grffile.s
ty
Package: grffile 2019/11/11 v2.1 Extended file name support for graphics (legac
y)
Package grffile Info: This package is an empty stub for compatibility on input 
line 40.
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/parskip/parskip.s
ty
Package: parskip 2020-06-15 v2.0f non-zero parskip adjustments

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/etoolbox/etoolbox
.sty
Package: etoolbox 2020/08/24 v2.5j e-TeX tools for LaTeX (JAW)
\etb@tempcnta=\count278
))
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/l3backend/l3backe
nd-pdfmode.def
File: l3backend-pdfmode.def 2020-08-07 L3 backend support: PDF mode
\l__kernel_color_stack_int=\count279
\l__pdf_internal_box=\box52
) (./XYH.aux)
\openout1 = `XYH.aux'.

LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 75.
LaTeX Font Info:    ... okay on input line 75.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 75.
LaTeX Font Info:    ... okay on input line 75.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 75.
LaTeX Font Info:    ... okay on input line 75.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 75.
LaTeX Font Info:    ... okay on input line 75.
LaTeX Font Info:    Checking defaults for TS1/cmr/m/n on input line 75.
LaTeX Font Info:    ... okay on input line 75.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 75.
LaTeX Font Info:    ... okay on input line 75.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 75.
LaTeX Font Info:    ... okay on input line 75.
LaTeX Font Info:    Checking defaults for PD1/pdf/m/n on input line 75.
LaTeX Font Info:    ... okay on input line 75.
LaTeX Font Info:    Checking defaults for PU/pdf/m/n on input line 75.
LaTeX Font Info:    ... okay on input line 75.
LaTeX Info: Redefining \microtypecontext on input line 75.
Package microtype Info: Generating PDF output.
Package microtype Info: Character protrusion enabled (level 2).
Package microtype Info: Using protrusion set `basicmath'.
Package microtype Info: Automatic font expansion enabled (level 2),
(microtype)             stretch: 20, shrink: 20, step: 1, non-selected.
Package microtype Info: Using default expansion set `basictext'.
LaTeX Info: Redefining \showhyphens on input line 75.
Package microtype Info: No adjustment of tracking.
Package microtype Info: No adjustment of interword spacing.
Package microtype Info: No adjustment of character kerning.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/microtype/mt-cmr.
cfg
File: mt-cmr.cfg 2013/05/19 v2.2 microtype config. file: Computer Modern Roman 
(RS)
)
\AtBeginShipoutBox=\box53
Package hyperref Info: Link coloring OFF on input line 75.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/hyperref/nameref.
sty
Package: nameref 2019/09/16 v2.46 Cross-referencing by name of section

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/refcount/refcount
.sty
Package: refcount 2019/12/15 v3.6 Data extraction from label references (HO)
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/gettitlestring/
gettitlestring.sty
Package: gettitlestring 2019/12/15 v1.6 Cleanup title references (HO)
)
\c@section@level=\count280
)
LaTeX Info: Redefining \ref on input line 75.
LaTeX Info: Redefining \pageref on input line 75.
LaTeX Info: Redefining \nameref on input line 75.
(./XYH.out) (./XYH.out)
\@outlinefile=\write3
\openout3 = `XYH.out'.

*geometry* driver: auto-detecting
*geometry* detected driver: pdftex
*geometry* verbose mode - [ preamble ] result:
* driver: pdftex
* paper: <default>
* layout: <same size as paper>
* layoutoffset:(h,v)=(0.0pt,0.0pt)
* modes: 
* h-part:(L,W,R)=(72.26999pt, 469.75502pt, 72.26999pt)
* v-part:(T,H,B)=(72.26999pt, 650.43001pt, 72.26999pt)
* \paperwidth=614.295pt
* \paperheight=794.96999pt
* \textwidth=469.75502pt
* \textheight=650.43001pt
* \oddsidemargin=0.0pt
* \evensidemargin=0.0pt
* \topmargin=-37.0pt
* \headheight=12.0pt
* \headsep=25.0pt
* \topskip=10.0pt
* \footskip=30.0pt
* \marginparwidth=65.0pt
* \marginparsep=11.0pt
* \columnsep=10.0pt
* \skip\footins=9.0pt plus 4.0pt minus 2.0pt
* \hoffset=0.0pt
* \voffset=0.0pt
* \mag=1000
* \@twocolumnfalse
* \@twosidefalse
* \@mparswitchfalse
* \@reversemarginfalse
* (1in=72.27pt=25.4mm, 1cm=28.453pt)


(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/epstopdf-pkg/epst
opdf-base.sty
Package: epstopdf-base 2020-01-24 v2.11 Base part for package epstopdf
Package epstopdf-base Info: Redefining graphics rule for `.eps' on input line 4
85.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/latexconfig/epsto
pdf-sys.cfg
File: epstopdf-sys.cfg 2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Liv
e
))
LaTeX Font Info:    Trying to load font information for OT1+lmr on input line 7
7.
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/lm/ot1lmr.fd
File: ot1lmr.fd 2009/10/30 v1.6 Font defs for Latin Modern
)
LaTeX Font Info:    Trying to load font information for OML+lmm on input line 7
7.
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/lm/omllmm.fd
File: omllmm.fd 2009/10/30 v1.6 Font defs for Latin Modern
)
LaTeX Font Info:    Trying to load font information for OMS+lmsy on input line 
77.
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/lm/omslmsy.fd
File: omslmsy.fd 2009/10/30 v1.6 Font defs for Latin Modern
)
LaTeX Font Info:    Trying to load font information for OMX+lmex on input line 
77.
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/lm/omxlmex.fd
File: omxlmex.fd 2009/10/30 v1.6 Font defs for Latin Modern
)
LaTeX Font Info:    External font `lmex10' loaded for size
(Font)              <12> on input line 77.
LaTeX Font Info:    External font `lmex10' loaded for size
(Font)              <8> on input line 77.
LaTeX Font Info:    External font `lmex10' loaded for size
(Font)              <6> on input line 77.
LaTeX Font Info:    Trying to load font information for U+msa on input line 77.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsfonts/umsa.fd
File: umsa.fd 2013/01/14 v3.01 AMS symbols A
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/microtype/mt-msa.
cfg
File: mt-msa.cfg 2006/02/04 v1.1 microtype config. file: AMS symbols (a) (RS)
)
LaTeX Font Info:    Trying to load font information for U+msb on input line 77.

(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsfonts/umsb.fd
File: umsb.fd 2013/01/14 v3.01 AMS symbols B
)
(c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/microtype/mt-msb.
cfg
File: mt-msb.cfg 2005/06/01 v1.0 microtype config. file: AMS symbols (b) (RS)
) (./XYH.toc)
\tf@toc=\write4
\openout4 = `XYH.toc'.


LaTeX Warning: Reference `fig:cars-plot' on page 1 undefined on input line 82.

<XYH_files/figure-latex/cars-plot-1.pdf, id=4, 469.755pt x 325.215pt>
File: XYH_files/figure-latex/cars-plot-1.pdf Graphic file (type pdf)
<use XYH_files/figure-latex/cars-plot-1.pdf>
Package pdftex.def Info: XYH_files/figure-latex/cars-plot-1.pdf  used on input 
line 83.
(pdftex.def)             Requested size: 469.75386pt x 325.2142pt.

Overfull \hbox (63.91412pt too wide) in paragraph at lines 82--84
[]\T1/lmr/m/n/10 (-20) See Fig-ure []. [] 
 []

Package atveryend Info: Empty hook `BeforeClearDocument' on input line 85.
[1

{c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-var/fonts/map/pdftex/updmap/pdft
ex.map} <./XYH_files/figure-latex/cars-plot-1.pdf>]
Package atveryend Info: Empty hook `AfterLastShipout' on input line 85.
(./XYH.aux)
Package atveryend Info: Executing hook `AtVeryEndDocument' on input line 85.
Package atveryend Info: Executing hook `AtEndAfterFileList' on input line 85.
Package rerunfilecheck Info: File `XYH.out' has not changed.
(rerunfilecheck)             Checksum: D41D8CD98F00B204E9800998ECF8427E;0.

LaTeX Warning: There were undefined references.

Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 85.
 ) 
Here is how much of TeX's memory you used:
 11971 strings out of 482841
 179883 string characters out of 5954775
 454289 words of memory out of 5000000
 27112 multiletter control sequences out of 15000+600000
 566028 words of font info for 59 fonts, out of 8000000 for 9000
 14 hyphenation exceptions out of 8191
 43i,6n,42p,966b,325s stack positions out of 5000i,500n,10000p,200000b,80000s
{c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/fonts/enc/dvips/lm/lm-ec.
enc}<c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/fonts/type1/public/lm/l
mbx10.pfb><c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/fonts/type1/publi
c/lm/lmbx12.pfb><c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/fonts/type1
/public/lm/lmr10.pfb><c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/fonts/
type1/public/lm/lmr12.pfb><c:/Users/lenovo/AppData/Roaming/TinyTeX/texmf-dist/f
onts/type1/public/lm/lmr17.pfb>
Output written on XYH.pdf (1 page, 116771 bytes).
PDF statistics:
 41 PDF objects out of 1000 (max. 8388607)
 30 compressed objects within 1 object stream
 3 named destinations out of 1000 (max. 500000)
 8198 words of extra memory for PDF output out of 10000 (max. 10000000)

That does not appear to be a .tex file.

I don't know if this is a typo when you created the post here, but in case it is not, you need a space between Figure and \@ref

See Figure \@ref(fig:cars-plot)
1 Like

A space isn't required. \LaTeX will pick up the escape character anywhere.

1 Like

Here is the entire body of my xref_fig.Rmd file again,

---
output:
  bookdown::pdf_document2:
    keep_tex: true
---

```{r a, fig.cap="The cars data.", echo=FALSE}
plot(cars)
```

See Figure \@ref(fig:a).

And this is the xref_fig.tex file it produces,

% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{longtable,booktabs}
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}

\author{}
\date{\vspace{-2.5em}}

\begin{document}

{
\setcounter{tocdepth}{2}
\tableofcontents
}
\begin{figure}
\centering
\includegraphics{xref_fig_files/figure-latex/a-1.pdf}
\caption{\label{fig:a}The cars data.}
\end{figure}

See Figure \ref{fig:a}.

\end{document}

And, for completeness, here is a screenshot of my output,

My hope is that by seeing your .tex file and comparing it to what it should create, we'll be able to ascertain where the error is happening.

1 Like

That is not true. You can reference before or after.

I'm glad it's working for you now, but we still haven't figured out what your issue was.

1 Like

Sorry that I just confused the pdt output of my computer with your picuture :joy:
The Rmarkdown still fails to cross-referencing figures and here are codes in the .tex file:

\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\PassOptionsToPackage{hyphens}{url} % url is loaded by hyperref
\usepackage[unicode=true]{hyperref}
\hypersetup{
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage[margin=1in]{geometry}
\usepackage{longtable,booktabs}
% Fix footnotes in tables (requires footnote package)
\IfFileExists{footnote.sty}{\usepackage{footnote}\makesavenoteenv{long table}}{}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

% set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother


\author{}
\date{\vspace{-2.5em}}

\begin{document}

{
\setcounter{tocdepth}{2}
\tableofcontents
}
\includegraphics{XYH_files/figure-latex/a-1.pdf} See Figure \ref{fig:a}.

\end{document}
1 Like

Thanks for the precision, you are completly right. For Latex it won't but I believe some of bookdown features can be sensible to that - maybe only for HTML. Anyhow, this is not the issue here it seems...

There is indeed an issue as here there is a missing part about label and caption compare to @elmstedt example. The figure environment should be used and inserted when fig.cap is set in the chunk option.

It is very strange that you don't get the same result as the small example above... Did you run it unmodified ?

@XU_Yihao Let's try something else. What is the output of the command,

sessionInfo()

When you post the results, please paste them here inside of a verbatim block, e.g.

```
<-- PASTE IN HERE -->
```

That will keep the formatting of the output and make it easier to read. You can either type the three backticks above and below the output yourself, click the editor button above which looks like </>, or use the shortcut Ctrl+Shift+C. Doing this whenever you are posting code or code output will help you get, on the whole, better quality help.

Not relevant to issues with R Markdown, but if and when you have issues with R code, you should strongly consider making a reprex.

1 Like

Thanks for your useful tips and patience!
The output of the command is:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936  LC_CTYPE=Chinese (Simplified)_China.936   
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C                              
[5] LC_TIME=Chinese (Simplified)_China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2    yaml_2.2.1

Thanks for your useful tips and patience!
The output of the command is:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936  LC_CTYPE=Chinese (Simplified)_China.936   
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C                              
[5] LC_TIME=Chinese (Simplified)_China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2    yaml_2.2.1

Dear all, when I install the new version RStudio (1.4.873) and use the cross-reference function in the visual mode, cross-references for figures are available.

1 Like

This is strange but is worthy of trying

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.