4

I want to have a barcode like given in this answer Can't generate code128 barcode

There also stands that it doesn't work with pdflatex engine - it has to be changed to the xelatex engine and that's done with latex_engine: xelatex in the YAML-header (hopefully?!).

So I build up my Rmd file

---
geometry: paperheight=8cm,paperwidth=12cm,margin=0.5cm
output: pdf_document
    latex_engine: xelatex
fontsize: 10pt
header-includes:
  - \pagenumbering{gobble}
  - \usepackage{pst-barcode,pstricks-add}
params:
  barCodeNumber: 12345678
---

```{=latex}
\psset{unit=1in}
\begin{pspicture}(3.5,1.2)
    \psbarcode{`r params[["barCodeNumber"]]`}{includetext}{code128}
\end{pspicture}
```

But sadly it fails with the following error message (render("C:/Users/tueftla/R/Test.Rmd") ):

Error in yaml::yaml.load(..., eval.expr = TRUE) : 
Scanner error: mapping values are not allowed in this context at line 3, column 17

What must I change? And which kind of change must I do, that the barcode number is not displayed?

Any hints are appreciate and many thanks in advance.

1
  • What will we find in " line 3, column 17" of the yaml-file? Can you post it (tagged as code)?
    – MS-SPO
    Mar 15 at 18:22

3 Answers 3

5

Your issue here lies in the YAML header specification. See this section (3.3.7.1 LaTeX engine) on how to specify the LaTeX engine. Specifically, you need

output:
  pdf_document:
    latex_engine: xelatex

You were missing the colon after pdf_document as the "options" that associated with it.

---
geometry: paperheight = 8cm, paperwidth = 12cm, margin = 0.5cm
output:
  pdf_document:
    latex_engine: xelatex
fontsize: 10pt
header-includes:
  - \pagenumbering{gobble}
  - \usepackage{pst-barcode,pstricks-add}
params:
  barCodeNumber: 12345678
---

```{=latex}
\psset{unit=1in}
\begin{pspicture}(3.5,1.2)
    \psbarcode{`r params[["barCodeNumber"]]`}{includetext}{code128}
\end{pspicture}
```

enter image description here

1
  • 1
    After running your code I had again an error (Error in yaml::yaml.load(..., eval.expr = TRUE) : Scanner error: while scanning for the next token at line 3, column 1 found character that cannot start any token at line 3, column 1) but here the solution was removing the tabs in the YAML header and do it with spaces (github.com/moraes/config/issues/1)
    – tueftla
    Mar 16 at 12:48
3

Just for reference, this would be a valid .tex file copied from your RMD file, compiled with XeLaTeX:

\documentclass[10pt,a4paper]{article}
\usepackage{pst-barcode,pstricks-add}

\begin{document}
\psset{unit=1in}
\begin{pspicture}(3.5,1.2)
    \psbarcode{`r params[["barCodeNumber"]]`}{includetext}{code128}
\end{pspicture}

\begin{pspicture}(3.5,1.2)
    \psbarcode{12345678}{includetext}{code128}
\end{pspicture}
\end{document}

As you can see, it takes the first of the 3 arguments literally:

result

So the key questions wrt to your question are:

  • will the first argument r params[["barCodeNumber"]] be replaced or not?
  • if so: by what?
  • and: why should your R-system replace it (successfully)?

As indicated, it would be helpful, if you'd post the YAML file.

1
  • See @Werner answer. R is doing the job fine... And the YAML header is in the Rmd file --- YAML header ---
    – tueftla
    Mar 16 at 12:51
3

Better if you use:

  • Use the newer Quarto syntax/format. Xelatex is the default LaTeX engine here, so at least that's what you save upfront.

  • Avoid as far as possible complex inline R code and complex inline R outputs, and you'll be happy.

MWE

---
format: pdf
geometry:
- margin=0.5cm
- paperheight=8cm
- paperwidth=12cm
header-includes:
- \usepackage{pst-barcode,pstricks-add}
- \pagenumbering{gobble}
params:
  barCodeNumber: 12345678
---

```{r}
#| echo: false

x <- params$barCodeNumber

```

```{=latex}
\psset{unit=1in}
\begin{pspicture}(3.5,1.2)
\psbarcode{`{r} x`}{includetext}{code128}
\end{pspicture}
```

However, with the old Rmarkdown this also work:

---
geometry: paperheight=8cm,paperwidth=12cm,margin=0.5cm
output: 
  pdf_document: 
    latex_engine: xelatex
fontsize: 10pt
header-includes:
  - \usepackage{pst-barcode,pstricks-add}
  - \pagenumbering{gobble}  
params:
  barCodeNumber: 12345678
---


```{r}
#| echo: false

x <- params$barCodeNumber

```


```{=latex}
\psset{unit=1in}
\begin{pspicture}(3.5,1.2)
    \psbarcode{`r x`}{includetext}{code128}
\end{pspicture}
```

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .