TransWikia.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!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP