如何在 LaTeX 中实现不同编程语言代码的类似显示

如何在 LaTeX 中实现不同编程语言代码的类似显示

我对 LaTeX 还很陌生,我正在写论文。我想在一章中展示我的代码,这些代码在 Mathematica 中。 在此处输入图片描述

我使用该listings包如下:

\documentclass{article}  
\usepackage{listings}  
\begin{document}  
\lstset{language=Mathematica}  

\begin{lstlisting}  
Do[i^j,{i,1,10},{j,1,10}]  
\end{lstlisting}  

\end{document}

但是,输出与 Mathematica IDE 中的代码有很大不同(即使我使用其他语言\lstset{language=<name>}):

在此处输入图片描述

我阅读了网站上的一些问题,但其中一些问题的代码字体或颜色不一样。您能帮我在 LaTeX 中显示如下图所示的内容吗?

如果它很重要,我会使用 BiDi 包。

答案1

部分取自我的旧答案此处(点击)

此代码不应被视为浮夸的模板,而应被视为易于定制的模板。

\documentclass[dvipsnames,border=15pt,preview,12pt]{standalone}
\usepackage{xcolor}
\usepackage{listings}

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


\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{DOS}
{
    style=Common,
    backgroundcolor=\color{Black},
    basicstyle=\color{White}\scriptsize\ttfamily,
    numbers=none,
}


\lstdefinestyle{Mathematica}
{
    style=Common,
    language={Mathematica},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
        Animate,
        AnimationRunning,
    },
}

\lstdefinestyle{Fortran}
{
    style=Common,
    language={Fortran},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
        % add your new fortran keywords here!
    },
}

\lstdefinestyle{CSharp}
{
    style=Common,
    language={[Sharp]C},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
        % add your new fortran keywords here!
    },
}

\lstdefinestyle{CSFall}
{
    style=CSharp,   
    backgroundcolor=\color{Blue},
    basicstyle=\color{Yellow}\scriptsize\ttfamily,
    keywordstyle=\color{White}\sffamily,
    identifierstyle=\color{Cyan}\bfseries,
    commentstyle=\color{ForestGreen},
    stringstyle=\color{Maroon},
    numberstyle=\color{Black}\tiny\noaccsupp,
    rulecolor=\color{Black},
}


\lstdefinestyle{MathWinter}
{
    style=Mathematica,
    keywordstyle=\color{Maroon},
    identifierstyle=\color{NavyBlue},
    backgroundcolor=\color{Orange!10},
    numberstyle=\color{Red}\tiny\noaccsupp,
    rulecolor=\color{Red},
}

\lstdefinestyle{MathSummer}
{
    style=Mathematica,
    keywordstyle=\color{Orange},
    identifierstyle=\color{ForestGreen},
    backgroundcolor=\color{Cyan!10},
    numberstyle=\color{Black}\tiny\noaccsupp,
    rulecolor=\color{Maroon},
}


\lstdefinestyle{ForSpring}
{
    style=Fortran,
    keywordstyle=\color{Magenta},
    identifierstyle=\color{Red},
    backgroundcolor=\color{ForestGreen!10},
    numberstyle=\color{NavyBlue}\tiny\noaccsupp,
    rulecolor=\color{NavyBlue},
}

\lstnewenvironment{CSFall}
{\lstset{style=CSFall}}
{}


\lstnewenvironment{MathWinter}
{\lstset{style=MathWinter}}
{}

\lstnewenvironment{MathSummer}
{\lstset{style=MathSummer}}
{}

\lstnewenvironment{ForSpring}[1][]
{\lstset{style=ForSpring,#1}}
{}

\usepackage{filecontents}
\begin{filecontents*}{fortran.f95}
FORMAT("EXTERNAL CODE")
INTEGER A,B,C
READ(5,501,END=50,ERR=90) A,B,C
\end{filecontents*}


\begin{document}

\begin{CSFall}
using System;
public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("PSTricks is better than TikZ.");// single-line comment
    }
    /* multi-line comment */
}
\end{CSFall}


\begin{MathWinter}
Do[i^j, {i,1,10}, {j,1,10}];
Animate[Plot[Sin[x + a], {x, 0, 10}], {a, 0, 5}, AnimationRunning -> False] 
\end{MathWinter}

\begin{MathSummer}
Do[i^j, {i,1,10}, {j,1,10}];
Animate[Plot[Sin[x + a], {x, 0, 10}], {a, 0, 5}, AnimationRunning -> False] 
\end{MathSummer}

\begin{ForSpring}[caption={An example of Fortran code.}]
FORMAT("INLINE CODE")
INTEGER A,B,C
READ(5,501,END=50,ERR=90) A,B,C
\end{ForSpring}

\lstinputlisting[style=ForSpring,caption={An example of Fortran code.}]{fortran.f95}

\begin{lstlisting}[style=DOS]
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

c:\Users\Garbage Collector>
\end{lstlisting}
\end{document}

在此处输入图片描述

相关内容