---
title: "esc"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
A. An amount between $5 and $10.
B. An amount between \$5 and \$10.
C. An amount between \\$5 and \\$10.
X. An equation $1 and 1 = 2$.
Y. An equation \$1 and 1 = 2\$.
Z. An equation \\$1 and 1 = 2\\$.
I've got a similar, but different question here. Please comment if I need to add clarification to differences. I'm asking two different questions and hope that's obvious.
Anyways, the output of knitting the .Rmd above is shown below. Why does example X properly invoke LaTeX, where example A does not? X and A seem almost identical to me, and I'm left wondering why they both don't transform whatever is inside the dollar signs $ into LaTeX text?
I'm aware I could use \( and \) to replace $ and $. I just want to know why $ and $ doesn't properly call LaTeX in my example. It seems arbitray that it works for X, but not A, although I doubt it's arbitrary.
Pandoc's manual, under the Pandoc's Markdown > Math section, probably explains it best:
Anything between two $ characters will be treated as TeX math. The opening $ must have a non-space character immediately to its right, while the closing $ must have a non-space character immediately to its left, and must not be followed immediately by a digit. Thus, $20,000 and $30,000 won’t parse as math. If for some reason you need to enclose text in literal $ characters, backslash-escape them and they won’t be treated as math delimiters.
Related
I am using Xaringan slides. I want to write dollar values like $10-$5=$5 using math notation.
My code is the following:
$ \$10-\$5 = \$5 $.
However, the code will not generate the outcome in mathematical way as I wanted.
I know it works with double dollar sign $$, but I want to the result stay on the same line.
Also I don't want to put USD or CAD instead of $. Any tip?
Instead of using \$ just define a macro using latex syntax \newcommand
---
output: xaringan::moon_reader
---
# Defining inline math expression containing currency dollar sign
<!---- This is a macro for Mathjax ----->
$$\newcommand\dollars{\$}$$
<!---- --------------------------- ----->
This line conttain inline expression $\dollars10 - \dollars5 = \dollars5$
which looks like,
When knitting my R markdown file to pdf the following error message occurs:
! Package inputenc Error: Unicode character − (U+2212)
(inputenc) not set up for use with LaTeX.
markdown
I know it has to do with the MINUS sign I'm using in some formulas, but I can't solve the problem.
I have already set the Typset LaTeX into pdf.
The formulas in question are:
\hat{\beta_1} = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n} (x_i - \bar{x})^2}
and
\hat{\beta_0}=\bar{y} - \hat{\beta_1}\bar{x}
Another method to get around this error is to search and replace the offending unicode character, which can result from copy/pasting code or text into R.
For your situation, the offending symbol: − (en dash) should be replaced with - (hyphen).
Your latex can't process the en dash. It's subtle, but these characters have different widths. There are three types of horizontal punctuation “lines”:
Hyphen (-), used to hyphenate compound words and simple compound adjective (hence its name) and often used as the minus sign (-) in math
En dash (–), used to mark ranges in numbers, dates, scores and for complex compound adjectives
Em dash (—), used to separate extra information or mark a break in a sentence and other niche writing situations
This solves the problem.
title: "Your Title here"
output:
pdf_document:
latex_engine: xelatex
A little late but for anyone else who has the same problem:
Use \- instead of -
Yihui Xie, the creator of knitr, writes in the official knitr chunk option documentation (emphasis by me):
(...) in theory, the chunk label should be quoted as well, but for the sake of convenience it will be automatically quoted if you did not quote it (e.g. ```{r, 2a} will become ```{r, '2a'})
As I understand this, the results of quoted and unquoted chunk labels should always be the same. Is this really true? Or might there be any (edge) cases where quoting vs. not quoting the chunk labels actually matters?
Especially, I'd like to know if there might be any differences in results if one adheres to the following recommendation also found in the knitr chunk option documentation:
(...) in general it is recommended to use alphabetic characters with words separated by - and avoid other characters (...)
The only edge case I can think of is when your chunk label contains a comma, e.g., a,b. In this case, it has to be quoted as 'a,b', otherwise a will be treated as the chunk label.
Chunk labels are automatically quoted via the internal function knitr:::quote_label(). You may try to find out other possible edge cases by yourself:
> knitr:::quote_label("a")
[1] "'a'"
> knitr:::quote_label("a,b")
[1] "'a',b"
> knitr:::quote_label('"a,b"')
[1] "\"a,b\""
> knitr:::quote_label("a a a,b=1")
[1] "'a a a',b=1"
> knitr:::quote_label("a},b=1")
[1] "'a}',b=1"
When editing an Sweave document in LaTeX (using the Noweb mode), Emacs knows to "ignore" code that is in <<>>= blocks. However, for interstitial \Sexpr{} blocks, this isn't the case. Given that R references by columns via '$' and LaTeX uses $ to set-off equations, these \Sexpr{} blocks often break the syntax highlighting, like so:
I have a very rudimentary understanding the elisp & Emacs syntax highlighting, but my hope is that it might be possible to add something to .emacs that will disable any parsing/$ detection within \Sexpr{}'s.
I thought emacs with ESS has correct syntax highlighting for Sweave?
Anyway, the easiest "fix" is to just not use the $ operator but [[ instead. For example:
foo$p.value
foo[['p.value']]
Should give the same result. I think foo$p.value is just short for foo[["p.value",exact=FALSE]]
I don't have a fix either, but I'll pass along my workaround, which is to never (well, rarely) do any processing in \Sexpr chunks but instead to store things I want to use in \Sexpr in variables, and to do so in the same chunk I do the main calculations in.
<<echo=FALSE, results=hide>>=
t1 <- chisq.test(someVar)
p1 <- formatC(t1$p.value, format="%f", digits=2)
#
\dots with a $p$-value of \Sexpr{p1}.
While there are some downsides to this, I find it helps me to better keep track of what I want to present, and how I want to present it.
As an aside, consider using formatC instead of round as it can keep significant zeros (ie, 0.10 instead of 0.1).
I have no good answer for you as I am not an Emacs hacker, so I usually do one of two things:
Either add a simple % $ comment at the of the line to "close" the math expression from $ to $,
Or rewrite the expression to not use $-based subsetting:
round(as.numeric(chisq.test(someVar)["p.value"]), 2).
When editing an Sweave document in LaTeX (using the Noweb mode), Emacs knows to "ignore" code that is in <<>>= blocks. However, for interstitial \Sexpr{} blocks, this isn't the case. Given that R references by columns via '$' and LaTeX uses $ to set-off equations, these \Sexpr{} blocks often break the syntax highlighting, like so:
I have a very rudimentary understanding the elisp & Emacs syntax highlighting, but my hope is that it might be possible to add something to .emacs that will disable any parsing/$ detection within \Sexpr{}'s.
I thought emacs with ESS has correct syntax highlighting for Sweave?
Anyway, the easiest "fix" is to just not use the $ operator but [[ instead. For example:
foo$p.value
foo[['p.value']]
Should give the same result. I think foo$p.value is just short for foo[["p.value",exact=FALSE]]
I don't have a fix either, but I'll pass along my workaround, which is to never (well, rarely) do any processing in \Sexpr chunks but instead to store things I want to use in \Sexpr in variables, and to do so in the same chunk I do the main calculations in.
<<echo=FALSE, results=hide>>=
t1 <- chisq.test(someVar)
p1 <- formatC(t1$p.value, format="%f", digits=2)
#
\dots with a $p$-value of \Sexpr{p1}.
While there are some downsides to this, I find it helps me to better keep track of what I want to present, and how I want to present it.
As an aside, consider using formatC instead of round as it can keep significant zeros (ie, 0.10 instead of 0.1).
I have no good answer for you as I am not an Emacs hacker, so I usually do one of two things:
Either add a simple % $ comment at the of the line to "close" the math expression from $ to $,
Or rewrite the expression to not use $-based subsetting:
round(as.numeric(chisq.test(someVar)["p.value"]), 2).