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

Decimal separator and $$ notation #348

Closed
cmmp opened this issue Jul 23, 2012 · 8 comments
Closed

Decimal separator and $$ notation #348

cmmp opened this issue Jul 23, 2012 · 8 comments
Milestone

Comments

@cmmp
Copy link
Contributor

cmmp commented Jul 23, 2012

Hi,

I'm using comma as a decimal separator: options(OutDec = ',').

I have this small formatting issue:

When I use \Sexpr{0.5} inline with text, the output 0,5 is nicely formatted.

However, if I have to use it inside a math expression, like $\alpha = \Sexpr{0.5}$, then the 0, 5 is output with an extra space after the comma. If I write $\alpha = \text{\Sexpr{0.5}}$ then it works ok.

is there another simpler (more elegant) workaround or a fix for this?

thanks!
Cássio

@yihui
Copy link
Owner

yihui commented Jul 23, 2012

You can hack at the inline hook to add \text{} to numbers, e.g.

knit_hooks$set(inline = function(x) {
  if (is.numeric(x)) x = sprintf('\\text{%s}', knitr:::format_sci(x, 'latex'))
  knitr:::.inline.hook(x)
})

but in the non-math mode that will be problematic. However, you can continue hacking at it, e.g. let I(0.5) be 0,5, and a normal 0.5 be \text{0,5}. Check the class of the number by inherits(x, 'AsIs') and decide if you need \text{} or not.

@cmmp
Copy link
Contributor Author

cmmp commented Jul 23, 2012

yeah, I'm not sure, maybe that's more fragile to corner cases than adding \text{} altogether. Reading this makes me think that maybe knitr could check if outdec is something other than '.', and then to use siunitx or numprint inside for numbers. But I'm not sure if this is too a corner case to handle in the package or not.

@yihui
Copy link
Owner

yihui commented Jul 24, 2012

You can design an inline hook first, and I may accept a pull request after it works well for you. Frankly speaking, I was not even aware of the OutDec option in the past before you mentioned it to me.

@cmmp
Copy link
Contributor Author

cmmp commented Jul 24, 2012

I have to think about this... what if x is an expression and not just a number? is the expression evaluated before the hook is called or after? e.g. \Sexpr{0.6*2} vs \Sexpr{1.2} or worse \Sexpr{paste('a = ', 1.2)}

@yihui
Copy link
Owner

yihui commented Jul 24, 2012

The expression is always evaluated before being passed to the hook.

@cmmp
Copy link
Contributor Author

cmmp commented Jul 24, 2012

Ok, I don't understand what you mean with

let I(0.5) be 0,5, and a normal 0.5 be \text{0,5}

and the need to check using inherits.

I also don't know why you said the hook you provided will be problematic outside a math environment. Could you clarify that?

I tried it and it works (in and out of math environments)... it adds \text{} always, but I could live with that.

Here's the example I created:

\documentclass[a4paper]{article}

\usepackage{amsmath}

\begin{document}

\title{Test}

\maketitle

<<>>=
options(OutDec = ',')
@

<<>>=
knit_hooks$set(inline = function(x) {
    if (is.numeric(x)) x = sprintf('\\text{%s}', knitr:::format_sci(x, 'latex'))
      knitr:::.inline.hook(x)
})
@ 

\section{Test}

This is the first test. abc \Sexpr{0.6} def

another test $a = \Sexpr{0.6}$.

\end{document}

at least for these simple cases it works.

An issue I see is that \text{} is provided by amsmath, so for this to work knitr would have to include that package. Not a problem by me, but maybe someone for some reason may not want to include that package.

@yihui
Copy link
Owner

yihui commented Jul 24, 2012

sorry I thought \text{} is for the math mode only like ^ and _

amsmath does not matter because you use it only when OutDec is not ., and I guess most R users do not use other characters, so this will not actually affect them at all

for I(0.5), I mean something like x = if (inherits(x, 'AsIs')) x else sprintf('\\text{%s}', x)

@yihui yihui closed this as completed in ceaa141 Jul 24, 2012
yihui added a commit that referenced this issue Jul 24, 2012
@github-actions
Copy link

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 10, 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