LaTeX Error When Knitting to pdf

Trying to knit to a pdf with and keep getting the following:

output file: Lab-2.knit.md

! Sorry, but C:\Users\JandT\AppData\Local\Programs\MiKTeX\miktex\bin\x64\pdflatex.exe did not succeed.

! The log file hopefully contains the information to get MiKTeX going again:

! C:\Users\JandT\AppData\Local\MiKTeX\miktex\log\pdflatex.log

Error: LaTeX failed to compile Lab-2.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See Lab-2.log for more info.
Execution halted

In my pdflatex.log I find the following:

2021-01-14 20:22:26,978-0800 INFO pdflatex - this process (18212) started by 'Rterm' with command line: C:\Users\JandT\AppData\Local\Programs\MiKTeX\miktex\bin\x64\pdflatex.exe -halt-on-error -interaction=batchmode Lab-2.tex
2021-01-14 20:22:26,996-0800 INFO pdflatex - allowing known shell commands
2021-01-14 20:22:28,051-0800 FATAL pdflatex.core - GUI framework cannot be initialized.
2021-01-14 20:22:28,051-0800 FATAL pdflatex.core - Data:
2021-01-14 20:22:28,051-0800 FATAL pdflatex.core - Source: Libraries\MiKTeX\UI\Qt\mikuiqt.cpp:77
2021-01-14 20:22:28,051-0800 FATAL pdflatex - GUI framework cannot be initialized.
2021-01-14 20:22:28,051-0800 FATAL pdflatex - Info:
2021-01-14 20:22:28,051-0800 FATAL pdflatex - Source: Libraries\MiKTeX\UI\Qt\mikuiqt.cpp
2021-01-14 20:22:28,052-0800 FATAL pdflatex - Line: 77
2021-01-14 20:22:28,052-0800 INFO pdflatex - this process (18212) finishes with exit code 1

Any idea what's going wrong? I've tried to uninstall and reinstall the package to no avail.

What have you got on line 77?

The error seems related to MikTeX that can't correctly initialized. I am not sure this is linked to any R package, and you already try to install the last version. Could you share a Rmd document causing this error?

If this is something that you would consider, you could try uninstalling MikTeX and using TinyTeX

Thank you for the help! I have both installed, I'm starting to think that is the issue. I tried to uninstall TinyTex but unfortunately nothing happened. Below is the RMD file:

---
title: "Lab 2"

output:
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

```

Pinniped populations in North America (seals and sea lions) rebounded dramatically after non-subsistence hunting was made illegal in the U.S. and Canada. These populations generally followed exponential growth trajectories for many years after hunting ended. They are also top predators in marine ecosystems. Today, there are now fights about how these large and growing populations may be limiting productivity of fisheries. This is especially a concern with struggling salmon populations in the Pacific Northwest where this summer, the US government issued permits to Washington, Oregon, Idaho, and local tribes to cull sea lions in the Columbia River Basin. There are discussions of even larger culls in British Columbia.

Let’s suppose we have a Stellar Sea Lion population with 1000 individuals in 1980 and an instantaneous rate of increase of 0.04 yr-1.

  1. Convert the instantaneous rate of increase to a finite rate of increase, assuming one year time steps and save this new quantity as an object. (1 point)
  
```{r Quesion1, echo=TRUE, include=TRUE, warning = FALSE}
lambda<- exp(.04)
lambda
```


  2. Create an empty vector of zeros that will track the sea lion population each year from 1980 to 2020. Project the population from 1980 to 2020. You don’t need to print this out. (1 point)
  
```{r Quesion2, echo=TRUE, include=TRUE}
knitr::opts_chunk$set(echo = FALSE)
nyrs<- 40
N<- numeric(nyrs+1)
for(t in 1: nyrs){N[t+1]<- lambda*N[t]}
N

```



  3. Project the population forward over that time period using a for loop. (2 points)
  
```{r Quesion3, echo=TRUE, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
nyrs<- 40
N<- numeric(nyrs+1)
N[1]<- 1000
for(t in 1: nyrs){N[t+1]<- lambda*N[t]}
N
```

  4. Make a years vector from 1980 to 2020 and then plot the population over time using a line. Label the axes (2 points)
```{r Quesion4, echo=TRUE, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
years<- c(1980:2020)
plot(x = years, y = N, type = 'l', xlab = 'Time', ylab = 'Population Size', col = 'darkblue')
```

  5. Primary productivity in the North Pacific is largely tied to large-scale climate patterns, such as the Pacific Decadal Oscillation (PDO). Thus, changes in the PDO can lead to major shifts in marine food webs, all the way up to piscivores like pinnipeds. The PDO experienced regime changes in 1997 and 2014. Create an empty vector lambda_t and use a for loop and conditional statements to make it equal to:

- 1.02 up through 1997

- 1.07 between 1998 and 2014

- 1.01 from 2015 to 2019.

Because 2020 is the terminal year, we don’t need to know lambda that year. Hint: have an indexing variable in your for loop that starts at 1, and use the years vector from the previous problem for your conditional statements. (3 points)

```{r Question5, echo=TRUE, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
lambda_t<- c()
for(t in 1:length(years)) {lambda_new=1
  if(years[t] <1998) {lambda_t[t] <- lambda_new+.02} 
  else if (years[t] <2015) {lambda_t[t] <- lambda_new+.07} 
  else {lambda_t[t] <- lambda_new+.01} }
lambda_t
```

  6. Make a new empty population vector. Project the population forward to 2020 using the time varying growth rates, once again assuming there are 1000 individuals in 1980. (Do not use the same variable name that you did in question 3.) (2 points)
```{r Question6, echo = TRUE, include = TRUE, messsage = FALSE, warning= FALSE}
knitr::opts_chunk$set(echo = TRUE)
N_new<- (nyrs+1)
N_new[1]<- 1000
for(t in 1: nyrs){N_new[t+1]<- lambda_t[t]*N_new[t]}
N_new
```

  7. Plot both population trajectories in the same figure. Make them two different colors. Label the axes. (2 points)
```{r Question7, echo=TRUE, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
plot(x = years, y = N, type = 'l', xlab = 'Time', ylab = 'Population Size', col = 'darkblue')
lines(x = years, y = N_new, type = 'l', xlab = 'Time', ylab = 'Population Size', col = 'red')
text(2015,1500, labels = c("Adujsted for PDO"), col = 'red')
text(2015, 3000, labels = c("Original"), col = 'darkblue')
```

  8. Now let’s see what would have happened to the population if it had been culled. We’ll do three projections for three different cull sizes to help managers determine what they want to do. Project the population forward from 1980 using the population growth rates from problem 5. Use conditional statements so that in the final ten years of the projection 
a) 25, b) 50, and c) 100 sea lions are removed each year. Subtract culled individuals from the population at the end of the year, after births and natural mortality have occurred. Use the varying population growth rates from problem 5. (4 points) (1 point extra credit: instead of making three new population vectors, make a matrix with three columns, each representing the trajectory for a different cull scenario.)

```{r Question8, echo=TRUE, include=TRUE, warning = FALSE}
knitr::opts_chunk$set(echo = TRUE)
Scenario_A<- (nyrs+1)
Scenario_A[1]<- 1000
for(t in 1: nyrs){Scenario_A[t+1]<- lambda_t[t]*Scenario_A[t]
if(years[t]>2009) {Scenario_A[t+1] <- (lambda_t[t]*Scenario_A[t])-25} }

Scenario_B<- (nyrs+1)
Scenario_B[1]<- 1000
for(t in 1: nyrs){Scenario_B[t+1]<- lambda_t[t]*Scenario_B[t]
if(years[t]>2009) {Scenario_B[t+1] <- (lambda_t[t]*Scenario_B[t])-50} }

Scenario_C<- (nyrs+1)
Scenario_C[1]<- 1000
for(t in 1: nyrs){Scenario_C[t+1]<- lambda_t[t]*Scenario_C[t]
if(years[t]>2009) {Scenario_C[t+1] <- (lambda_t[t]*Scenario_C[t])-100} }

pinniped_matrix <- matrix(c(Scenario_A, Scenario_B, Scenario_C, N_new))

pinniped_matrix <- cbind(Scenario_A, Scenario_B, Scenario_C, N_new)
rownames(pinniped_matrix)<- 1980:2020
pinniped_matrix
```

  9. Plot the unculled trajectory first from problem 6, and then add the three culled trajectories from problem 8 using "lines()". All four lines should be different colors. Label the axes. (1 point)
```{r Question9, echo=TRUE, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
plot(x = years, y = N_new, type = 'l', xlab = 'Time', ylab = 'Population Size', col = 'blue', lty = 1)
lines(x = years, y = Scenario_A, col = 'seagreen', lty = 2)
lines(x = years, y = Scenario_B, col = 'purple', lty = 3)
lines(x = years, y = Scenario_C, col = 'red', lty = 4)
legend(1980, 4000, legend = c('Without cull', 'Scenario A', 'Scenario B', 'Scenario C'), col = c('blue', 'seagreen', 'purple', 'red'), lty = 1:4)
```

  10. How did the sea lion population respond to the cull during positive environmental conditions? During poor environmental conditions? What would you tell managers? (2 points)
  
In the years with positive environmental conditions all scenarios saw population growth, albeit slower then the original scenario that had no culling and the larger the cull the lower the population growth rate. During the years with poor environmental conditions only scenario A saw population growth; scenario B and C saw decreases in their populations, with scenario C decreasing at a drastic rate. I would advise managers to either stick strictly to scenario A and only cull 25 individuals, that way they can operate without concern for adjusting to the PDO. Otherwise they could use any of the cull rates, and decrease their chosen cull rate when the PDO decreases. I would advise though against culling over 100 individuals per year even in good years.

Nothing code related, just a sentence unfortunately.

I have TinyTeX installed and I can render you document to PDF without issue.
This means there is something off with your MikTeX installation. You should look into this part.
Either update it or maybe switch to TinyTeX if you are mainly using LateX with R Markdown.

This worked perfectly, thank you so much for the help!!

1 Like

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.