AnswerBun.com

How to display $ in listings Latex

TeX - LaTeX Asked by vrige on September 5, 2020

I need to write three different programming languages: C#, R and TensorFlow.
TensorFlow has the keyword tf$ and i would like to have it as keyword in my latex file, but i have got an error.

documentclass{article}
usepackage[utf8]{inputenc}

usepackage{xcolor}
usepackage{listings}
usepackage{accsupp}
newcommand*{noaccsupp}[1]{BeginAccSupp{ActualText={}}#1EndAccSupp{}}

lstset{%
literate=
        {$}{{${$}}}1
}

lstdefinestyle{Common}
{
    basicstyle=scriptsizettfamilynull,
    numbers=left,
    numbersep=1em,
    frame=single,
    framesep=fboxsep,
    framerule=fboxrule,
    xleftmargin=dimexprfboxsep+fboxrule,
    xrightmargin=dimexprfboxsep+fboxrule,
    breaklines=true,
    breakindent=0pt,
    tabsize=5,
    columns=flexible,
    showstringspaces=false,
    captionpos=b,% or t for top (default)
    abovecaptionskip=0.5smallskipamount,   % there is also belowcaptionskip
}
lstdefinestyle{Mathematica}
{
    style=Common,
    language={Mathematica},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
        Animate,
        AnimationRunning,
    },
    keywordstyle =color{blue},
}
lstdefinestyle{CSharp}
{
    style=Common,
    language={[Sharp]C},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
       %
    },
    keywordstyle =color{blue},
}
lstdefinestyle{R}
{
    style=Common,
    language={R},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
       %
    },
    keywordstyle =color{blue},
}
lstdefinestyle{TensorFlow}
{
    alsoletter = {$},
    style=Common,
    language={[Sharp]C},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
        tf$
    },
    keywordstyle =color{blue},
}

lstnewenvironment{Mathematica}
{lstset{style=Mathematica}}
{}
lstnewenvironment{CSharp}
{lstset{style=CSharp}}
{}
lstnewenvironment{R}
{lstset{style=R}}
{}
lstnewenvironment{TensorFlow}
{lstset{style=TensorFlow}}
{}

title{...}
author{...}
date{August 2020}

begin{document}

maketitle

section{Introduction}

begin{R}
fib <- function(n) {
  if (n < 2)
    n
  else
    fib(n - 1) + fib(n - 2)
}
fib(10) # => 55
end{R}

begin{TensorFlow}
 sess$run(TF_up81a)
 tf$logical_not()
end{TensorFlow}

end{document}

2 Answers

Using mathescape = true, but the keyword is not reconize.enter image description here

documentclass{article}
usepackage[utf8]{inputenc}

usepackage{xcolor}
usepackage{listings}
usepackage{accsupp}
usepackage{amsmath}

newcommand*{noaccsupp}[1]{BeginAccSupp{ActualText={}}#1EndAccSupp{}}

lstset{%
literate=
        {$}{{${$}}}1
}

lstdefinestyle{Common}
{
    basicstyle=scriptsizettfamilynull,
    numbers=left,
    numbersep=1em,
    frame=single,
    framesep=fboxsep,
    framerule=fboxrule,
    xleftmargin=dimexprfboxsep+fboxrule,
    xrightmargin=dimexprfboxsep+fboxrule,
    breaklines=true,
    breakindent=0pt,
    tabsize=5,
    columns=flexible,
    showstringspaces=false,
    captionpos=b,% or t for top (default)
    abovecaptionskip=0.5smallskipamount,   % there is also belowcaptionskip
}
lstdefinestyle{Mathematica}
{
    style=Common,
    language={Mathematica},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
        Animate,
        AnimationRunning,
    },
    keywordstyle =color{blue},
}
lstdefinestyle{CSharp}
{
    style=Common,
    language={[Sharp]C},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
       %
    },
    keywordstyle =color{blue},
}
lstdefinestyle{R}
{
    style=Common,
    language={R},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
       %
    },
    keywordstyle =color{blue},
}
lstdefinestyle{TensorFlow}
{
  % alsoletter = {$},
%  escapechar= {$},
  mathescape = true,
    style=Common,
    language={[Sharp]C},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
        tf$mathdollar$%
    },
    keywordstyle =color{blue},
}

lstnewenvironment{Mathematica}
{lstset{style=Mathematica}}
{}
lstnewenvironment{CSharp}
{lstset{style=CSharp}}
{}
lstnewenvironment{R}
{lstset{style=R}}
{}
lstnewenvironment{TensorFlow}
{lstset{style=TensorFlow}}
{}

title{...}
author{...}
date{August 2020}

begin{document}

maketitle

section{Introduction}

begin{R}
  fib <- function(n) {
    if (n < 2)
    n
    else
    fib(n - 1) + fib(n - 2)
  }
fib(10) # => 55
end{R}

begin{TensorFlow}
 sess$mathdollar$run(TF_up81a)
 $color{blue}rm{tf}mathdollar$logical_not()
end{TensorFlow}

end{document}

Answered by flav on September 5, 2020

By using in the front of $ symbol.

Answered by Anton Vrdoljak on September 5, 2020

Add your own answers!

Related Questions

kpfonts changes appereance of textcomp symbols

4  Asked on June 26, 2021 by david-woitkowski

     

GIF in XeLaTeX PDF using animate

0  Asked on June 26, 2021 by alayt-abraham

       

Cannot include my .bib content into my .tex document

1  Asked on June 26, 2021 by john-campbell

 

Reduce horizontal gap between legend and pie chart

1  Asked on June 25, 2021 by yogesh-sharma

   

Problem with writing in Hebrew in LyX

0  Asked on June 25, 2021 by alor-k

   

ETbb conflict with tufte

1  Asked on June 25, 2021 by otaviolb

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir