1

I am trying to build my Ph.D thesis in a template which has been basically set for Persian language. The template is wonderful when everything is persian. But there is a problem. I need to add my Python code to the appendices. I found a link here in tex.stackexchange.com which makes it exactly like in the Jupyter environment and it is beautiful.

But when I add this styling to my file, some parts of the output LaTeX file get disrupted.

The main LaTeX code is:

\documentclass[oneside,phd,12pt]{AUTthesis}

\usepackage{graphicx,adjustbox}
\graphicspath{{figures/}} %Setting the graphicspath
\usepackage{amsthm,amssymb,amsmath}
\usepackage[center]{caption}
\usepackage{afterpage}
\usepackage{lscape}
\usepackage{xcolor}
\usepackage{color,soul}
\usepackage{multirow}
\usepackage{float}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta,patterns}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usepgfplotslibrary{units}
\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}


\usepackage{array}
\usepackage[skip=0pt]{caption}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcommand{\hlgreen}[1]{{\sethlcolor{green}\hl{#1}}}

%%%%%For colorful Jupyter style of Python code
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\usepackage{xcolor}
\definecolor{maroon}{cmyk}{0, 0.87, 0.68, 0.32}
\definecolor{halfgray}{gray}{0.55}
\definecolor{ipython_frame}{RGB}{207, 207, 207}
\definecolor{ipython_bg}{RGB}{247, 247, 247}
\definecolor{ipython_red}{RGB}{186, 33, 33}
\definecolor{ipython_green}{RGB}{0, 128, 0}
\definecolor{ipython_cyan}{RGB}{64, 128, 128}
\definecolor{ipython_purple}{RGB}{170, 34, 255}

\usepackage{listings}
\lstset{
    breaklines=true,
    %
    extendedchars=true,
    literate=
    {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
    {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
    {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
    {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
    {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
    {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
    {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
    {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
    {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
    {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
    {€}{{\EUR}}1 {£}{{\pounds}}1
}

%%
%% Python definition (c) 1998 Michael Weber
%% Additional definitions (2013) Alexis Dimitriadis
%% modified by me (should not have empty lines)
%%
\lstdefinelanguage{iPython}{
    morekeywords={access,and,break,class,continue,def,del,elif,else,except,exec,finally,for,from,global,if,import,in,is,lambda,not,or,pass,print,raise,return,try,while},%
    %
    % Built-ins
    morekeywords=[2]{abs,all,any,basestring,bin,bool,bytearray,callable,chr,classmethod,cmp,compile,complex,delattr,dict,dir,divmod,enumerate,eval,execfile,file,filter,float,format,frozenset,getattr,globals,hasattr,hash,help,hex,id,input,int,isinstance,issubclass,iter,len,list,locals,long,map,max,memoryview,min,next,object,oct,open,ord,pow,property,range,raw_input,reduce,reload,repr,reversed,round,set,setattr,slice,sorted,staticmethod,str,sum,super,tuple,type,unichr,unicode,vars,xrange,zip,apply,buffer,coerce,intern},%
    %
    sensitive=true,%
    morecomment=[l]\#,%
    morestring=[b]',%
    morestring=[b]",%
    %
    morestring=[s]{'''}{'''},% used for documentation text (mulitiline strings)
    morestring=[s]{"""}{"""},% added by Philipp Matthias Hahn
    %
    morestring=[s]{r'}{'},% `raw' strings
    morestring=[s]{r"}{"},%
    morestring=[s]{r'''}{'''},%
    morestring=[s]{r"""}{"""},%
    morestring=[s]{u'}{'},% unicode strings
    morestring=[s]{u"}{"},%
    morestring=[s]{u'''}{'''},%
    morestring=[s]{u"""}{"""},%
    %
    % {replace}{replacement}{lenght of replace}
    % *{-}{-}{1} will not replace in comments and so on
    literate=
    {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
    {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
    {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
    {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
    {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
    {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
    {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
    {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
    {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
    {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
    {€}{{\EUR}}1 {£}{{\pounds}}1
    %
    {^}{{{\color{ipython_purple}\^{}}}}1
    {=}{{{\color{ipython_purple}=}}}1
    %
    {+}{{{\color{ipython_purple}+}}}1
    {*}{{{\color{ipython_purple}$^\ast$}}}1
    {/}{{{\color{ipython_purple}/}}}1
    %
    {+=}{{{+=}}}1
    {-=}{{{-=}}}1
    {*=}{{{$^\ast$=}}}1
    {/=}{{{/=}}}1,
    literate=
    *{-}{{{\color{ipython_purple}-}}}1
     {?}{{{\color{ipython_purple}?}}}1,
    %
    identifierstyle=\color{black}\ttfamily,
    commentstyle=\color{ipython_cyan}\ttfamily,
    stringstyle=\color{ipython_red}\ttfamily,
    keepspaces=true,
    showspaces=false,
    showstringspaces=false,
    %
    rulecolor=\color{ipython_frame},
    frame=single,
    frameround={t}{t}{t}{t},
    framexleftmargin=6mm,
    numbers=left,
    numberstyle=\tiny\color{halfgray},
    %
    %
    backgroundcolor=\color{ipython_bg},
    %   extendedchars=true,
    basicstyle=\scriptsize,
    keywordstyle=\color{ipython_green}\ttfamily,
}

\input{commands}

\begin{document}
\baselineskip=.75cm
\linespread{1.75}
\input{fa_title}
\pagenumbering{alph}
%-----------------------------------------------------------------------------

\input{TOC-TOF-LOT}

\pagenumbering{arabic}
\pagestyle{style1}


%--------------------------------------------------------------------------appendix

\chapterfont{\vspace*{-2em}\centering\LARGE}%

\appendix
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\include{appendix1}

\end{document}

And the appendix is:

\chapter{کد شبیه‌سازی به زبان $Python$ در بستر $Jupyter$\ref{sec5}}\label{PythonCode}
\begin{lstlisting}[language=iPython]
"""
Initial definitions
"""

KSstrategies=['S','N']
CAstrategies=['R','O','D']
ALLstrategies=['SR','SO','SD','NR','NO','ND']
ALLstrategiesCodes={'SR':0,'SO':1,'SD':2,'NR':3,'NO':4,'ND':5}
Theta = 0.2
s = 0.3333
Lambda = 0.4
Etha = 1.15
l = {'Ent':0.08,'Inc':0.08} #[Ent,Inc]
k = {'Ent':Etha*l['Ent'],'Inc':Etha*l['Inc']} #[Ent,Inc]
n={'Ent':0.27,'Inc':0.12} #[Ent,Inc]
r={'Ent':0.2,'Inc':0.15} #[Ent,Inc]
v={'Ent':0.36,'Inc':0.225} #[Ent,Inc]
rho = {'Ent':1.2,'Inc':0.8} #[Ent,Inc]
omega = {'Ent':0.9,'Inc':1.1} #[Ent,Inc]
delta = {'Ent':1.4,'Inc':0.6} #[Ent,Inc]
phi = {'Ent':0.07,'Inc':0.085} #[Ent,Inc]
tau = {'Ent':0.06,'Inc':0.066} #[Ent,Inc]
c={}
c['Inc']={'R':0.09,'O':0.10,'D':0.20} #c[R,O,D]
c['Ent']={'R':0.11,'O':0.12,'D':0.14} #c[R,O,D]
costToQualityRatio = 2
ksnoise = 0.15

"""
Define functions
"""

import numpy as np
def optimumprofitIncLead(qEnt,qInc,Leader):
    if Leader == 'Incumbent':
        #The Incumbent is the leader in pricing
        if qEnt>qInc:
            priceInc = ((qEnt - qInc)*qInc)/(2*(2*qEnt - qInc))
            profitInc = ((qEnt - qInc)*qInc)/(16*qEnt - 8*qInc)
            priceEnt = ((qEnt - qInc)*(4*qEnt - qInc))/(4*(2*qEnt - qInc))
            profitEnt = ((qEnt - qInc)*(-4*qEnt + qInc)**2)/(16*(-2*qEnt + qInc)**2)
        elif qEnt==qInc:
            priceInc = 'Whatever'
            profitInc = 0
            priceEnt = qEnt/2
            profitEnt = qEnt/4
        elif qEnt<qInc:
            priceInc = ((qInc-qEnt)*qInc)/(2*qInc-qEnt)
            profitInc = ((qEnt - qInc)*qInc)/(2*(qEnt - 2*qInc))
            priceEnt = ((qInc-qEnt)*qEnt)/(2*(2*qInc-qEnt))
            profitEnt = (qEnt*qInc*(-qEnt + qInc))/(4*(qEnt - 2*qInc)**2)
    elif Leader == 'Entrant':
        #The New Entrant is the leader in pricing
        if qEnt>qInc:
            priceInc = ((qInc-qEnt)*qInc)/(2*(qInc-2*qEnt))
            profitInc = (qEnt*qInc*(qEnt-qInc))/(4*((2*qEnt-qInc)**2))
            priceEnt = ((qInc-qEnt)*qEnt)/(qInc-2*qEnt)
            profitEnt = ((qEnt - qInc)*qEnt)/(4*qEnt - 2*qInc)
        elif qEnt==qInc:
            priceInc = qInc/2
            profitInc = qInc/4
            priceEnt = 'Whatever'
            profitEnt = 0
        elif qEnt<qInc:
            priceInc = ((qInc - qEnt)*(4*qInc - qEnt))/(4*(2*qInc - qEnt))
            profitInc = ((qInc - qEnt)*(-4*qInc + qEnt)**2)/(16*(-2*qInc + qEnt)**2)
            priceEnt = ((qInc - qEnt)*qEnt)/(2*(2*qInc - qEnt))
            profitEnt = ((qInc - qEnt)*qEnt)/(16*qInc - 8*qEnt)      
    return priceEnt,profitEnt,priceInc,profitInc

def KSpayoff(strategyEnt,strategyInc):
    global payoffEnt, payoffInc
    if strategyEnt=='S' and strategyInc=='S':
        payoffEnt=(1+Theta)*(r['Ent']+r['Inc']+Lambda*s+l['Ent'])-n['Ent']
        payoffInc=(1+Theta)*(r['Ent']+r['Inc']+(1-Lambda)*s+l['Inc'])-n['Inc']
    elif strategyEnt=='S' and strategyInc=='N':
        payoffEnt=(1+Theta)*(r['Ent']-n['Ent'])
        payoffInc=(1-Theta)*(r['Ent']+r['Inc']+v['Inc']+l['Inc'])
    elif strategyEnt=='N' and strategyInc=='S':
        payoffEnt=(1-Theta)*(r['Ent']+r['Inc']+v['Ent']+l['Ent'])
        payoffInc=(1+Theta)*(r['Inc']-n['Inc'])
    elif strategyEnt=='N' and strategyInc=='N':
        payoffEnt=(1-Theta)*(r['Ent']+v['Ent'])
        payoffInc=(1-Theta)*(r['Inc']+v['Inc'])
    return payoffEnt,payoffInc
def CApayoff(CAstrategyEnt,CAstrategyInc,KSstrategyEnt,KSstrategyInc,qEnt,qInc,Leader):
    global payoffEnt, payoffInc
    receivedknowledge = {}
    if KSstrategyEnt == 'N':
        receivedknowledge['Inc'] = 0
    else:
        receivedknowledge['Inc'] = 1 
    if KSstrategyInc == 'N':
        receivedknowledge['Ent'] = 0
    else:
        receivedknowledge['Ent'] = 1 
        
    if CAstrategyEnt=='R' and CAstrategyInc=='R':
        priceEnt,profitEnt,priceInc,profitInc = optimumprofitIncLead(qEnt,qInc,Leader)
        payoffEnt = rho['Ent']*profitEnt-c['Ent']['R']
        payoffInc = rho['Inc']*profitInc-c['Inc']['R']
    elif CAstrategyEnt=='R' and CAstrategyInc=='O':
        qInc += receivedknowledge['Inc']*k['Inc']+c['Inc']['O']/costToQualityRatio-phi['Inc']
        priceEnt,profitEnt,priceInc,profitInc = optimumprofitIncLead(qEnt,qInc,Leader)
        payoffEnt = rho['Ent']*profitEnt-c['Ent']['R']
        payoffInc = omega['Inc']*profitInc-c['Inc']['O']
    elif CAstrategyEnt=='R' and CAstrategyInc=='D':
        qInc += np.tanh(receivedknowledge['Inc']*k['Inc']+c['Inc']['D']/costToQualityRatio-tau['Inc'])
        priceEnt,profitEnt,priceInc,profitInc = optimumprofitIncLead(qEnt,qInc,Leader)
        payoffEnt = rho['Ent']*profitEnt-c['Ent']['R']
        payoffInc = delta['Inc']*profitInc-c['Inc']['D']
    elif CAstrategyEnt=='O' and CAstrategyInc=='R':
        qEnt += receivedknowledge['Ent']*k['Ent']+c['Ent']['O']/costToQualityRatio-phi['Ent']
        priceEnt,profitEnt,priceInc,profitInc = optimumprofitIncLead(qEnt,qInc,Leader)
        payoffEnt = omega['Ent']*profitEnt-c['Ent']['O']
        payoffInc = rho['Inc']*profitInc-c['Inc']['R']
    elif CAstrategyEnt=='O' and CAstrategyInc=='O':
        qEnt += receivedknowledge['Ent']*k['Ent']+c['Ent']['O']/costToQualityRatio-phi['Ent']
        qInc += receivedknowledge['Inc']*k['Inc']+c['Inc']['O']/costToQualityRatio-phi['Inc']
        priceEnt,profitEnt,priceInc,profitInc = optimumprofitIncLead(qEnt,qInc,Leader)
        payoffEnt = omega['Ent']*profitEnt-c['Ent']['O']
        payoffInc = omega['Inc']*profitInc-c['Inc']['O']
    elif CAstrategyEnt=='O' and CAstrategyInc=='D':
        qEnt += receivedknowledge['Ent']*k['Ent']+c['Ent']['O']/costToQualityRatio-phi['Ent']
        qInc += np.tanh(receivedknowledge['Inc']*k['Inc']+c['Inc']['D']/costToQualityRatio-tau['Inc'])
        priceEnt,profitEnt,priceInc,profitInc = optimumprofitIncLead(qEnt,qInc,Leader)
        payoffEnt = omega['Ent']*profitEnt-c['Ent']['O']
        payoffInc = delta['Inc']*profitInc-c['Inc']['D']
    elif CAstrategyEnt=='D' and CAstrategyInc=='R':
        qEnt += np.tanh(receivedknowledge['Ent']*k['Ent']+c['Ent']['D']/costToQualityRatio-tau['Ent'])
        priceEnt,profitEnt,priceInc,profitInc = optimumprofitIncLead(qEnt,qInc,Leader)
        payoffEnt = delta['Ent']*profitEnt-c['Ent']['D']
        payoffInc = rho['Inc']*profitInc-c['Inc']['R']
    elif CAstrategyEnt=='D' and CAstrategyInc=='O':
        qEnt += np.tanh(receivedknowledge['Ent']*k['Ent']+c['Ent']['D']/costToQualityRatio-tau['Ent'])
        qInc += receivedknowledge['Inc']*k['Inc']+c['Inc']['O']/costToQualityRatio-phi['Inc']
        priceEnt,profitEnt,priceInc,profitInc = optimumprofitIncLead(qEnt,qInc,Leader)
        payoffEnt = delta['Ent']*profitEnt-c['Ent']['D']
        payoffInc = omega['Inc']*profitInc-c['Inc']['O']
    elif CAstrategyEnt=='D' and CAstrategyInc=='D':
        qEnt += np.tanh(receivedknowledge['Ent']*k['Ent']+c['Ent']['D']/costToQualityRatio-tau['Ent'])
        qInc += np.tanh(receivedknowledge['Inc']*k['Inc']+c['Inc']['D']/costToQualityRatio-tau['Inc'])
        priceEnt,profitEnt,priceInc,profitInc = optimumprofitIncLead(qEnt,qInc,Leader)
        payoffEnt = delta['Ent']*profitEnt-c['Ent']['D']
        payoffInc = delta['Inc']*profitInc-c['Inc']['D']
    return payoffEnt,payoffInc,qEnt,qInc



\end{lstlisting}

There are three problems in the output (please also see the images):

  1. Some Persian parts of the file which were OK before adding the python styling are now displayed in squares!!!

enter image description here

  1. The coloring python code itself is fine, but all the words are displayed in reverse as can be seen in the image. For example the word def is displayed like fed which is weird.

enter image description here

  1. The numbers are displayed in Persian. This is OK for the rest of the document but here I wish they could be in English.

Would be grateful for any advice. Thanks.

3
  • 1
    you should not load code meant for pdflatex like \usepackage[T1]{fontenc} in a xepersian document. But beside this: your thesis class is not on ctan and so it is not possible to compile your code. Sep 16, 2020 at 18:22
  • I found a solution for the last two problems. I just had to put the lisings inside \begin{latin}\end{latin}. But still problem 1 remains (squares).
    – Mike
    Sep 17, 2020 at 6:27
  • @UlrikeFischer I tried your suggestion and removed \usepackage[T1]{fontenc} and also \usepackage[utf8]{inputenc} and \usepackage[english]{babel}. And now everythin is back to normal. Thanks a lot.
    – Mike
    Sep 17, 2020 at 6:31

0

You must log in to answer this question.

Browse other questions tagged .