1

I am trying to add my R-Code to my Latex file.

This is the R-Code (output) that I saved in a R-file

Coefficients:
              Estimate    Std. Error  t value   Pr(>|t|)  
(Intercept)   39.534      868.517     0.046     0.9648  
Besuche10     18.881      9.187       2.055     0.0739 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 385.5 on 8 degrees of freedom
Multiple R-squared:  0.3455,    Adjusted R-squared:  0.2637 
F-statistic: 4.224 on 1 and 8 DF,  p-value: 0.07391

When I compile my Latex file, Latex has a problem with the ***-symbols:

Package inputenc Error: Invalid UTF-8 byte sequence

How can I solve this problem?

Thanks you for help.

2
  • Try to change the symbol ‘ by typing it directly in your LaTeX editor. Use "find and replace". Or verify that your R code has been saved in UTF-8.
    – projetmbc
    Jun 13, 2020 at 22:43
  • I have checked, my R code is saved in UFT-8. I have incorporated the R code via \lstinputlisting{script.R}, so I cannot change the symbol in my LaTeX editor.
    – user77512
    Jun 13, 2020 at 22:52

1 Answer 1

1

Your problem with listings can be solved in this way:

Example1.tex:

\documentclass{article}
\usepackage{listings}
\lstset{
    inputencoding=utf8,
    extendedchars=true,
    literate={‘}{{`}}1 {’}{{'}}1,}
\begin{document}
\lstinputlisting{script.R}
\end{document}

But as there are not any syntax code in script.R, probably is better just use verbatim:

Example2.tex:

\documentclass{article}
\usepackage{verbatim}
\begin{document}
\verbatiminput{script.R}
\end{document}

Said that, do not include externally saved R outputs. Use R to produce that output from the document itself with knitr. The example below show some approaches to show parts of a regression model summary.

The code is a bit complex mainly due to the added option to cut the raw summary as you do manually. Without output.lines option, show the summary is fairly simple.

The other alternative is extract values this format and format these elements in your own LaTeX summary. The main hack here could be if you need the asteriks, that are not in the matrix of coefficients.

But anyway, all the outputs will be updated automatically if you change the source data, while using a saved outputs, or copying and pasting ...

Example3.Rnw

(if you do not know how compile this file, use Rstudio > Compile PDF).

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{booktabs,parskip,tabto}
\begin{document}

\section*{Sample data}
  
<<xxx,echo=F>>=
# Sample data and model regression
var <- c(1,2,3,4,5,6,7,8,9) 
Besuche <- c(111,231,328,450,541,613,711,815,919)
mod <-  lm(var~Besuche)
@

var = \Sexpr{var}\par Besuche = \Sexpr{Besuche}

\section*{Raw}

% https://stackoverflow.com/a/23205752/2803235

<<output,echo=F>>=
library(knitr)
hook_output <- knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
  lines <- options$output.lines
  if (is.null(lines)) {
    return(hook_output(x, options))  # pass to default hook
  }
  x <- unlist(strsplit(x, "\n"))
  more <- "   "
  if (length(lines)==1) {        # first n lines
    if (length(x) > lines) {
      # truncate the output, but add ....
      x <- c(head(x, lines), more)
    }
  } else {
    x <- c(more, x[lines], more)
  }
  # paste these lines together
  x <- paste(c(x, ""), collapse = "\n")
  hook_output(x, options)
})
@
  
<<zzz,echo=F,output.lines=-(1:8),comment=" ">>=
y <- summary(mod)
y
@
  
\section*{Cooked}

Coefficients: 
  
<<test,echo=F,results='asis'>>=
library(xtable)
options(xtable.floating = FALSE)

DF <- read.table(textConnection(#
      capture.output(summary(mod))[11:12]), fill = T)

names(DF) <- c(" ", colnames(coef(summary(mod))), "Signif.")

print(xtable(DF,digits=c(0,0,3,3,3,2,0),display=c("s","s","g","g","g","g","s")), math.style.exponents=T, include.rownames = F, booktabs = T)
@

<<buklreport,echo=F,output.lines=-(1:12),results="asis">>=
y
@

\section*{Floating version}

See the table \ref{float}.

\begin{table}[hb!]
\centering
\caption{Regression coefficients.}\label{float}
<<test2,echo=F,results="asis">>=
print(xtable(y), booktabs = T)
@
\begin{tabular}{ll}
Residual standard error: &   \Sexpr{round(y$sigma,4)} 
on \Sexpr{y$df[2]} degrees of freedom.\\
Multiple R$^2$: & \Sexpr{round(y$r.squared,4)}.\\  
Adjusted R$^2$: & \Sexpr{round(y$adj.r.squared,4)}\\ 
F-statistic:  & \Sexpr{round(y$fstatistic[1],0)} on 
\Sexpr{y$fstatistic[2]} and  
\Sexpr{y$fstatistic[3]} DF.\\
p-value: & \Sexpr{signif(y$coef[,4][2],4)} \\
\end{tabular}
\end{table}

\end{document}

mwe

You must log in to answer this question.