0

enter image description here

How can I write the above formula to then knit it as pdf please? I am very new and I am still learning the basics.

This is what I tried writing:

enter image description here

\begin{center}
$\nabla ~Y$ $f$(**Y**) = $\nabla$ (**Y**^**v** + $$a$$) =
$\nabla ~Y$ (**Y**^**v**) + $\nabla$ ($$a$$) = **v** + **0**
= **v**
\end{center}

i tried something else before which gave me this output: enter image description here

However, I want the formatting to be as in the first imagine, neat and centred. Any help would be appreciated, thank you!

enkorvaks
  • 487
  • 2
    The \begin{center} and \end{center} you have are LaTeX commands, and don't have any special meaning in R-markdown. Unless you are running your R-markdown through LaTeX (as opposed to the basic knit in R-studio), what you are getting is what is expected. There are several options for LaTeX within R-studio, the most basic being (I think) TinyTeX. You can also download TeXLive, and use that, but it may be more than you need, if you are only doing a few reports. – enkorvaks Jun 20 '23 at 04:48
  • 1
    Double $$ should be avoided and will insert a new block which breaks things here. You should try single $, so $a$ instead of $$a$$ (two times). I also have the feeling that mixing mardown with TeX-flavoured syntax is not optimal. Try using \mathbf{Y} instead of **Y** (but I don't know if it works in your set up). – Jasper Habicht Jun 20 '23 at 06:28

2 Answers2

1

Although Rmarkdown allow to include LaTeX environments and commands, and this should work as expected:

---
output:
  pdf_document: default
---
  1. foo with a \LaTeX\ command.
  2. baz:

\begin{center} bar \end{center}

Sometimes happens that the export to LaTeX is unable to recognize the environment, and it is managed as markdown text.

Solution 1: Mark the LaTeX chunk explicitly as LaTeX or TeX code:

---
output:
  pdf_document: default
---
  1. foo with a \LaTeX\ command.
  2. baz:
\begin{center}
bar
\end{center}

Note that it should be =latex or =tex with the equal sign before: Otherwise the block just will show the code with syntax highlight.

This solution have the advantage that you can pass any chunk of LaTeX, not just one environment.

Solution 2: Make the LaTeX environment as a Pandoc's Div block:

---
output:
  pdf_document: default
---
  1. foo with a \LaTeX\ command
  2. baz:

::: {.center data-latex=""} bar :::

This have the advantage that the content environment could be markdowm syntax (you can type here *bar* instead of \emph{bar} to have cursives).

Note that you can also just insert only \begin{center} and somewhere later \end{center} with the solution 1, i.e, marking two chunks of LaTeX, to use markdown text inside, but in \begin{center} *foo* \end{center}, as is, there are no markdown text, only a word in LaTeX with two asteriks.

Fran
  • 80,769
0

You should not use $$ in general, especially not inside a formula. The use of this is strongly discouraged and it will start displayed math (that is, insert a new paragraph) what you don't want anyways.

Furthermore, you should probably just use one pair of $...$ which you place before and after the whole formula in order to preserve the correct spacing between the elements in math mode.

Finally, it seems that mixing Markdown syntax and TeX syntax is not working well here, so I would suggest that you use \mathbf{Y} instead of **Y** in the formula.

I don't know whether the syntax can be used in you set up, but in (La)TeX I would write (but it seems that the center environment is not supported in your set up, so maybe just omit it):

\begin{center}
$
\nabla_\mathbf{Y} f(\mathbf{Y}) = 
\nabla_\mathbf{Y} (\mathbf{Y}^\top \mathbf{v} + a) =
\nabla_\mathbf{Y} (\mathbf{Y}^\top \mathbf{v}) + \nabla_\mathbf{Y} (a) = 
\mathbf{v} + \mathbf{0} = 
\mathbf{v}
$
\end{center}

Or maybe:

\[
\nabla_\mathbf{Y} f(\mathbf{Y}) = 
\nabla_\mathbf{Y} (\mathbf{Y}^\top \mathbf{v} + a) =
\nabla_\mathbf{Y} (\mathbf{Y}^\top \mathbf{v}) + \nabla_\mathbf{Y} (a) = 
\mathbf{v} + \mathbf{0} = 
\mathbf{v}
\]

enter image description here

  • Thank you so much for taking the time to write this down. i'll take your suggestions into account for next time. the second code worked perfectly! thanks again – Rick The Sizzler Jun 21 '23 at 04:04