Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaTeX Error: Environment rmdtip undefined. #1

Closed
cpsievert opened this issue Dec 18, 2018 · 8 comments
Closed

LaTeX Error: Environment rmdtip undefined. #1

cpsievert opened this issue Dec 18, 2018 · 8 comments

Comments

@cpsievert
Copy link

@cpsievert cpsievert commented Dec 18, 2018

First off, thanks so much for this template! It's going to save me so much time and agony 馃嵒.

Have you given any thought to supporting custom blocks in the pdf version? Currently if I try to add something like this to an Rmd:

```{block, type = "rmdtip"}
Bookdown rocks
```

I get

LaTeX Error: Environment rmdtip undefined.
@yihui

This comment has been minimized.

Copy link
Owner

@yihui yihui commented Dec 19, 2018

I used these environments in the bookdown book. The relevant LaTeX code is here: https://github.com/rstudio/bookdown/blob/7a0364b7ffd16b360b7abde36a29a8801675906c/inst/examples/latex/preamble.tex#L58-L88

The icons are here: https://github.com/rstudio/bookdown/tree/master/inst/examples/images

The HTML versions are defined here: https://github.com/rstudio/bookdown/blob/7a0364b7ffd16b360b7abde36a29a8801675906c/inst/examples/css/style.css#L1-L20

And of course, the documentation: https://bookdown.org/yihui/bookdown/custom-blocks.html

Personally I don't quite like the syntax I invented. I wish someone could figure out how to support fenced divs for LaTeX output: http://pandoc.org/MANUAL.html#divs-and-spans i.e. convert ::: foo to \begin{foo}. I'm sure @RLesur has the capability since he is familiar with Pandoc filters :)

@RLesur

This comment has been minimized.

Copy link

@RLesur RLesur commented Dec 19, 2018

It looks to be feasible. BTW, there is a Pandoc python filter that does a very similar job https://github.com/chdemko/pandoc-latex-environment
I think that the most difficult part is to write the specs.
I'm not sure to have fully understood what you need.

  • Do you want to have a syntax that would be compatible with html output format? I guess your answer is positive.

  • Do you want to create an environment for all divs? For instance:

    ::: {.foo}
    :::

    would give (for any div in the document, maybe risky?)

    \begin{foo}
    \end{foo}

    Or do you prefer something like this:

    ::: {.block data-type="foo"}
    :::

    would give (only for divs of class block)

    \begin{foo}
    \end{foo}

    You also may prefer an API like the above mentioned python filter.

  • Would you need to pass options to the LaTeX environments?

Since fenced divs is a Pandoc 2 feature, the filter could be written in lua (advantage: no system dependency).

@yihui

This comment has been minimized.

Copy link
Owner

@yihui yihui commented Dec 20, 2018

Very good questions. I agree that the spec requires more thoughts than the actual implementation. I was thinking of something like

::: {.foo data-latex="latex,options,bar=zzz"}
:::

which is converted to

\begin[latex,options,bar=zzz]{foo}
\end{foo}

where the data-latex attribute is optional.

@RLesur

This comment has been minimized.

Copy link

@RLesur RLesur commented Dec 20, 2018

OK. That's clear.
Just another question: what would you want for multiple classes div?

::: {.foo .baz data-latex="latex,options,bar=zzz"}
:::

I guess you just want the first class (I ask to be sure).

@yihui

This comment has been minimized.

Copy link
Owner

@yihui yihui commented Dec 20, 2018

Yes, that's exactly what I'd expect.

@RLesur

This comment has been minimized.

Copy link

@RLesur RLesur commented Dec 21, 2018

So, here is the Pandoc lua filter.

--[[
     A Pandoc 2 lua filter converting Pandoc native divs to LaTeX environments
     Author: Romain Lesur
     License: Public domain
--]]
local pandocList = require 'pandoc.List'

Div = function (div)
  -- if the output format is not latex, the object is left unchanged
  if FORMAT ~= 'latex' then return nil end
  
  local env = div.classes[1]
  -- if the div has no class, the object is left unchanged
  if not env then return nil end

  local options = div.attributes['data-latex']
  local content = div.content

  -- build the options character string
  local optionsString
  if options then
    optionsString = '[' .. options .. ']'
  else
    optionsString = ''
  end
  
  -- build the returned list of blocks
  local beginEnv = pandocList:new{pandoc.RawBlock('tex', '\\begin' .. optionsString .. '{' .. env .. '}')}
  local endEnv = pandocList:new{pandoc.RawBlock('tex', '\\end{' .. env .. '}')}
  local returnedList = beginEnv .. content .. endEnv    

  return returnedList
end

Be aware that this filter will transform fenced divs but also html raw divs (since the native_divs extension is enabled by default):

<div class="foo baz" data-latex="latex,options,bar=zzz">
Here is a paragraph.

And another.
</div>

is converted to

\begin[latex,options,bar=zzz]{foo}

Here is a paragraph.

And another.

\end{foo}

This is convenient but could have some side effects.

@yihui

This comment has been minimized.

Copy link
Owner

@yihui yihui commented Dec 21, 2018

Excellent!! I'll include this in a future version of bookdown (ideally after the stable release of RStudio 1.2, so most people have access to Pandoc 2). Many thanks!

@yihui

This comment has been minimized.

Copy link
Owner

@yihui yihui commented Sep 20, 2019

I just incorporated it into the rmarkdown package. Note that originally I made a mistake in my example. The LaTeX environment syntax should be \begin{foo}[latex,options,bar=zzz] instead of \begin[latex,options,bar=zzz]{foo}, and sometimes it could also be \begin{foo}[latex,options,bar=zzz]{more options}, so I decided not to add the square brackets automatically and let users provide the full argument string after the environment.

Another minor change I made was to drop the data-latex attribute when the output format is not LaTeX: rstudio/rmarkdown@cecd812#diff-2406dc32c250628c6ff0162c391a7a20R14 since this attribute is unlikely to be useful to other formats such as HTML (hence the HTML code will be a little cleaner).

Many thanks to @RLesur for the lua filter! Finally I can sleep well---I really disliked the block and block2 hacks I invented originally: https://bookdown.org/yihui/bookdown/custom-blocks.html This new lua filter makes custom blocks much more powerful since you can include anything in the div (even R code blocks).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can鈥檛 perform that action at this time.