yihui / xaringan

Presentation Ninja 幻灯忍者 · 写轮眼
https://slides.yihui.org/xaringan/
Other
1.49k stars 281 forks source link

Relative paths to dir above root? #29

Closed pat-s closed 7 years ago

pat-s commented 7 years ago

I placed my custom CSS file one directory above the .Rmd file. However, it is not recognized there.

output:
  xaringan::moon_reader:
    css: "../my-theme.css"

The same happens if I specify such a path for background-image in a layout slide

---
layout: true

background-image: url(../img/some-image.png) 
background-size: cover
---
yihui commented 7 years ago

This is a known limitation due to the fact that I start a local web server using the directory of the Rmd file as the root directory, and you cannot go to upper directories of a web server. I'll see if I can come up with a fix.

pat-s commented 7 years ago

Would be great! This would avoid having to copy theme.css and all related images x times into subfolders of all presentations but create one shared top-level folder for the theme content. :blush:

yihui commented 7 years ago

Yep, I understand. I ran into this issue myself two weeks ago, but I didn't have time to consider a fix.

yihui commented 7 years ago

Done. All you need to do is set your working directory to the parent directory before clicking the Infinite Moon Reader addin in RStudio, or call xaringan::inf_mr('path/to/your.Rmd').

jvcasillas commented 7 years ago

Do you foresee making CSS files accesible without changing the working directory? For example, I am using xaringan to make slides for my class, which are inside a directory for the class website. The website has a site_libs directory where I keep all css. So I must I set the working directory to a folder that is rather out of the way, then use the path back to the .Rmd file to render the slides, and, finally, change the working directory back to the project root. I can keep the css in a more convenient place as a work around, but it seems like having complete control of the path without changing the working directory would probably be ideal.

yihui commented 7 years ago

@jvcasill You don't have to change the working directory, but you will have to type something like xaringan::inf_mr(cast_from = '..') in the console instead of clicking the RStudio addin "Infinite Moon Reader". You have to make sure the directory cast_from is the common parent directory of your CSS and Rmd (it does not have to be the direct parent, e.g. can be two levels up ../..). The first argument of inf_mr() can be omitted when the Rmd is currently open in the RStudio source editor, otherwise you have to type the Rmd filename.

wjhopper commented 4 years ago

I know this is a very old issue, but I just ran into it myself trying to integrate some xaringan slides into a jekyll site that already has an assets directory for css.

I understand that if you're using the "Infinite Moon Reader" live preview functionality, you should use the cast_from argument to set the root of the web server. But, is there any way around it when using rmarkdown::render() or the knit button in RStudio (besides change the working directory, which opens its own can of worms for knitting)? Or does the knit button rely on the webserver as well?

tcgriffith commented 4 years ago

I know this is a very old issue, but I just ran into it myself trying to integrate some xaringan slides into a jekyll site that already has an assets directory for css.

I understand that if you're using the "Infinite Moon Reader" live preview functionality, you should use the cast_from argument to set the root of the web server. But, is there any way around it when using rmarkdown::render() or the knit button in RStudio (besides change the working directory, which opens its own can of worms for knitting)? Or does the knit button rely on the webserver as well?

My understanding is that you can set the lib_dir: assets in the yaml header then you are good to go

full header:

---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
institute: "RStudio, Inc."
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
  xaringan::moon_reader:
    lib_dir: assets
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
---

Then knit in Rstudio would result in the following structure

.
├── assets
│   ├── crosstalk
│   ├── datatables-binding
│   ├── datatables-css
│   ├── dt-core
│   ├── htmlwidgets
│   ├── jquery
│   ├── leaflet
│   ├── leaflet-binding
│   ├── leafletfix
│   ├── Proj4Leaflet
│   ├── remark-css
│   └── rstudio_leaflet
├── testit_files
│   └── figure-html
├── testit.html
└── testit.Rmd

Here is my example repo for xaringan+jekyll+ghpages Result

wjhopper commented 4 years ago

Thanks for sharing @tcgriffith! But, I don't think this will work for me specifically. In your example, the xaringan slide deck testit.Rmd file is at the root (where the assets folder also is), whereas mine is in a subfolder like so:

├── _config.yml
├── index.md
├── _labs
│   └── Lab 01
│       └── Introduction.Rmd ## Xaringan slides here
├── assets
│   └──css
│       └── custom.css

If I just put lib_dir: assets, knitting just creates a new directory named assets in the same sub directory as the slides, like so:

├── _config.yml
├── index.md
├── _labs
│   └── Lab 01
│       └── Introduction.Rmd
│       └── Introduction.html
│       └── assets                              ##  <- just adds this
│              └── remark-css-0.0.1
│                     └── default.css
│                     └── default-fonts.css
├── assets
│   └── css
│       └── custom.css

So, I think it comes back to the core issue of accessing files above the "root", where "root" is the directory where your .Rmd is stored.

daijiang commented 3 years ago

Did rmarkdown::render() reverted to the old behavior recently? At March 2021, I still can refer ../style.css and render it without problem (the generated html file used relative path). But today, I render the xaringan slides again (without changing anything, just deleted the html and try to rebuild), the generated html file used absolute path, which can be a problem when deploying online. Just report here; and sorry for not providing a reproducible example.

yihui commented 3 years ago

@daijiang That sounds like a bug. It'll be great to file a new issue with a minimal reproducible example (you can upload a zip file containing the Rmd and css). Thanks!

daijiang commented 3 years ago

Here is an example. Note that no matter which way to generate the html (make at the root or use the knit button of RStudio), the line 10 of 01_about/presentation.html has the absolute path there. Thanks @yihui example.zip

cderv commented 3 years ago

@daijiang I created a new issue to follow that in its own thread. That is better to do so that just discussing in a closed one.