3

In Latex, it is common to reference a label using the "~" to keep the number attached to the label such as Figure or Table.

https://tex.stackexchange.com/questions/227285/problems-with-tilde-and-line-breaking/227296 shows an example of this.

A fully working example can be found here:

http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/

In the Rmd file, http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/reference-tilde.Rmd,

I have the following:

---
title: "Untitled"
output:
  pdf_document:
    keep_tex: true
    number_sections: true
    latex_engine: pdflatex
---


\section{My Section Name}
\label{sec:my}

Below you will find Equation~\ref{eq:my-model}.

\begin{equation}
\label{eq:my-model}
    Y_{jt} = \alpha + \beta X_{jt} + \upsilon_{j}  + \varepsilon_{jt} ,
\end{equation}


\subsection{My sub section}
\label{sec:mysub}

For more information, please refer to Section~\ref{sec:my}.

Notice the "~" tilde being used as outlined in basic Latex usage.

When I click Knit-PDF, it outputs something I did not expect:

http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/reference-tilde.pdf

Since I choose "keep_tex", I can view the .TEX file

http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/reference-tilde.tex which can be seen online here: http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/reference-tilde.tex.txt

It appears to have replaced the tilde with a text version:

\begin{document}
\maketitle

\section{My Section Name}
\label{sec:my}

Below you will find Equation\textasciitilde{}\ref{eq:my-model}.

\begin{equation}
\label{eq:my-model}
    Y_{jt} = \alpha + \beta X_{jt} + \upsilon_{j}  + \varepsilon_{jt} ,
\end{equation}

\subsection{My sub section}
\label{sec:mysub}

For more information, please refer to
Section\textasciitilde{}\ref{sec:my}.

\end{document}

How do I rectify this situation?

How do I get the Latex to render as expected?

How do I get my tilde as a spacer back?

Phil
  • 7,287
  • 3
  • 36
  • 66
mshaffer
  • 959
  • 1
  • 9
  • 19
  • I feel it is a good point to rise an issue on `knitr` github (look at `escape_latex` function in https://github.com/yihui/knitr/blob/master/R/utils.R). Maybe the `gsub` pattern for `~` should avoid something like `\S~\ ` (written as if \ was not a special character. Now I do not find how to look for a `\ ` in regex...) – iago Nov 05 '20 at 17:15
  • Now I found it. It should avoid at least `\\S~\\ref`, but also (https://tex.stackexchange.com/a/36924/8639) `\\S~\\d`. – iago Nov 05 '20 at 17:23
  • You may want to consider `\usepackage{cleveref}` and use `\Cref{sec:my}` rather than `Section~\ref{sec:my}`. – Hugh Nov 06 '20 at 10:37

2 Answers2

4

A unicode non-breaking space should work. Or pandoc parses even an HTML entity inside markdown into the correct thing, see e.g.

echo ' foo' | pandoc -t latex

~foo

If you must include some hard to parse LaTeX in your markdown, you can use generic raw attributes like:

```{=latex}
my LaTeX
```
mb21
  • 34,845
  • 8
  • 116
  • 142
  • I am asking how to get "standard Latex syntax" to work in RMarkdown. Under the hood, the "~" got replaced with "\textasciitilde{}" before being processed by pdflatex. How do I prevent that replacement? Your answer is "create new grammar and syntax" that is alien to the core of Latex. When I want "\textasciitilde{}" or "\sim" I would use it. For example, `\def\IIDdist{\,{\buildrel iid \over \sim}\,}` – mshaffer Nov 06 '20 at 14:17
  • 1
    Since you're writing markdown, I assumed you want markdown syntax? It seems you're under the mistaken impression that your input format is LaTeX. But in fact it's Rmd, which is basically [pandoc flavoured markdown](https://pandoc.org/MANUAL.html#pandocs-markdown), which can include raw latex... but is not intended to be used the way you use it. And that's the reason the `~` behaves like it does, it's not parsed as latex but as markdown. – mb21 Nov 06 '20 at 15:24
  • So how do I block off a chunk of "flavoured markdown" to be pure Latex? – mshaffer Nov 06 '20 at 23:22
  • 1
    @mshaffer using ```{=latex} , see https://pandoc.org/MANUAL.html#generic-raw-attribute – mb21 Nov 07 '20 at 08:58
  • I notice when I use `input{path/to/latex.file}` it does not parse the latex. Let me try this other syntax. What if I use a generic syntax ```{=text}```? – mshaffer Nov 07 '20 at 14:01
  • This works, if you want to update your answer ... expand it, or create a new answer. I will check it as "answered" – mshaffer Nov 07 '20 at 14:04
0
  1. According to this question and answer, you can try replacing your tilde with "\~" or "\sim" and see which one works.

  2. Since you kept the .tex file, you can always search+replace the \textasciitilde{} with a real tilde, even if #1 does not work.

Michal J Figurski
  • 1,262
  • 1
  • 11
  • 18
  • I want it to behave like a "space" not a tilde. That is, to behave like it should in Latex. – mshaffer Nov 05 '20 at 20:40
  • 1
    Maybe this will help: https://stackoverflow.com/questions/37665579/rmarkdown-space-without-line-break ? – Michal J Figurski Nov 05 '20 at 22:57
  • Thanks Michal. You are just reinforcing the point. RStudio is changing the established Latex syntax. I don't want to create "new syntax", I want the "traditional syntax" to work. Under the hood, they replaced "~" with "\textasciitilde{}". How do I prevent that replacement? – mshaffer Nov 06 '20 at 01:10