How to write equation including `abs` inside, using Rmarkdown?

10,582

Solution 1

Expanding on Andrie's answer:

Another way to write absolute value is \lvert -3 \rvert

To automatically adjust the size of the vertical lines to what's inside them, use

\left\lvert -3 \right\rvert

In your case, this becomes:

$$MAPE = \frac{1}{n} \sum_{d_i} (\frac{1}{q} \sum_{t_j}\left\lvert{\frac{gap_{i,j}-s_{i,j}}{gap_{i,j}}}\right\rvert)$$

Image of Output

edit: When I originally posted this answer, I couldn't embed images yet. Now replaced link with embed.

Solution 2

Using mathjax, you can use the "pipe" symbol |to indicate absolute values.

Try this:

$$ |-3| $$

enter image description here

In your case:

$$MAPE = \frac{1}{n} \sum_{d_i} (\frac{1}{q} \sum_{t_j} |\frac{gap_{i,j}-s_{i,j}}{gap_{i,j}}|  )$$ 

This gives:

enter image description here

Share:
10,582
Daniel
Author by

Daniel

Updated on June 13, 2022

Comments

  • Daniel
    Daniel almost 2 years

    I have managed to write the whole equation Equation I want to write

    using the following code in Rmarkdown

    $$MAPE = \frac{1}{n} \sum_{d_i} (\frac{1}{q} \sum_{t_j} 
    \abs{\frac{gap_{i,j}-s_{i,j}}{gap_{i,j}}}  )$$ 
    

    However, in Rmarkdown, the code above returns:

    Rmarkdown output

    Could anyone help figure out how to get abs right here? Thanks

  • Bob__
    Bob__ almost 8 years
    I'd use \left( and \right) too, instead of ( ... ).
  • hdkrgr
    hdkrgr almost 8 years
    Thanks for the addition. Yes, that's how I would do it, too. I just wanted to reduce the changes in the original code to a minimum here.
  • Daniel
    Daniel almost 8 years
    Thanks for this straightforward solution!
  • Daniel
    Daniel almost 8 years
    This solution has more to write but prettier. Thanks