Inserting document structure using tree command in PDF output

Hi!

For writing the documentation of one project, I wanted to insert the folder structure. But I am unable to do so, probably due to some encoding issues.

Here's a sample Rmd file to reproduce the issue:

---
output: pdf_document
---

some text 1

```{bash preparing directory}
mkdir test_dir
mkdir test_dir/test_subdir_1
mkdir test_dir/test_subdir_2
touch test_dir/test_subdir_1/test_file_1
touch test_dir/test_subdir_1/test_file_2
mkdir test_dir/test_subdir_2/test_sub_subdir_1
touch test_dir/test_subdir_2/test_sub_subdir_1/test_file_3
touch test_dir/test_subdir_2/test_file_4
```

some text 2

```{bash}
tree --charset=ascii test_dir/
```

some text 3

```{bash removing directory}
rm -r test_dir
```

some text 4

It gives the following output in the R Markdown tab of RStudio:

  |..........                                                            |  14%
  ordinary text without R code

  |....................                                                  |  29%
label: preparing directory (with options) 
List of 1
 $ engine: chr "bash"



processing file: Untitled.Rmd
running: bash  -c 'mkdir test_dir
mkdir test_dir/test_subdir_1
mkdir test_dir/test_subdir_2
touch test_dir/test_subdir_1/test_file_1
touch test_dir/test_subdir_1/test_file_2
mkdir test_dir/test_subdir_2/test_sub_subdir_1
touch test_dir/test_subdir_2/test_sub_subdir_1/test_file_3
touch test_dir/test_subdir_2/test_file_4'
  |..............................                                        |  43%
  ordinary text without R code

  |........................................                              |  57%
label: unnamed-chunk-1 (with options) 
List of 1
 $ engine: chr "bash"

running: bash  -c 'tree --charset=ascii test_dir'
  |..................................................                    |  71%
  ordinary text without R code

  |............................................................          |  86%
label: removing directory (with options) 
List of 1
 $ engine: chr "bash"

running: bash  -c 'rm -r test_dir'
  |......................................................................| 100%
  ordinary text without R code


/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS Untitled.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output Untitled.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rmarkdown/rmd/lua/latex-div.lua --variable 'geometry:margin=1in' 
output file: Untitled.knit.md

! Package inputenc Error: Unicode character ^^[ (U+001B)
(inputenc)                not set up for use with LaTeX.

Try other LaTeX engines instead (e.g., xelatex) if you are using pdflatex. For R Markdown users, see https://bookdown.org/yihui/rmarkdown/pdf-document.html
Error: Failed to compile Untitled.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See Untitled.log for more info.
Execution halted

I tried to use xelatex as suggested, and changed the header to this:

---
output: 
  pdf_document: 
    latex_engine: xelatex
---

This time, I get this message:



processing file: Untitled.Rmd
  |..........                                                            |  14%
  ordinary text without R code

  |....................                                                  |  29%
label: preparing directory (with options) 
List of 1
 $ engine: chr "bash"

running: bash  -c 'mkdir test_dir
mkdir test_dir/test_subdir_1
mkdir test_dir/test_subdir_2
touch test_dir/test_subdir_1/test_file_1
touch test_dir/test_subdir_1/test_file_2
mkdir test_dir/test_subdir_2/test_sub_subdir_1
touch test_dir/test_subdir_2/test_sub_subdir_1/test_file_3
touch test_dir/test_subdir_2/test_file_4'
  |..............................                                        |  43%
  ordinary text without R code

  |........................................                              |  57%
label: unnamed-chunk-1 (with options) 
List of 1
 $ engine: chr "bash"

running: bash  -c 'tree --charset=ascii test_dir/'
  |..................................................                    |  71%
  ordinary text without R code

  |............................................................          |  86%
label: removing directory (with options) 
List of 1
 $ engine: chr "bash"

  |......................................................................| 100%
  ordinary text without R code


/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS Untitled.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output Untitled.tex --self-contained --highlight-style tango --pdf-engine xelatex --variable graphics --lua-filter /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rmarkdown/rmd/lua/latex-div.lua --variable 'geometry:margin=1in' 
running: bash  -c 'rm -r test_dir'
output file: Untitled.knit.md

! Text line contains an invalid character.
l.134 ## ^^[
            [01;34mtest_dir/^^[[00m 

Error: Failed to compile Untitled.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See Untitled.log for more info.
Execution halted

Here's my session information:

> xfun::session_info('rmarkdown')
R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.2, RStudio 1.2.5033

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  base64enc_0.1.3 digest_0.6.23   evaluate_0.14   glue_1.3.1      graphics_3.6.2 
  grDevices_3.6.2 highr_0.8       htmltools_0.4.0 jsonlite_1.6    knitr_1.26     
  magrittr_1.5    markdown_1.1    methods_3.6.2   mime_0.8        Rcpp_1.0.3     
  rlang_0.4.2     rmarkdown_2.0   stats_3.6.2     stringi_1.4.5   stringr_1.4.0  
  tinytex_0.18    tools_3.6.2     utils_3.6.2     xfun_0.12       yaml_2.2.0     

Pandoc version: 2.3.1

Can someone please help me figuring out the problem?

(I wanted to include the content of the log files, but that exceeds the character limit.)

Thanks.

This post suggests

\usepackage[utf8x]{inputenc} 

Not an answer about bash issue, but I wanted to share that for this I use fs::dir_tree from R directly

In case it may help.

3 Likes

Building on @cderv's suggestion, I got the following to compile by changing

\usepackage[utf8x]{inputenc} 

to

\usepackage[utf8]{inputenc} 

dropping the x

This allowed me to compile to pdf


output: pdf_document
header-includes:

  - \usepackage[utf8]{inputenc}

some text 1

fs::dir_tree(path = ".", recurse = TRUE)

Sorry about the backticks. I've now figured out how to do a cut-and-paste by using HTML markup.

If you wrap your code blocks in 4 back-ticks you can have back-ticks inside your code block. Example:

```{r}
rnorm(100)
```
1 Like

Thanks, I'll experiment with that; didn't work first time with trying to work with an Rmd

1 Like

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