Jean V. Adams
Jean V. Adams

Reputation: 4784

Insert section symbol in R markdown document

How can I insert a section symbol in an R markdown document?

Since I don't know how to insert the symbol into this question, I will refer you to this page instead, https://en.wikipedia.org/wiki/Section_sign.

I am able to incorporate other symbols using Latex, for example $\dag$ gives me a dagger and $\ddag$ gives me a double dagger. (Both the dagger and the double dagger can be viewed at the page link above.) But if I use $\s$ or $\S$ (which I found on an internet search) for the section symbol, I simply get $\s$ in $\S$ in my document.

Upvotes: 5

Views: 4404

Answers (2)

James Hirschorn
James Hirschorn

Reputation: 7994

Extending @GregorThomas's solution to one that works for both pdf and html output:

```{r setup, include=FALSE}
S_symbol <- 
  if (knitr::is_html_output()) {
    "&sect;"
  } else {
    "\\S"
  }
```
`r S_symbol` this is the section symbol.

Upvotes: 0

Gregor Thomas
Gregor Thomas

Reputation: 145775

The section symbol isn't a math symbol, so it doesn't need to be in math mode (inside dollar signs). If you just use \S, you will get it.

Minimal example:

---
title: "Section Symbol"
author: "Gregor"
date: "November 16, 2015"
output: pdf_document
---

\S this is the section symbol.

enter image description here

If you need to use it inside math mode, you can enclose it in \text{}, for example this will produce a display equation, Gamma = Section Symbol (feel free to use $$ instead of \[):

\[
    \Gamma = \text{\S}
\]

Of course, special code in markdown depends on your destination format. The above will work if you're compiling to PDF via LaTeX. If you destination format is HTML, then according to the Wikipedia link in the question, the code is &sect;, and this works for me:

---
title: "Section Symbol"
author: "Gregor"
date: "November 16, 2015"
output: html_document
---

&sect; this is the section symbol.

Notice that the output is now html_document. This worked for Word as well.

Though less easy to remember / more manual, you can also insert the Unicode section symbol character straight into your Rmd document. You can probably copy/paste the symbol out of your Wikipedia link, or follow the "Typing Character" instructions from the Wikipedia link, such as Alt+0167 on Windows.

Upvotes: 11

Related Questions