一.代码高亮
解决方案一:minted宏包
\documentclass[a4paper]{ctexart}
\usepackage{minted}
\begin{document}
示例1
\begin{minted}{c++}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
示例2
\begin{minted}[mathescape,
linenos,
numbersep=5pt,
gobble=2,
frame=lines,
framesep=2mm]{csharp}
string title = "This is a Unicode π in the sky"
/*
Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the perimeter
of an $n$-sided regular polygon circumscribing a
circle of diameter $d$.
*/
const double pi = 3.1415926535
\end{minted}
\end{document}
解决方案二:listings宏包
\documentclass[a4paper]{ctexart}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{fontspec}
\newfontfamily\monaco{Monaco}
\setmonofont[Mapping={}]{Monaco} %英文引号之类的正常显示,相当于设置英文字体\setsansfont{Monaco} %设置英文字体 Monaco, Consolas, Fantasque Sans Mono
\setmainfont{Monaco} %设置英文字体
\setCJKmainfont{方正兰亭黑简体} %中文字体设置
%\setCJKsansfont{华康少女字体} %设置中文字体
%\setCJKmonofont{华康少女字体} %设置中文字体
% 全局设置
\lstset{columns=flexible,numbers=left,numberstyle=\tiny\monaco,basicstyle=\small\monaco,keywordstyle= \color{ blue!70},commentstyle=\color{red!50!green!50!blue!50}, frame=shadowbox, rulesepcolor= \color{ red!20!green!20!blue!20} }
\begin{document}
% 默认
\begin{ lstlisting}[ language=C]
int main(int argc, char ** argv)
{
printf("Hello world! \n");
return 0;
}
\end{ lstlisting}
% 添加边框
\begin{lstlisting}[language={[ANSI]C},
keywordstyle=\color{blue!70},
commentstyle=\color{red!50!green!50!blue!50},
frame=shadowbox,
numbers=left,
rulesepcolor=\color{red!20!green!20!blue!20}]
{
printf("Hello world! \n");
return 0;
}
\end{lstlisting}
\end{ document}
自定义配置
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\lstset{ %
backgroundcolor=\color{white}, % choose the background color
basicstyle=\footnotesize\ttfamily, % size of fonts used for the code
columns=fullflexible,
breaklines=true, % automatic line breaking only at whitespace
captionpos=b, % sets the caption-position to bottom
tabsize=4,
commentstyle=\color{mygreen}, % comment style
escapeinside={\%*}{*)}, % if you want to add LaTeX within your code
keywordstyle=\color{blue}, % keyword style
stringstyle=\color{mymauve}\ttfamily, % string literal style
frame=single,
rulesepcolor=\color{red!20!green!20!blue!20},
% identifierstyle=\color{red},
language=c++,
}
\lstset{ %
backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
basicstyle=\ttfamily, % the size of the fonts that are used for the code
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
captionpos=b, % sets the caption-position to bottom
commentstyle=\ttfamily\color{mygreen},
% comment style
deletekeywords={}, % if you want to delete keywords from the given language
escapeinside={}, % if you want to add LaTeX within your code
extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
frame=single, % adds a frame around the code
keepspaces=true, % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
keywordstyle=\color{blue}, % keyword style
language=C++, % the language of the code
morekeywords={}, % if you want to add more keywords to the set
numbers=left, % where to put the line-numbers; possible values are (none, left, right)
numbersep=5pt, % how far the line-numbers are from the code
numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
showstringspaces=false, % underline spaces within strings only
showtabs=false, % show tabs within strings adding particular underscores
stepnumber=1, % the step between two line-numbers. If it's 1, each line will be numbered
stringstyle=\color{mymauve}, % string literal style
tabsize=2, % sets default tabsize to 2 spaces
title=\lstname % show the filename of files included with \lstinputlisting; also try caption instead of title
}
二.伪代码
样式一
\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\begin{document}
\begin{algorithm}
\caption{Calculate $y = x^n$} %单独一栏标题
\label{alg1} %标签显示为:Algorithm 1
\begin{algorithmic} %开始algorithmic环境
\REQUIRE $n \geq 0 \vee x \neq 0$ %\REQUIRE显示Require:
\ENSURE $y = x^n$ %\ENSURE显示Ensure:
\STATE $y \gets 1$ %\gets和\leftarrow一样都是左箭头,建议用\gets
\IF{$n < 0$} %\IF{$xxx$}显示if xxx then
\STATE $X \gets 1 / x$ %每一行都要\STATE $xxx$
\STATE $N \gets -n$
\ELSE %\ELSE显示else
\STATE $X \gets x$
\STATE $N \gets n$
\ENDIF %\ENDIF显示end if
\WHILE{$N \neq 0$} %\WHILE{$xxx$}显示while xxx do
\IF{$N$ is even}
\STATE $X \gets X \times X$
\STATE $N \gets N / 2$
\ELSE[$N$ is odd] %ELSE[$xxx$]显示else {xxx}
\STATE $y \gets y \times X$
\STATE $N \gets N - 1$
\ENDIF
\ENDWHILE %%\ENDWHILE显示end while
\end{algorithmic}
\end{algorithm}
\end{document}
知乎-在 LaTeX 中排版算法(伪代码),如何正确缩进?
样式二
\documentclass{article}
\usepackage{xeCJK}
\usepackage[ruled]{algorithm2e}
\renewcommand{\algorithmcfname}{算法}
\begin{document}
\begin{algorithm}[H]
% \SetAlgoNoLine可以去掉竖线
\caption{How to write algorithms}
\KwIn{this text}
\KwOut{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\end{algorithm}
\end{document}
样式三
\documentclass{article}
\usepackage{xeCJK}
\usepackage[linesnumbered,boxed]{algorithm2e}
\begin{document}
\begin{algorithm}
\caption{identifyRowContext}
\KwIn{$r_i$ , $Backgrd(T_i)$=${T_1,T_2,\ldots,T_n}$ and similarity threshold $\theta_r$}
\KwOut{$con(r_i)$}
$con(r_i)= \Phi$\;
\For{$j=1;j \le n;j \ne i$}
{
float $maxSim=0$\;
$r^{maxSim}=null$\;
\While{not end of $T_j$}
{
compute Jaro($r_i,r_m$)($r_m \in T_j$)\;
\If{$(Jaro(r_i,r_m) \ge \theta _r) \wedge ((Jaro(r_i,r_m) \ge r^{maxSim}) $}
{
replace $r^maxSim$ with $r_m$\;
}
}
$con(r_i)=con(r_i) \cup {r^{maxSim}}$\;
}
return $con(r_i)$\;
\end{algorithm}
\end{document}
网友评论