-1

I am trying to read .tex files containing LaTeX code, and paste their content into different .tex files depending on the results of calculations in R.

I need to avoid changing any character of the tex files by processing them with R. I am looking for a way to stop R from interpreting the content of the files and make R just "copy" the files character for character.

Example R file:

cont <- paste(readLines("path/to/file/a.tex"), collapse = "\n")

write.table(cont , file = "Mother.tex", append = FALSE, quote = FALSE, sep = "",
              eol = "\n", na = "NA", dec = ".", row.names = FALSE,
              col.names = FALSE, qmethod = c("escape", "double"),
              fileEncoding = "")

cont2 <- paste(readLines("path/to/file/b.tex"), collapse = "\n")

write.table(cont2 , file = "Mother.tex", append = TRUE, quote = FALSE, sep = "",
              eol = "\n", na = "NA", dec = ".", row.names = FALSE,
              col.names = FALSE, qmethod = c("escape", "double"),
              fileEncoding = "")

cont3 <- paste(readLines("path/to/file/c.tex"), collapse = "\n")

write.table(cont3 , file = "Mother.tex", append = TRUE, quote = FALSE, sep = "",
              eol = "\n", na = "NA", dec = ".", row.names = FALSE,
              col.names = FALSE, qmethod = c("escape", "double"),
              fileEncoding = "")


cont4 <- paste(readLines("path/to/file/d.tex"), collapse = "\n")

write.table(cont4 , file = "Mother.tex", append = TRUE, quote = FALSE, sep = "",
              eol = "\n", na = "NA", dec = ".", row.names = FALSE,
              col.names = FALSE, qmethod = c("escape", "double"),
              fileEncoding = "")

Example Latex File a:

\documentclass{beamer}
\usepackage{listings}
\lstset{basicstyle=\ttfamily, keywordstyle=\bfseries}

\begin{document}

Example Latex file b:

\begin{frame}

Example Latex file c:

content based on values in r
\end{frame}

Example Latex file d:

\end{document}

I do have two Problems now:

  1. wrong escape information for readlines
  2. non utf-8 keyword at files: b,c,d

Latex is not abled to compile sucessfully, because theres an non utf-8 information inside the Motherfile after processing Mother with r.

If i do copy and paste the content of each file manually i am abled to compile Latex sucessfully. As a result of the information about bad utf-8 information in Latex (no wrong Characters in TexLive IDE shown) I suspect r to add information into the files, which is not shown by IDE TextLive.

I do not understand why theres something "invisible" added into my Mother tex file which is not shown inside TexLive.

Hans Peter
  • 99
  • 8
  • I don't see what the issue is. `.tex` files are simple text files. If R opens the text file and treats the content as strings, it won't be interpreting the contents. Perhaps you can give an example which shows how R is interpreting the TeX content when you don't want it to. – John Coleman Nov 01 '21 at 14:41
  • i did try to copy a working latex file and after processing it its not cimpilable. If i had a clue what went wrong i could ask a better, a question of higher quality. I am sorry about that – Hans Peter Nov 01 '21 at 14:52
  • You could show what you tried. The way to ask a better question is to strive to make a [mcve] which enables those who read the question to replicate the problem. We have even less of a clue than you until you enlighten us. Does the answer posted below answer your question? If so, accept it. If not, please make an effort to clarify just what the problem is. – John Coleman Nov 01 '21 at 15:39

1 Answers1

3

Assuming you want to store the content of the .tex file into a string.

cont <- paste(readLines("path/to/file/file.tex"), collapse = "\n")
tacoman
  • 882
  • 6
  • 10
  • I did use your example to add more details. There might be added some escape Characters which are not shown in editors but do have an impact on the compiling process in Latex. – Hans Peter Nov 02 '21 at 09:40