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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing spaces within examples in Sec. 2.2.1 #545

Closed
notZaki opened this issue Mar 8, 2018 · 3 comments
Closed

Trailing spaces within examples in Sec. 2.2.1 #545

notZaki opened this issue Mar 8, 2018 · 3 comments

Comments

@notZaki
Copy link
Contributor

notZaki commented Mar 8, 2018

Hi.

I was wondering why there are trailing spaces in the sample code in Section 2.2.1 of the book - specifically, the space after \begin{equation} and \end{equation}.
It seems like they were deliberately added but I couldn't decipher the commit message.

The reason this came up is because the sample code fails to run using bookdown::word_document2 - a document is produced, but the equations do not show up (not an issue for html or pdf output).
This is a quirk of Word output - there can't be anything after ...{equation} as mentioned in #190 - and I was able to get the equation to show by removing the trailing spaces.
But I'm still left wondering why the trailing space was there in the first place.

Here's sample code with trailing spaces which doesn't render properly.

---
output:
  bookdown::word_document2
---

\begin{equation} 
  f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
  (\#eq:binom) 
\end{equation} 

Equation \@ref(eq:binom) is the probability mass function of the binomial distribution with size parameter $n$ and success probability $p$. 
@yihui
Copy link
Member

yihui commented Mar 8, 2018

Yes, there was a reason, but it is a little difficult to explain: jgm/pandoc#2758 For EPUB/Word output, bookdown has to wrap the equations inside $$ $$, because Pandoc does not support them out of the box like in other output formats such as LaTeX or HTML.

Then I have a problem: when I need to show verbatim examples of equations inside fenced code blocks, I need a way to tell which equations to wrap. The hidden rule in bookdown is that an equation must start with \begin{equation} and end with \end{equation} exactly:

bookdown/R/ebook.R

Lines 190 to 208 in 06bd619

# may add more LaTeX environments later
math_envs = c('equation', 'align', 'eqnarray', 'gather')
# wrap math environments in $$, otherwise they are discarded by Pandoc
# https://github.com/jgm/pandoc/issues/2758
protect_math_env = function(x) {
env = c(math_envs, paste0(math_envs, '*'))
s1 = sprintf('\\begin{%s}', env)
s2 = sprintf('\\end{%s}', env)
for (s in s1) {
i = x == s
x[i] = paste0('$$', x[i])
}
for (s in s2) {
i = x == s
x[i] = paste0(x[i], '$$')
}
x
}

I added trailing spaces to the examples so that they won't be wrapped in $$ $$ when converted to EPUB/Word. Of course, this is totally hackish, and you seem to be the first person who has noticed it.

In short, for most users, there must be no leading or trailing spaces around the equation environments.

@notZaki
Copy link
Contributor Author

notZaki commented Mar 9, 2018

Ok, so bookdown doesn't recognize the code block and wraps it in $$...$$ anyways.
That means if I render my above sample again, but have the equation in a fenced code block, then the word output displays the equation's code but with $$...$$ wrapping it.

I imagine someone will eventually run into the scenario where dollar signs mysterious start appearing in their verbatim code - hopefully they find this issue and it clears it up for them.

Thanks for the explanation, and for this package.

@notZaki notZaki closed this as completed Mar 9, 2018
@github-actions
Copy link

github-actions bot commented Nov 6, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants