0

My codes are follows:

\documentclass[executive,twoside, justified, symmetric, notoc, openany]{tufte-book}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
\newenvironment{Shaded}{}{}
%\usepackage{verbatim}
\usepackage{listings}
\lstset{literate={-}{-}1,fancyvrb=true,basicstyle=\sffamily,columns=flexible}

\begin{document}

\newcommand{\KeywordTok}[1]{\color{cyan}{#1}}
\newcommand{\NormalTok}[1]{\color{cyan}#1}
\newcommand{\OperatorTok}[1]{\color{cyan}{#1}}
\newcommand{\StringTok}[1]{\color{cyan}{#1}}
\newcommand{\CommentTok}[1]{\textcolor{gray}{\textrm{#1}}}

$\sim$

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{lm}\NormalTok{(co}\OperatorTok{$}\NormalTok{gdp }\OperatorTok{~}\StringTok{ }\NormalTok{co}\OperatorTok{$}\NormalTok{prior_gdp) }\CommentTok{# fits linear model}
\end{Highlighting}
\end{Shaded}


\end{document}

Output produced as:

enter image description here

My requirement is to match the \sim output should match inside program code, i.e., need to use \sim in both in program code and in text

PS: Excuse me for my language skill...

I tried this, Using math mode escapes with listings enabled fancyvrb but couldn't fix my need...

9
  • 1
    You probably means that you what the tilde char ~ to look like \sim (might want to update your title and text with that as it makes your problem easier to understand). There should be solutions to this on the site.
    – daleif
    Commented Jun 10, 2022 at 11:09
  • 1
    I don't know a solution but you mentioned your language skills, so I mention that the term tilde might be a good idea to use in your question and title as it makes it easier to understand. I would change the title to something similar to Tilde (~) should show like $\sim$ in verbatim text
    – daleif
    Commented Jun 10, 2022 at 11:23
  • 1
    IMHO it looks much better as is, but is there are reason you can't write something like \OperatorTok{\ensuremath{\sim}} (or possibly \OperatorTok{\ensuremath{\sim\ }} to get a trailing space)?
    – frabjous
    Commented Jun 10, 2022 at 15:25
  • 1
    Or are you trying to make the first more like the second? Then don't use \sim, use \textasciitilde.or \texttt{\textasciitilde} for the typewriter version.
    – frabjous
    Commented Jun 10, 2022 at 15:26
  • 1
    In all your \…Tok commands you should have \textcolor, not \color
    – egreg
    Commented Jun 11, 2022 at 7:54

1 Answer 1

0
\newcommand\Sim{\ensuremath{\sim}}

\Sim

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{lm}\NormalTok{(co}\OperatorTok{$}\NormalTok{gdp }\OperatorTok{\Sim}\StringTok{ 
}\NormalTok{co}\OperatorTok{$}\NormalTok{prior_gdp) }\CommentTok{# fits linear model}
\end{Highlighting}
\end{Shaded}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .