Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am very new to LaTeX and I am writing my thesis. In a chapter, I want to show my codes, which are in Mathematica. enter image description here

I use the listings package as follows:

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

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

\end{document}

However, the output is much different to what the code looks like in the Mathematica IDE (even if I use other languages in \lstset{language=<name>}):

enter image description here

I read some questions on the site but, in some of them, the font or color of the codes were not the same. Could you please help me to display something like the picture below in LaTeX?

If it is important, I am using the BiDi package.

share|improve this question
1  
As I said in my previously deleted answer, perhaps github.com/gpoore/minted can be useful. –  Timothée Poisot Jul 27 at 18:37

1 Answer 1

up vote 26 down vote accepted

Taken partially from my old answer here (click).

This code should not be considered as a bombastic template but an easy-to-customize one.

\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}

enter image description here

share|improve this answer
    
Can you decrease the width of a code block in listing? For instance, instead of a page width code block, we have a half-page code block. –  Enthusiastic Student Sep 7 at 15:28
    
@EnthusiasticStudent: use linewidth=.25\linewidth for example. –  stalking is prohibited Sep 7 at 15:39
    
it worked. thanks... :) –  Enthusiastic Student Sep 7 at 15:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.