Bookdown not acknowledging (finding) css and tex definitions

Hi

I am trying to create a "book" using bookdown. I am finding it helpful look at the source of the bookdown book; very helpful.

But one thing I cannot figure: The new environments declared for that book (e.g. rmdtip; rmdwarning) in style.css and preamble.text simply are not acknowledged when I try it.

I have placed them in, respectively, the /.css and ./latex directories (as per the bookdown book, figuring that when I am learning I'll do what the Master (Yihui Xie) does...).

I have this in the _output.yml file, to point to these definitions (again, as the Master does):

bookdown::gitbook:
css: css/style.css

bookdown::pdf_book:
includes:
in_header: latex/preamble.tex

So, as far as I can tell, I am pointing to the correct files.

But the HTML render ignores the rmdtip environment (the text of what I place in the environment shows, but not in the new environment), and rendering the pdf throws an error:

! LaTeX Error: Environment rmdnote undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
...

l.187 \BeginKnitrBlock{rmdnote}

So it seems the new environment definitions are not being seen.

I must be pointing to the wrong thing somewhere, but I cannot see where, and I have tried to follow Yihui's code.

Any advice for a newbie?

Thanks.

BSP

Hi @BackseatPete! Are you able to build a complete reproducible example, or reprex, for this? It'll help us working out what's going wrong for you.

In particular, I'd love to see your YAML code put into a code block by fencing it with triple backticks. As it is, those options for css and includes look like they're right up against the start of their lines. As YAML relies on indentation to create its hierarchy of options, it's possible this is causing R Markdown to lose those options. This link demonstrates some ways of properly indenting options in YAML (the main rules are (a) do it, (b) be consistent, and (b) use spaces, not tabs):

https://docs.saltstack.com/en/latest/topics/troubleshooting/yaml_idiosyncrasies.html#indentation

Hope that helps! If not, try using our guide to building a reprex and we can start peeling this apart :slight_smile:

1 Like

Thanks. The spacing was an artifact of a copy-and-paste into here; yes, the spacing is correct, but I should have been more vigilant after pasting here. And thanks for the link, but it doesn't answer one perhaps-relevant question: are tabs (rather than spaces) OK for indenting?

I understand about MWE... but how does one create a MWE in this case? It requires numerous files, file structures, etc...?

BSP

  bookdown::gitbook:
    css: css/style.css
 bookdown::pdf_book:
    includes:
      in_header: latex/preamble.tex

Yep, boiling your problem down to a reprex is often the hardest part of solving it, and it isn't always completely possible. It is worth doing what you can, though: I've come to this site several times with a problem and then solved it myself in the process of creating the reprex! Sometimes it's helpful to start with some deliberately simple examples (eg. directly cloning someone else's bookdown example project and attempting to compile that) and introduce complexity from there.

Hard tabs are a hard no for YAML. They may not be the source of your problem—my understanding is that YAML processors should flag tabs for you, but I'm not super clear on it—but it's worth using a text editor like VS Code to flush hard tabs out and convert them to spaces if you can.

OK. Let's try this.

I have this file index.Rmd:

---
title: "Test"
author: "Back Seat Pete"
---

# Preface {-}

## About this book {-}

Some info.


```{block2, type="rmdnote"}
Meant to be a **note**.
```

It is that last rmdnote that is ignored.

Here is _output.yml (not sure what I should and should not remove here):

bookdown::gitbook:
  css: style.css
  split_by: section
bookdown::html_chapters:
  css: style.css
bookdown::pdf_book:
  includes:
    in_header: preamble.tex
  keep_tex: yes
  pandoc_args: --top-level-division=chapter
  toc_depth: 3
  toc_unnumbered: no
bookdown::epub_book:
  stylesheet: style.css

And _bookdown.yml:

output_dir: _book
clean: [packages.bib, bookdown.bbl]
delete_merged_file: true

The preamble.tex file is (in the same directory as the other files):

\newenvironment{rmdnote}
  {\begin{center}}
  {\end{center}}

When I run

render_book("index.html", output_format="pdf_book")

I get this error message:

! LaTeX Error: Environment rmdnote undefined.

Error: Failed to compile _main.tex. See _main.log for more info.

When I examine _main.text, there is no mention of preamble.tex anywhere, or a definition for the rmdnote environment. The code to include the rmdnote is there, but of course it throws an error if the definition for that environment isn't loaded.

(Indeed, if I make a syntax error in the preamble.tex file by not closing a bracket, there is no difference: _main.tex still fails to compile because no rmdnote environment is defined, not because of the missing bracket.)

This suggests to me (who knows little) that the issue is that the files aren't being found or used. Why is my preamble.tex file not being found?

(Similarly, but details not included to save space, the style.css with a rmdnote definition also is ignored when the html file is rendered.)

Thanks for any help.

BSP.

Thanks for adding some additional details @BackseatPete - I use preamble.tex and style.css to define rmdnote (among other things) in my course syllabi. Here is an example that may provide some reference to work off of. I do notice a few differences in our bookdown::pdf_book: syntax that might be worth taking a look at.

If you simplify your _output.yml to just the gitbook does it build successfully?

1 Like

I'll also add that you've got a nice description here @BackseatPete - is there any chance that your directory structure is on GitHub so we could look at it? Or can you create a reprex in a GitHub repo?

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