Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using R, RStudio Knit to PDF, how to pass tilde to prevent line wrapping "Table~\ref{table:data-from-phone}"?

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?

like image 540
mshaffer Avatar asked Dec 14 '25 07:12

mshaffer


1 Answers

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
```
like image 114
mb21 Avatar answered Dec 16 '25 23:12

mb21



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!