当前位置 - 股票行情交易網 - 股票行情 - 如何用LATEX優雅的打出代碼

如何用LATEX優雅的打出代碼

python

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{pythonhighlight}[2011/09/19

python code highlighting; provided by Olivier Verdier

<olivier.verdier@gmail.com>]

\RequirePackage{listings}

\RequirePackage{xcolor}

\renewcommand*{\lstlistlistingname}{Code Listings}

\renewcommand*{\lstlistingname}{Code Listing}

\definecolor{gray}{gray}{0.5}

\colorlet{commentcolour}{green!50!black}

\colorlet{stringcolour}{red!60!black}

\colorlet{keywordcolour}{magenta!90!black}

\colorlet{exceptioncolour}{yellow!50!red}

\colorlet{commandcolour}{blue!60!black}

\colorlet{numpycolour}{blue!60!green}

\colorlet{literatecolour}{magenta!90!black}

\colorlet{promptcolour}{green!50!black}

\colorlet{specmethodcolour}{violet}

\newcommand*{\framemargin}{3ex}

\newcommand*{\literatecolour}{\textcolor{literatecolour}}

\newcommand*{\pythonprompt}{\textcolor{promptcolour}{{>}{>}{>}}}

\lstdefinestyle{mypython}{

%\lstset{

%keepspaces=true,

language=python,

showtabs=true,

tab=,

tabsize=2,

basicstyle=\ttfamily\footnotesize,%\setstretch{.5},

stringstyle=\color{stringcolour},

showstringspaces=false,

alsoletter={1234567890},

otherkeywords={\%, \}, \{, \&, \|},

keywordstyle=\color{keywordcolour}\bfseries,

emph={and,break,class,continue,def,yield,del,elif ,else,%

except,exec,finally,for,from,global,if,import,in,%

lambda,not,or,pass,print,raise,return,try,while,assert,with},

emphstyle=\color{blue}\bfseries,

emph={[2]True, False, None},

emphstyle=[2]\color{keywordcolour},

emph={[3]object,type,isinstance,copy,deepcopy,zip,enumerate,reversed,list,len,dict,tuple,xrange,append,execfile,real,imag,reduce,str,repr},

emphstyle=[3]\color{commandcolour},

emph={Exception,NameError,IndexError,SyntaxError,TypeError,ValueError,OverflowError,ZeroDivisionError},

emphstyle=\color{exceptioncolour}\bfseries,

%upquote=true,

morecomment=[s]{"""}{"""},

commentstyle=\color{commentcolour}\slshape,

%emph={[4]1, 2, 3, 4, 5, 6, 7, 8, 9, 0},

emph={[4]ode,

fsolve, sqrt, exp, sin, cos,arctan, arctan2, arccos, pi, array, norm,

solve, dot, arange, , isscalar, max, sum, flatten, shape, reshape, find,

any, all, abs, plot, linspace, legend, quad, polyval,polyfit, hstack,

concatenate,vstack,column_stack,empty,zeros,ones,rand,vander,grid,pcolor,eig,eigs,eigvals,svd,qr,tan,det,logspace,roll,min,mean,cumsum,cumprod,diff,vectorize,lstsq,cla,eye,xlabel,ylabel,squeeze},

emphstyle=[4]\color{numpycolour},

emph={[5]__init__,__add__,__mul__,__div__,__sub__,__call__,__getitem__,__setitem__,__eq__,__ne__,__nonzero__,__rmul__,__radd__,__repr__,__str__,__get__,__truediv__,__pow__,__name__,__future__,__all__},

emphstyle=[5]\color{specmethodcolour},

emph={[6]assert,yield},

emphstyle=[6]\color{keywordcolour}\bfseries,

emph={[7]range},

emphstyle={[7]\color{keywordcolour}\bfseries},

% emph={[7]self},

% emphstyle=[7]\bfseries,

literate=*%

{:}{{\literatecolour:}}{1}%

{=}{{\literatecolour=}}{1}%

{-}{{\literatecolour-}}{1}%

{+}{{\literatecolour+}}{1}%

{*}{{\literatecolour*}}{1}%

{**}{{\literatecolour{**}}}2%

{/}{{\literatecolour/}}{1}%

{//}{{\literatecolour{//}}}2%

{!}{{\literatecolour!}}{1}%

%{(}{{\literatecolour(}}{1}%

%{)}{{\literatecolour)}}{1}%

{[}{{\literatecolour[}}{1}%

{]}{{\literatecolour]}}{1}%

{<}{{\literatecolour<}}{1}%

{>}{{\literatecolour>}}{1}%

{>>>}{\pythonprompt}{3}%

,%

%aboveskip=.5ex,

frame=trbl,

%frameround=tttt,

%framesep=.3ex,

rulecolor=\color{black!40},

%framexleftmargin=\framemargin,

%framextopmargin=.1ex,

%framexbottommargin=.1ex,

%framexrightmargin=\framemargin,

%framexleftmargin=1mm, framextopmargin=1mm, frame=shadowbox, rulesepcolor=\color{blue},#1

%frame=tb,

backgroundcolor=\color{white},

breakindent=.5\textwidth,frame=single,breaklines=true%

%}

}

\newcommand*{\inputpython}[3]{\lstinputlisting[firstline=#2,lastline=#3,firstnumber=#2,frame=single,breakindent=.5\textwidth,frame=single,breaklines=true,style=mypython]{#1}}

\lstnewenvironment{python}[1][]{\lstset{style=mypython}}{}

\lstdefinestyle{mypythoninline}{

style=mypython,%

basicstyle=\ttfamily,%

keywordstyle=\color{keywordcolour},%

emphstyle={[7]\color{keywordcolour}},%

emphstyle=\color{exceptioncolour},%

literate=*%

{:}{{\literatecolour:}}{2}%

{=}{{\literatecolour=}}{2}%

{-}{{\literatecolour-}}{2}%

{+}{{\literatecolour+}}{2}%

{*}{{\literatecolour*}}2%

{**}{{\literatecolour{**}}}3%

{/}{{\literatecolour/}}{2}%

{//}{{\literatecolour{//}}}{2}%

{!}{{\literatecolour!}}{2}%

%{(}{{\literatecolour(}}{2}%

%{)}{{\literatecolour)}}{2}%

{[}{{\literatecolour[}}{2}%

{]}{{\literatecolour]}}{2}%

{<}{{\literatecolour<}}{2}%

{<=}{{\literatecolour{<=}}}3%

{>}{{\literatecolour>}}{2}%

{>=}{{\literatecolour{>=}}}3%

{==}{{\literatecolour{==}}}3%

{!=}{{\literatecolour{!=}}}3%

{+=}{{\literatecolour{+=}}}3%

{-=}{{\literatecolour{-=}}}3%

{*=}{{\literatecolour{*=}}}3%

{/=}{{\literatecolour{/=}}}3%

%% emphstyle=\color{blue},%

}

\newcommand*{\pyth}{\lstinline[style=mypythoninline]}

使用時:

\begin{python}

myString='zhangxiaoshuai is shabi'

print(myString)

\end{python}

原網址:/olivierverdier/python-latex-highlighting

2C/C++

Wiki

/questions/2724760/how-to-write-c-in-latex

mentstyle=\color{olive},

directivestyle=\color{blue},

extendedchars=false,

%

frame=single,%shadowbox

framerule=0pt,

keywordstyle=\color{blue}\bfseries,

morekeywords={*,define,*,include...},

numbersep=5pt,

rulesepcolor=\color{red!20!green!20!blue!20},

showspaces=false,

showstringspaces=false,

showtabs=false,

stepnumber=2,

stringstyle=\color{purple},

tabsize=4,

title=\lstname

}

--------------

\usepackage{listings}

\lstset{language=C++}%這條命令可以讓LaTeX排版時將C++鍵字突出顯示

\lstset{breaklines}%這條命令可以讓LaTeX自動將長的代碼行換行排版

\lstset{extendedchars=false}%這壹條命令可以解決代碼跨頁時,章節標題,頁眉等漢字不顯示的問題

\begin{lstlisting}

%paste your C++ code here

\end{lstlisting}

\begin{algorithm}[h]

\caption{An example for format For \& While Loop in Algorithm}

\begin{algorithmic}[1]

\FOR{each $i\in [1,9]$}

\STATE initialize a tree $T_{i}$ with only a leaf (the root);\\

\STATE $T=T\bigcup T_{i};$\\

\ENDFOR

\FORALL {$c$ such that $c\in RecentMBatch(E_{n-1})$} \label{code:TrainBase:getc}

\STATE $T=T \cup PosSample(c)$; \label{code:TrainBase:pos}

\ENDFOR;

\FOR{$i=1$; $i<n$; $i++$ }

\STATE $//$ Your source here;

\ENDFOR

\FOR{$i=1$ to $n$}

\STATE $//$ Your source here;

\ENDFOR

\STATE $//$ Reusing recent base classifiers. \label{code:recentStart}

\WHILE {$(|E_n| \leq L_1 )and( D \neq \phi)$}

\STATE Selecting the most recent classifier $c_i$ from $D$;

\STATE $D=D-c_i$;

\STATE $E_n=E_n+c_i$;

\ENDWHILE \label{code:recentEnd}

\end{algorithmic}

\end{algorithm}