前言

最近在写 表达与交流 这门课的论文。我肯定不会用Word的,毕竟太烦了。 但是Markdown+Typora的方案对于复杂的论文格式来说有点力不从心,遂作罢,选择LaTeX。

感谢Rei Suzunami自制的Overleaf模板!

listing+minted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
\usepackage{minted}% 语法高亮和代码样式设置方面更加强大和灵活
\usepackage{listings}% 引入listings包,用于在文档中插入代码,并可自定义代码样式
\usepackage{xcolor}

\begin{document}

\begin{listing}[htb]
\caption{判断质数}
\label{code:2}
\begin{minted}{cpp}
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d",a+b);
return 0;
}
\end{minted}
\end{listing}

\end{document}

但是没有行号,而且无法进行自动换行。也可能我设置的不对。遂作罢。

listings

网上查到的教程,大多都是使用listings而非listing

参考文章:LaTeX listings 宏包使用说明 - 知乎

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
\usepackage{listings}
\usepackage{xcolor}
\setmonofont{JetBrains Mono}

\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray}\ttfamily,
stringstyle=\color{codepurple},
basicstyle=\footnotesize\ttfamily,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left, %行号
stepnumber=5, %每五行标号一次
numbersep=5pt,
showspaces=false,
showstringspaces=false, %去掉空格时产生的下划的空格标志, 设置为true则出现
showtabs=false,
tabsize=2,
xleftmargin=1em %整体距离左边边距一个字符
}

\begin{document}

\begin{lstlisting}[language=Python]
import numpy as np

def incmatrix(genl1,genl2):
m = len(genl1)
n = len(genl2)
M = None #to become the incidence matrix
VT = np.zeros((n*m,1), int) #dummy variable

#compute the bitwise xor matrix
M1 = bitxormatrix(genl1)
M2 = np.triu(bitxormatrix(genl2),1)

for i in range(m-1):
for j in range(i+1, m):
[r,c] = np.where(M2 == M1[i,j])
for k in range(len(r)):
VT[(i)*n + r[k]] = 1;
VT[(i)*n + c[k]] = 1;
VT[(j)*n + r[k]] = 1;
VT[(j)*n + c[k]] = 1;

if M is None:
M = np.copy(VT)
else:
M = np.concatenate((M, VT), 1)

VT = np.zeros((n*m,1), int)

return M
\end{lstlisting}

\end{document}

效果很好,前面可以显示行号,并且有最低限度的代码高亮。唯一的小缺点是行号会超出正文的宽度。现在在研究如何调整边距。

将相关注释写在上面了。

RISC-V高亮方案

已联系维护者 Jobst Hoffmann ,等待进一步的交流与改进!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
\lstdefinelanguage[riscv]{Assembler}
{morekeywords={add,addi,addw,addiw,sub,subw,and,andi,or,ori,xor,xori,%
sll,slli,sllw,slliw,srl,srli,srlw,srliw,sra,srai,sraw,sraiw,%
slt,slti,sltu,sltiu,lui,auipc,jal,jalr,beq,bne,blt,bge,bltu,bgeu,%
lb,lh,lw,ld,lbu,lhu,lwu,sb,sh,sw,sd,fence,fence.i,ecall,ebreak,%
csrrw,csrrs,csrrc,csrrwi,csrrsi,csrrci,mul,mulh,mulhsu,mulhu,%
mulw,div,divw,divu,divuw,rem,remw,remu,remuw,lr.w,sc.w,lr.d,sc.d,%
amoswap.w,amoadd.w,amoxor.w,amoand.w,amoor.w,amomin.w,amomax.w,%
amominu.w,amomaxu.w,amoswap.d,amoadd.d,amoxor.d,amoand.d,amoor.d,%
amomin.d,amomax.d,amominu.d,amomaxu.d,flw,fld,fsw,fsd,fmadd.s,%
fmsub.s,fnmsub.s,fnmadd.s,fadd.s,fsub.s,fmul.s,fdiv.s,fsqrt.s,%
fsgnj.s,fsgnjn.s,fsgnjx.s,fmin.s,fmax.s,fcvt.w.s,fcvt.wu.s,%
fmv.x.w,feq.s,flt.s,fle.s,fclass.s,fcvt.s.w,fcvt.s.wu,fmv.w.x,%
fmadd.d,fmsub.d,fnmsub.d,fnmadd.d,fadd.d,fsub.d,fmul.d,fdiv.d,%
fsqrt.d,fsgnj.d,fsgnjn.d,fsgnjx.d,fmin.d,fmax.d,fcvt.s.d,fcvt.d.s,%
feq.d,flt.d,fle.d,fclass.d,fcvt.w.d,fcvt.wu.d,fcvt.d.w,fcvt.d.wu,%
fcvt.l.s,fcvt.lu.s,fcvt.s.l,fcvt.s.lu,fcvt.l.d,fcvt.lu.d,%
fmv.x.d,fcvt.d.l,fcvt.d.lu,fmv.d.x,%
nop,NOP,ret},%
morekeywords=[2]{.align,.balign,.p2align,.ascii,.asciiz,.string,.byte,%
.half,.hword,.short,.word,.long,.dword,.quad,.octa,.float,.double,%
.space,.zero,.fill,.skip,.org,.set,.equ,.equiv,.eqv,.macro,.endm,%
.rept,.endr,.if,.ifdef,.ifndef,.else,.endif,.include,.globl,.global,%
.weak,.local,.extern,.type,.size,.section,.text,.data,.rodata,.bss,%
.file,.line,.loc,.cfi_startproc,.cfi_endproc,.cfi_def_cfa_offset,%
.cfi_offset,.option,.attribute,.insn},%
morekeywords=[3]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,%
21,22,23,24,25,26,27,28,29,30,31,32,63,64,127,128,255,256,511,%
512,1023,1024,2047,2048,4095,4096},%
morekeywords=[4]{x0,zero,x1,ra,x2,sp,x3,gp,x4,tp,x5,t0,x6,t1,x7,t2,%
x8,s0,fp,x9,s1,x10,a0,x11,a1,x12,a2,x13,a3,x14,a4,x15,a5,%
x16,a6,x17,a7,x18,s2,x19,s3,x20,s4,x21,s5,x22,s6,x23,s7,%
x24,s8,x25,s9,x26,s10,x27,s11,x28,t3,x29,t4,x30,t5,x31,t6,%
f0,ft0,f1,ft1,f2,ft2,f3,ft3,f4,ft4,f5,ft5,f6,ft6,f7,ft7,%
f8,fs0,f9,fs1,f10,fa0,f11,fa1,f12,fa2,f13,fa3,f14,fa4,f15,fa5,%
f16,fa6,f17,fa7,f18,fs2,f19,fs3,f20,fs4,f21,fs5,f22,fs6,f23,fs7,%
f24,fs8,f25,fs9,f26,fs10,f27,fs11,f28,ft8,f29,ft9,f30,ft10,f31,ft11},
morekeywords=[5]{IF,ID,EX,MEM,WB,IM,REG,ALU,DM},
alsoletter=.,alsodigit=?,%
sensitive=f,%
morestring=[b]",%
morestring=[b]',%
morecomment=[l]\#
}[keywords,comments,strings]

\lstdefinestyle{riscvstyle}{
language=[riscv]Assembler,
keywordstyle=\color{blue},
keywordstyle=[2]\color{teal},
keywordstyle=[3]\color{red},
keywordstyle=[4]\color{orange!70!black},
keywordstyle=[5]\color{purple},
keywordstyle=[6]\color{cyan!80!black},
commentstyle=\color{green!50!black}\itshape,
literate=
*{|}{{{\textcolor{cyan!80!black}{\textbf{|}}}}}1
{0(}{{{\color{red}0}{\color{black}(}}}2
{1(}{{{\color{red}1}{\color{black}(}}}2
{2(}{{{\color{red}2}{\color{black}(}}}2
{3(}{{{\color{red}3}{\color{black}(}}}2
{4(}{{{\color{red}4}{\color{black}(}}}2
{5(}{{{\color{red}5}{\color{black}(}}}2
{6(}{{{\color{red}6}{\color{black}(}}}2
{7(}{{{\color{red}7}{\color{black}(}}}2
{8(}{{{\color{red}8}{\color{black}(}}}2
{9(}{{{\color{red}9}{\color{black}(}}}2
{\ 0\ }{{{\color{red}\ 0 \ }}}3
{\ 1\ }{{{\color{red}\ 1 \ }}}3
{\ 2\ }{{{\color{red}\ 2 \ }}}3
{\ 3\ }{{{\color{red}\ 3 \ }}}3
{\ 4\ }{{{\color{red}\ 4 \ }}}3
{\ 5\ }{{{\color{red}\ 5 \ }}}3
{\ 6\ }{{{\color{red}\ 6 \ }}}3
{\ 7\ }{{{\color{red}\ 7 \ }}}3
{\ 8\ }{{{\color{red}\ 8 \ }}}3
{\ 9\ }{{{\color{red}\ 9 \ }}}3
{\ 0}{{{\color{red}\ 0}}}2
{\ 1}{{{\color{red}\ 1}}}2
{\ 2}{{{\color{red}\ 2}}}2
{\ 3}{{{\color{red}\ 3}}}2
{\ 4}{{{\color{red}\ 4}}}2
{\ 5}{{{\color{red}\ 5}}}2
{\ 6}{{{\color{red}\ 6}}}2
{\ 7}{{{\color{red}\ 7}}}2
{\ 8}{{{\color{red}\ 8}}}2
{\ 9}{{{\color{red}\ 9}}}2
{-1}{{{\color{red}-1}}}2
{-2}{{{\color{red}-2}}}2
{-3}{{{\color{red}-3}}}2
{-4}{{{\color{red}-4}}}2
{-5}{{{\color{red}-5}}}2
{-6}{{{\color{red}-6}}}2
{-7}{{{\color{red}-7}}}2
{-8}{{{\color{red}-8}}}2
{-9}{{{\color{red}-9}}}2
}
高亮效果