Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing tilde inserted in latex #1992

Closed
brry opened this issue Apr 28, 2021 · 6 comments
Closed

Fixing tilde inserted in latex #1992

brry opened this issue Apr 28, 2021 · 6 comments

Comments

@brry
Copy link

brry commented Apr 28, 2021

This is a comment to #492 (Code chunks with a formula as in lm(y~x) are typeset as a "high" tilde instead of a "middle" tilde) and may be closed at will:

In the .Rnw LaTeX preamble, I add

\usepackage{textcomp} % enable middle tilde 
\newcommand{\tildemid}{\raisebox{0.5ex}{\texttildelow}}

Then in the setup chunk, I add

knitr::knit_hooks$set(source = function(x, options) {
  out <- knitr::hooks_latex()$source(x, options)
  gsub("~","\\\\tildemid", out)
  })

To avoid a textcomp dependency, I also tried

gsub("\\hlopt{~} ","\\sim", out)

which looks really nice, but generates missing $ inserted errors.
I chose to not further pursue that road but use the first solution.

@yihui
Copy link
Owner

yihui commented Apr 28, 2021

which looks really nice, but generates missing $ inserted errors.

I don't understand---if it throws an error, how did you get the PDF output that looked nice? It seems you need $\\sim$?

@brry
Copy link
Author

brry commented Apr 28, 2021

I don't know how that worked while generating errors.
$\\sim$ displayed the dollar signs in the code chunk. Same with \\$\\sim\\$.
Both still produced error messages.

I'm absolutely happy with the \tildemid solution.
As for me, this issue can be closed, as long as a reference shows up in #492 in case someone else lands there in the future.

@yihui
Copy link
Owner

yihui commented Apr 28, 2021

Let me rethink about it (darn it I have completely forgotten that it was @jaredlander who filed #492---he is my VIP). Thanks for filing the issue again!

@jaredlander
Copy link
Contributor

And you're my MVP

@yihui yihui closed this as completed in ae6ab6e May 5, 2021
@yihui
Copy link
Owner

yihui commented May 5, 2021

I just provide a package option to allow for customizing the tilde symbol. Two examples:

Using \texttildelow in textcomp

\documentclass{article}
\usepackage{textcomp} % enable middle tilde
\newcommand{\tildemid}{\raisebox{0.5ex}{\texttildelow}}
\begin{document}

<<setup, include=FALSE>>=
knitr::opts_knit$set(latex.tilde = r"(\hlopt{\tildemid})")
@

<<>>=
y ~ x
@
\end{document}

Using the math symbol \sim

\documentclass{article}
\begin{document}

<<setup, include=FALSE>>=
knitr::opts_knit$set(latex.tilde = r"(\hlopt{\scriptsize{\ensuremath{\sim}}})")
@

<<>>=
y ~ x
@
\end{document}

The output of the second example looks better in my eyes, but keep in mind that the symbol is actually not a tilde, so you won't be able to copy the code and run it.

As I mentioned in #492, this is a surprisingly complicated task: https://tex.stackexchange.com/q/312/9128 I can't provide a good default (ideally the correct ~ character with no extra dependency, which seems to be impossible), and will let you decide which solution you want to adopt.

yihui added a commit to yihui/yihui.org that referenced this issue May 5, 2021
@github-actions
Copy link

github-actions bot commented Nov 3, 2021

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants