我需要编写三种不同的编程语言:C#、R 和 TensorFlow。TensorFlow 有关键字tf$
,我想将其作为关键字添加到我的 latex 文件中,但出现了错误。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
\lstset{%
literate=
{$}{{\${$}}}1
}
\lstdefinestyle{Common}
{
basicstyle=\scriptsize\ttfamily\null,
numbers=left,
numbersep=1em,
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
breakindent=0pt,
tabsize=5,
columns=flexible,
showstringspaces=false,
captionpos=b,% or t for top (default)
abovecaptionskip=0.5\smallskipamount, % 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}
答案1
在 $ 符号前面使用 \。
答案2
使用 mathescape = true,
但关键字无法识别。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{accsupp}
\usepackage{amsmath}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
\lstset{%
literate=
{$}{{\${$}}}1
}
\lstdefinestyle{Common}
{
basicstyle=\scriptsize\ttfamily\null,
numbers=left,
numbersep=1em,
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
breakindent=0pt,
tabsize=5,
columns=flexible,
showstringspaces=false,
captionpos=b,% or t for top (default)
abovecaptionskip=0.5\smallskipamount, % 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}