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} 

Thanks for the suggestion, but unfortunately this leads to another error.

Here's my Rmd file:

---
output: pdf_document
header-includes:
  - \usepackage[utf8x]{inputenc}
---

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 folder structure}
tree -F --charset=ascii test_dir/
```

Expected output:

```
test_dir/
|-- test_subdir_1
|   |-- test_file_1
|   `-- test_file_2
`-- test_subdir_2
    |-- test_file_4
    `-- test_sub_subdir_1
        `-- test_file_3

3 directories, 4 files
```

some text 3

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

some text 4

And, here's the output:

rmarkdown::render(input = "~/Untitled.Rmd")
#> 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'
#> running: bash  -c 'tree -F --charset=ascii test_dir/'
#> running: bash  -c 'rm -r test_dir'
#> output file: Untitled.knit.md
#> /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 --include-in-header /var/folders/p4/qzqlr4dd6s97ykbwztn90nlwrkzyyg/T//Rtmprxt4vQ/rmarkdown-straa133213924.html --variable 'geometry:margin=1in'
#> ! LaTeX Error: Option clash for package inputenc.
#> Error: LaTeX failed to compile Untitled.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See Untitled.log for more info.
Session info
sessionInfo()
#> R version 3.6.2 (2019-12-12)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS Catalina 10.15.2
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] compiler_3.6.2  magrittr_1.5    tools_3.6.2     htmltools_0.4.0
#>  [5] yaml_2.2.0      tinytex_0.19    Rcpp_1.0.3      stringi_1.4.5  
#>  [9] rmarkdown_2.0   highr_0.8       knitr_1.27      stringr_1.4.0  
#> [13] xfun_0.12       digest_0.6.23   rlang_0.4.2     evaluate_0.14

If the same code working on your system, can you please share your session information?

I searched with the error LaTeX Error: Option clash for package inputenc, and came across this thread. However, with my limited LaTeX and R markdown knowledge, I did not understand how to use the suggestions.

Also, I do not know much about encodings, but if I specify the character set to be ASCII, why should I get errors apparently related to Unicode? As far as I know, ASCII is a subset.

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.