2023年7月18日发(作者:)
Latex中插⼊C语⾔代码Latex是⼀个⽂本排版的语⾔,能排版出各种我们想要的效果。⽽且⽤代码排版的优点是易于修改板式,因此在⽂本内容的排版时,Latex应⽤⼗分⼴泛。当我们需要在Latex中插⼊代码时,就需要⽤到 usepackage{listings} 宏包。例如插⼊⼀个简单的C语⾔代码#include int main(int argc, char ** argv){ printf("Hello, world!n"); return 0;}要将上⾯ Hello,world! 这段C语⾔代码⽤Latex实现排版的效果,Latex的脚本如下documentclass{article}usepackage{listings}usepackage{xcolor} %代码着⾊宏包usepackage{CJK} %显⽰中⽂宏包lstset{ basicstyle=tt, %⾏号 numbers=left, rulesepcolor=color{red!20!green!20!blue!20}, escapeinside=``, xleftmargin=2em,xrightmargin=2em, aboveskip=1em, %背景框 framexleftmargin=1.5mm, frame=shadowbox, %背景⾊ backgroundcolor=color[RGB]{245,245,244}, %样式 keywordstyle=color{blue}bfseries, identifierstyle=bf, numberstyle=color[RGB]{0,192,192}, commentstyle=itcolor[RGB]{96,96,96}, stringstyle=rmfamilyslshapecolor[RGB]{128,0,0}, %显⽰空格 showstringspaces=false}begin{document}begin{CJK*}{GBK}{song}lstset{language=C}begin{lstlisting} %插⼊要显⽰的代码#include int main(int argc, char ** argv){ /*`打印`Hello,world*/ printf("Hello, world!n"); return 0;}end{lstlisting}end{CJK*}end{document}上⾯的Latex脚本可以显⽰出C语⾔中的注释(包括中⽂注释),代码着⾊,并添加了代码⾏号。效果如下 参考:[1][2] Latex论坛,[3] 提取码: ac4n
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689640469a273009.html
评论列表(0条)