0

Dont know if SO is the right platform for this question since I dont get an error with what I am doing, more an unexpected behavior...

But what am I doing? I am knitting the default template of the R-Studio markdown file (file > new file > r-markdown). The "code" of the file is at the end of the post since it is rather large... When I knit that file, I get a PDF file.

This file works as expected: When I click one of the two bookmarks ("R-Markdown" or "Including Plots") in the bookmark-pane of the R-Studio internal viewer, pdf.js/firefox or Adobe Acrobat DC both scroll/move to the matching headline.

When I add classoption: landscape to the YAML header the internal viewer and pdf.js/firefox still work as expected, but clicking the bookmarks in Adobe Acrobat DC has no effect. Well, thats not correct. Clicking any bookmark when you're on the first page has no effect, but if you scroll down to the second page or further, clicking a bookmark moves you up to the first page.

Just by changing the page orientation? What am I doing wrong? Hopefully its not Adobe Acrobat since I cant change the viewer.

---
title: "Untitled"
author: "test"
output: pdf_document
---

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

EDIT

Since we're able to fix the simplified scenario I've added parts of the real scenario that still contain the problem. As you can see it nearly doesn't contain markdown and more tex because things like headers are not part of markdown. Therefore I've checked which tex output is generated for a level 2 markdown section like

## page 9 3/4

Answer:

\hypertarget{page-9-34}{%
\subsection{page 9 3/4}\label{page-9-34}}

and used that tex code in the header to create bookmarks that redirect to the specific page. But neither markdown bookmarks nor the tex variant works.

test.r

# Should contain a file "logo.png" which is used on title page and in page header
img <- "path/to/image/folder"
path <- "path/to/output/folder"
fname <- "report1.pdf"

infodata <- tribble(~pid, ~person, ~name, ~date, ~m1)
infodata <- infodata %>% add_row(
  pid = 4711,
  person = "Max Mustermann",
  name = "Hotel California",
  date = lubridate::ymd('2021-09-30'),
  m1 = lubridate::ymd('2021-10-15')
)

rmarkdown::render(input = "test.Rmd",
                  output_format = "pdf_document",
                  output_file = fname,
                  output_dir = path,
                  params = list(info = infodata, img = img))

test.Rmd

---
title:  |
  ![](`r img`/logo.png){width=15cm}  
  `r params$info$name`
subtitle: "1. Report (`r format(params$info$date, '%d.%m.%Y')` - `r format(params$info$m1, '%d.%m.%Y')`)"
author: "Your-Name-Here"

mainfont: Calibri
fontsize: 12pt
papersize: a4
classoption: landscape
geometry: "left=1cm,right=1cm,top=1cm,bottom=1cm"

output:
  pdf_document:
    latex_engine: xelatex # using fonts from local environment
    toc: false
    number_sections: false
    fig_caption: false
  
params:
  info:
  img:

header-includes:
  \usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \setlength{\headheight}{80pt}
    \rhead{\includegraphics[width=2cm]{`r img`/logo.png}}
    \fancypagestyle{plain}{\pagestyle{fancy}}
  \usepackage{xcolor}
  \usepackage{pagecolor}
    \definecolor{color1}{HTML}{e7FFFF}
    \pagecolor{color1}
  \usepackage{titlesec}
    \titlespacing{\section}{0pt}{0pt}{-0.5cm}
  \usepackage{hyperref}  
    \hypersetup{bookmarks=true,bookmarksopen=true,pdfcreator=} 
---

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



\thispagestyle{empty}
\newpage



\lhead{\hypertarget{page-1}{\subsection{Page 1}\label{page-1}}}
\begin{huge}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{huge}
\newpage



\lhead{Page 2}
## Page 2
\begin{huge}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{huge}
\newpage



\lhead{\hypertarget{page-3}{\subsection{Page 3}\label{page-3}}}
\begin{huge}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{huge}
\newpage



\lhead{Page 4}
## Page 4
\begin{huge}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{huge}
\newpage
Michael
  • 1,931
  • 2
  • 8
  • 22

1 Answers1

0

I know an easy solution: add the table of contents.

///
output: 
  pdf_document:
    toc: true
 ///

Tested in Acrobat DC 2021.007.20099

If you want to use a LaTeX solution - see there. But exists one problem with this solution: "page numbers print sideways on the left side of the landscape pages".


New addition

---
title: "Landscape PDF from rmarkdown"
author: "Michael von Deutschland"
date: "`r Sys.Date()`"
output: pdf_document
classoption: landscape
---
## bookmark 1
When converting R Markdown to PDF, we can use [a lot of variables] 
(http://johnmacfarlane.net/pandoc/README.html) for the LaTeX template. 
This short example shows the `classoption` variable that takes the value 
`landscape` to produce PDF in the landscape orientation.


 ```{r}
 str(cars)
 summary(lm(dist~speed, cars))
 ```

 ## bookmark 2
 Please read the documentation of Pandoc for other possible variables.

 \newpage 

 ## bookmark3
 Have you tried not using csas_table? You can also put just plain old 
 LaTeX in the .Rmd document. It won't render in the Word document but it 
 will in the PDF. If you wanted to get fancy you could use an if else 
 statement to render one way with TeX and the other way with .docx.

 \newpage

 ## bookmark 4

 Dont want a table of contents (or I need to find a way to put the toc 
 on a separate page after the title page), but tested anyway. Sadly 
 doesn't work for me (same behavior as described in my post: clicking 
 toc-entry or bookmark doesnt has any effect, only when viewing page 2 
 it jumps back to the top) :( Have the same acrobat version, running r 
 studio v1.4.1717 with r4.0.5, markdown 1.1 and knitr 1.33

 \newpage

 ## bookmark5

 ```{r pressure, echo=FALSE}
 plot(pressure)
 ```

Dharman
  • 30,962
  • 25
  • 85
  • 135
manro
  • 3,529
  • 2
  • 9
  • 22
  • Dont want a table of contents (or I need to find a way to put the toc on a separate page after the title page), but tested anyway. Sadly doesn't work for me (same behavior as described in my post: clicking toc-entry or bookmark doesnt has any effect, only when viewing page 2 it jumps back to the top) :( Have the same acrobat version, running r studio v1.4.1717 with r4.0.5, markdown 1.1 and knitr 1.33 – Michael Oct 19 '21 at 07:28
  • @Michael Try a new answer, i have rstudio 1.4.1106, r4.10, same markdown and knitr 1.4. Now it works nice without ```toc``` – manro Oct 19 '21 at 09:17
  • Yes, your example works for me too :) The only notable difference I see is the `\newpage`...? When I add it to the r-studio included example, it works there too. But in my real scenario I am already using `\newpage` and it doesn't work :( Any ideas? – Michael Oct 20 '21 at 06:10
  • @Michael Give an example from your real scenario. – manro Oct 20 '21 at 06:31
  • sorry for the delay, jumping between multiple projects :( I've updated my initial post with parts of the real scenario :) – Michael Oct 21 '21 at 08:45
  • @Michael So. It work nice for me (I deleted strings with ```img's```). Bookmarks work. Try to change latex-engine to "pdflatex". With ```latex_engine: xelatex``` i even can't render, although xe/lua-LaTeX engines exist. – manro Oct 21 '21 at 09:09
  • Changed the latex engine to `pdflatex`, bigger file size (which isn't a problem) but the problem still exists: viewing the file in adobe dc, bookmarks have no effect (besides that every bookmark scrolls to top), viewing file in firefox/chrome all bookmarks lead to the page 1 anchor. Updated to r-studio 2021.09.0351, r 4.1.1 and knitr 1.3.6 (can't find anything about knitr 1.4), but still no success... I start thinking its my system - but what could have an effect on adobe, firefox and chrome? – Michael Oct 21 '21 at 10:46
  • @Michael Hmm, dont know. Try to read this pdf file in ```foxit``` f.e.? – manro Oct 21 '21 at 10:56
  • Tried foxit reader 11.0.1 portable - same behavior as chrome and firefox: all bookmarks lead to page 1... – Michael Oct 21 '21 at 12:47
  • @Michael What a curse? Do you have an another PC with RStudio to test your code? or virtual machine...? – manro Oct 21 '21 at 13:10