%##################################### packages
\usepackage{amsthm,amssymb,mathrsfs,graphicx,float,color}
\usepackage{fancyvrb,fancyhdr,makeidx,amsfonts,bidicode,tabularx}
\usepackage{cool,tikz,subfigure}
\usepackage[fleqn]{amsmath}
%\allowdisplaybreaks
\usepackage{zref-perpage}
\newcommand\persiangloss[2]{#1\dotfill\lr{#2}\\}
\usepackage{algorithm}
\usepackage{algorithmic}
%amsmath,fleqn,
\usepackage{perpage}
\usepackage{tocbibind}
\usepackage{fancybox}
\usepackage[top=3cm,right=3cm,bottom=2.5cm,left=2.5cm]{geometry}      
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\usepackage{graphicx}
\usepackage{fancyhdr}
%\usepackage[usenames,dvipsnames]{color}
\usepackage{xcolor}
\definecolor{green2}{HTML}{669933}

%تنظیمات فونت caption
\usepackage[font={small}]{caption}%,labelfont={normalsize,it}]{caption}
%
\renewcommand*{\baselinestretch}{1.5}
%
%
%\makeatletter
%\renewenvironment{figure}
%               {
%\renewcommand{\baselinestretch}{1}
%\renewcommand{\abovecaptionskip}{5pt}
%\renewcommand{\belowcaptionskip}{5pt}
%\@float{table}
%}
%               {\end@float}
%
%\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% بسته مورد نیاز برای نوشتن کدهای برنامه نویسی در نوشتار 
\usepackage{listings}
\usepackage{framed}
% بسته ای برای رنگی کردن لینک ها و فعال سازی لینک ها در یک نوشتار
%pagebackref=true در کنار مرجع صفخه ارجاع قرار می‌گیرد

\usepackage[colorlinks,linkcolor=blue,filecolor=blue,urlcolor=cyan,citecolor=magenta]{hyperref}
%\immediate\write18{xindy -L persian -C utf8 -I xindy -M \jobname.xdy  -t \jobname.glg -o \jobname.gls \jobname.glo}
\usepackage{makeidx}
\usepackage{ifpdf}

\usepackage[extrafootnotefeatures]{xepersian}
\twocolumnfootnotes
\usepackage{hyperref}
\usepackage[xindy ,acronym, nonumberlist=true] {glossaries}
\usepackage{xepersian}
\settextfont[Scale=1]{Yas}%{B Nazanin}
\setlatintextfont[Scale=1]{Times New Roman}
%\setdigitfont{Arial}
%\setdigitfont{B Zar}
\setdigitfont[Scale=1]{Yas}
%%##################################### new command
\newcommand{\dt}{\,\mathsf{d}t}
\newcommand{\ds}{\,\mathsf{d}s}
\newcommand{\no}{\nonumber}
\newcommand{\mbb}[1]{\mathbb{#1}}
\newcommand{\mc}[1]{\mathcal{#1}}
\newcommand{\inner}[1]{\left\langle #1\right\rangle}
\newcommand{\abs}[1]{\left| #1\right|}
\newcommand{\norm}[1]{\left\| #1\right\|}
\MakePerPage[1]{footnote}
\defpersianfont\nastaliq{IranNastaliq}
\defpersianfont\timei{Times New Roman}
\setfootnoteLR
\setcounter{tocdepth}{3}
\setlength{\mathindent}{0pt}
%algorithm
%%##############################################
\newenvironment{alteralgorithm}{}{}
\newcommand{\altercaption}[1]{\refstepcounter{algorithm}%
\noindent\hrule height .8pt
\smallskip\noindent
\textbf{الگوریتم \thealgorithm} #1
\hrule}
\renewcommand{\algorithmicrequire}{\textbf{ورودی:}}
\renewcommand{\algorithmicensure}{\textbf{خروجی:}}
\renewcommand{\algorithmicif}{\textbf{اگر}}
\renewcommand{\algorithmicthen}{\textbf{آن‌گاه}}
\renewcommand{\algorithmicfor}{\textbf{برای}}
\renewcommand{\algorithmicrepeat}{\textbf{تکرار کن}}
\renewcommand{\algorithmicuntil}{\textbf{تا اینکه}}
\renewcommand{\algorithmicprint}{\textbf{چاپ کن}}
\renewcommand{\algorithmicreturn}{\textbf{برگردان}}
\renewcommand{\algorithmictrue}{\textbf{درست}}
\renewcommand{\algorithmicfalse}{\textbf{غلط}}
\renewcommand{\algorithmicdo}{\textbf{انجام بده}}
\renewcommand{\algorithmicend}{\textbf{پایان}}
\renewcommand{\algorithmicwhile}{\textbf{تا زمانی که}}
\renewcommand{\algorithmicelse}{\textbf{در غیر اینصورت}}
\renewcommand{\algorithmicforall}{\textbf{برای هر}}

%\twocolumnfootnotes
%##################################### theorem & environment
\newtheoremstyle{mystyle}{1.5ex}{1.5ex}{\mdseries}{}{\bfseries}{.}{ }{}
\theoremstyle{mystyle}
\newtheorem{sop}{فرضیه}[chapter]
\newtheorem{thm}{قضیه}[chapter]
\newtheorem{notation}{نکته}[chapter]
\newtheorem{definition}{تعریف}[chapter]
\newtheorem{lemma}{لم}[chapter]
\newtheorem{corollary}{نتیجه}[chapter]
\newtheorem{example}{مثال}[chapter]
\newtheoremstyle{esbat}% name
{3pt}% Space above
{3pt}% Space below
{}% Body font
{}% Indent amount
{\itshape}% Theorem head font
{.}% Punctuation after theorem head
{.5em}% Space after theorem head
{}% Theorem head spec (can be left empty, meaning `normal')
\theoremstyle{esbat}
\newtheorem*{prof}{برهان}
\renewenvironment{proof}{\begin{prof}}{\hfill$\blacksquare$\end{prof}}
%\renewenvironment{proof}{\begin{prof}}{$\blacksquare$\end{prof}}
%##################################### Header
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\renewcommand{\headrulewidth}{2pt}
\fancyhead[R]{\chaptername\thechapter.\hspace{2mm}\leftmark}
%\fancyhead[O]{\thesection.\hspace{2mm}\rightmark}
%S\fancyhead[EOL]{\thepage}
\fancyfoot[C]{\thepage}
%##################################### insert \includegraphics{fig.1}
\ifpdf
\DeclareGraphicsRule{*}{mps}{*}{}
\fi
\graphicspath{{files/}}

\makeindex
%################################# insert figure with multi part.
%\usepackage{subfig}
\makeatletter
\def\abj@num@i#1{
\ifcase#1\or الف\or ب\or ج\or د
\or ه‍\or و\or ز\or ح\or ط\fi
\ifnum#1=\z@\abjad@zero\fi}
\makeatother 

\makeatletter
\def\@myharfi#1{\ifcase#1\or الف\or ب\or پ\or ت\or ث\or
ج\or چ\or ح\or خ\or د\or ذ\or ر\or ز\or س\or ش\or ص\or ض\or ع\or غ\or
ف\or ق\or ک\or گ\or ل\or م\or ن\or و\or ه\or ی
\else\@ctrerr\fi}
\def\myharfi#1{\expandafter\@myharfi\csname c@#1\endcsname}
\makeatother
\renewcommand{\thesubfigure}{\myharfi{subfigure}}
\renewcommand*{\thesubfigure}{(\myharfi{subfigure})}
%\captionsetup[subfloat]{labelformat=simple,listofformat=subsimple}
%%\makeglossaries
%######################################Matlab Code
\lstset{% general command to set parameter(s) 
basicstyle=\small, % print whole listing small
keywordstyle=\color{blue}\bfseries,
% underlined bold black keywords
identifierstyle=, % nothing happens
stringstyle=\ttfamily\color{red},
commentstyle=\color{green2}, % white comments
stringstyle=\ttfamily\color{red}, % typewriter type for strings
showstringspaces=false} % no special string spaces
%%%%%%%%%%%%%%%
%%%%%% ============================================================================================================

%%% تنظیمات مربوط به بسته  glossaries
%%% تعریف استایل برای واژه نامه فارسی به انگلیسی، در این استایل واژه‌های فارسی در سمت راست و واژه‌های انگلیسی در سمت چپ خواهند آمد. از حالت گروه ‌بندی استفاده می‌کنیم، 
%%% یعنی واژه‌ها در گروه‌هایی به ترتیب حروف الفبا مرتب می‌شوند، مثلا:
%%% الف
%%% افتصاد ................................... Economy
%%% اشکال ........................................ Failure
%%% ش
%%% شبکه ...................................... Network
\newglossarystyle{myFaToEn}{%
	\renewenvironment{theglossary}{}{}
	\renewcommand*{\glsgroupskip}{\vskip 10mm}
	\renewcommand*{\glsgroupheading}[1]{\subsection*{\glsgetgrouptitle{##1}}}
	\renewcommand*{\glossentry}[2]{\noindent\glsentryname{##1}\dotfill\space \glsentrytext{##1}
		
	}
}

%% % تعریف استایل برای واژه نامه انگلیسی به فارسی، در این استایل واژه‌های فارسی در سمت راست و واژه‌های انگلیسی در سمت چپ خواهند آمد. از حالت گروه ‌بندی استفاده می‌کنیم، 
%% % یعنی واژه‌ها در گروه‌هایی به ترتیب حروف الفبا مرتب می‌شوند، مثلا:
%% % E
%%% Economy ............................... اقتصاد
%% % F
%% % Failure................................... اشکال
%% %N
%% % Network ................................. شبکه

\newglossarystyle{myEntoFa}{%
	%%% این دستور در حقیقت عملیات گروه‌بندی را انجام می‌دهد. بدین صورت که واژه‌ها در بخش‌های جداگانه گروه‌بندی می‌شوند، 
	%%% عنوان بخش همان نام حرفی است که هر واژه در آن گروه با آن شروع شده است. 
	\renewenvironment{theglossary}{}{}
	\renewcommand*{\glsgroupskip}{\vskip 10mm}
	\renewcommand*{\glsgroupheading}[1]{\begin{LTR} \subsection*{ \glsgetgrouptitle{##1}} \end{LTR}}
	%%% در این دستور نحوه نمایش واژه‌ها می‌آید. در این جا واژه فارسی در سمت راست و واژه انگلیسی در سمت چپ قرار داده شده است، و بین آن با نقطه پر می‌شود. 
	\renewcommand*{\glossentry}[2]{\noindent\glsentrytext{##1}\dotfill\space \glsentryname{##1}
		
	}
}

%%% تعیین استایل برای فهرست اختصارات
\newglossarystyle{myAbbrlist}{%
	%%% این دستور در حقیقت عملیات گروه‌بندی را انجام می‌دهد. بدین صورت که اختصارات‌ در بخش‌های جداگانه گروه‌بندی می‌شوند، 
	%%% عنوان بخش همان نام حرفی است که هر اختصار در آن گروه با آن شروع شده است. 
	\renewenvironment{theglossary}{}{}
	\renewcommand*{\glsgroupskip}{\vskip 10mm}
	\renewcommand*{\glsgroupheading}[1]{\begin{LTR} \subsection*{\glsgetgrouptitle{##1}} \end{LTR}}
	%%% در این دستور نحوه نمایش اختصارات می‌آید. در این جا حالت کوچک اختصار در سمت چپ و حالت بزرگ در سمت راست قرار داده شده است، و بین آن با نقطه پر می‌شود. 
	\renewcommand*{\glossentry}[2]{\noindent\glsentrytext{##1}\dotfill\space \Glsentrylong{##1}
		
	}
	%%% تغییر نام محیط abbreviation به فهرست اختصارات
	\renewcommand*{\acronymname}{\rl{فهرست اختصارات}}
}

%%% برای اجرا xindy بر روی فایل .tex و تولید واژه‌نامه‌ها و فهرست اختصارات و فهرست نمادها یکسری  فایل تعریف شده است.‌ Latex داده های مربوط به واژه نامه و .. را در این 
%%%  فایل‌ها نگهداری می‌کند. مهم‌ترین option‌ این قسمت این است که 
%%% عنوان واژه‌نامه‌ها و یا فهرست اختصارات و یا فهرست نمادها را می‌توانید در این‌جا مشخص کنید. 
%%% در این جا عباراتی مثل glg، gls، glo و ... پسوند فایل‌هایی است که برای xindy بکار می‌روند. 
\newglossary[glg]{english}{gls}{glo}{واژه‌نامه انگلیسی به فارسی}
\newglossary[blg]{persian}{bls}{blo}{واژه‌نامه فارسی به انگلیسی}
\makeglossaries
\glsdisablehyper
%%% تعاریف مربوط به تولید واژه نامه و فهرست اختصارات و فهرست نمادها
%%%  در این فایل یکسری دستورات عمومی برای وارد کردن واژه‌نامه آمده است.
%%%  به دلیل این‌که قرار است این دستورات پایه‌ای را بازنویسی کنیم در این‌جا تعریف می‌کنیم. 
\let\oldgls\gls
\let\oldglspl\glspl

\makeatletter

\renewrobustcmd*{\gls}{\@ifstar\@msgls\@mgls}
\newcommand*{\@mgls}[1] {\ifthenelse{\equal{\glsentrytype{#1}}{english}}{\oldgls{#1}\glsuseri{f-#1}}{\lr{\oldgls{#1}}}}
\newcommand*{\@msgls}[1]{\ifthenelse{\equal{\glsentrytype{#1}}{english}}{\glstext{#1}\glsuseri{f-#1}}{\lr{\glsentryname{#1}}}}

\renewrobustcmd*{\glspl}{\@ifstar\@msglspl\@mglspl}
\newcommand*{\@mglspl}[1] {\ifthenelse{\equal{\glsentrytype{#1}}{english}}{\oldglspl{#1}\glsuseri{f-#1}}{\oldglspl{#1}}}
\newcommand*{\@msglspl}[1]{\ifthenelse{\equal{\glsentrytype{#1}}{english}}{\glsplural{#1}\glsuseri{f-#1}}{\glsentryplural{#1}}}

\makeatother

\newcommand{\newword}[4]{
	\newglossaryentry{#1}     {type={english},name={\lr{#2}},plural={#4},text={#3},description={}}
	\newglossaryentry{f-#1} {type={persian},name={#3},text={\lr{#2}},description={}}
}

%%% بر طبق این دستور، در اولین باری که واژه مورد نظر از واژه‌نامه وارد شود، پاورقی زده می‌شود. 
\defglsentryfmt[english]{\glsgenentryfmt\ifglsused{\glslabel}{}{\LTRfootnote{\glsentryname{\glslabel}}}}

%%% بر طبق این دستور، در اولین باری که واژه مورد نظر از فهرست اختصارات وارد شود، پاورقی زده می‌شود. 
\defglsentryfmt[acronym]{\glsentryname{\glslabel}\ifglsused{\glslabel}{}{\LTRfootnote{\glsentrydesc{\glslabel}}}}


%%%%%% ============================================================================================================

\let\Oldprintglossary\printglossary
%%============================ دستور برای قرار دادن فهرست اختصارات 
\newcommand{\printabbreviation}{
	\cleardoublepage
	\phantomsection
	\baselineskip=.75cm
	%% با این دستور عنوان فهرست اختصارات به فهرست مطالب اضافه می‌شود. 
	\addcontentsline{toc}{chapter}{فهرست اختصارات}
	\setglossarystyle{myAbbrlist}
	\begin{LTR}
		\Oldprintglossary[type=acronym]	
	\end{LTR}
	\clearpage
}%

\newcommand{\printacronyms}{\printabbreviation}
%%% در این جا محیط هر دو واژه نامه را باز تعریف کرده ایم، تا اولا مشکل قرار دادن صفحه اضافی را حل کنیم، ثانیا عنوان واژه نامه ها را با دستور addcontentlist وارد فهرست مطالب کرده ایم.
\renewcommand{\printglossary}{
	\let\appendix\relax
	%% تنظیم کننده فاصله بین خطوط در این قسمت
	\clearpage
	\phantomsection
	%% این دستور موجب این می‌شود که واژه‌نامه‌ها در  حالت دو ستونی نوشته شود. 
	\twocolumn{}
	%% با این دستور عنوان واژه‌نامه به فهرست مطالب اضافه می‌شود. 
	\addcontentsline{toc}{chapter}{واژه نامه انگلیسی به فارسی}%
	\setglossarystyle{myEntoFa}%
	\Oldprintglossary[type=english]%
%
	\clearpage
	\phantomsection
	%% با این دستور عنوان واژه‌نامه به فهرست مطالب اضافه می‌شود. 
	\addcontentsline{toc}{chapter}{واژه نامه فارسی به انگلیسی}%
	\setglossarystyle{myFaToEn}%
	\Oldprintglossary[type=persian]%
	\onecolumn{}%
%
\addcontentsline{toc}{chapter}{پیوست}
\setglossarystyle{myAbbrlist}
	\begin{LTR}
		\Oldprintglossary[type=acronym]	
	\end{LTR}
	\clearpage
%	\clearpage
}%
\newcommand{\glsindex}[1]{%
\gls{#1}\expandafter\index\expandafter{\glsentryname{#1}}%
}
%%%%%% ================================================================================