1. \lstdefinestyle

参考 https://blog.csdn.net/ProgramChangesWorld/article/details/52142313

我们在使用listings的时候,需要设置语言和样式。用\lstset会设置全局的变量,如果我们文章中有多种代码,那么就需要\lstdefinestyle,设置多种样式,在使用的时候选择对应的样式。

% system=ubuntu
%soft=Tex Live2015
% complie=XeLaTeX
\documentclass[a4paper,UTF8]{article}
\usepackage{listings}
\usepackage{ctex}
\usepackage{color}
\definecolor{keywordcolor}{rgb}{0.8,0.1,0.5}
\definecolor{webgreen}{rgb}{,.,}
\definecolor{bgcolor}{rgb}{0.92,0.92,0.92} \lstdefinestyle{styleJ}{
language=[AspectJ]Java,
keywordstyle=\color{keywordcolor}\bfseries,
commentstyle=\color{blue} \textit,
showstringspaces=false,
numbers=left,
numberstyle=\small
}
\lstdefinestyle{styleP}{
language=Python,
numbers=right,
frame=single,
numberstyle=\small ,
}
\begin{document}
\begin{lstlisting}[style=styleJ]
public int sum(int n){
int sum = ;
for(int i=;i<n;i++){ //开始的
sum += i;
}
return sum;
}
\end{lstlisting} \begin{lstlisting}[style=styleP]
def fun():
print('你好,世界') #我是注释
\end{lstlisting} \end{document}

可以看到使用lstdefinestyle定义了两个样式,styleJ和styleP,分别是java和python的样式,在使用lstlisting环境的时候调设置了这两个样式。

如果不想把代码放在.tex文件里,也可以把代码放在单独的文件,然后使用下面的命令即可:

\lstinputlisting[style=styleJ]{code.java}

2. \listings样式1

参考 https://tex.stackexchange.com/questions/68091/how-do-i-add-syntax-coloring-to-my-c-source-code-in-beamer

\documentclass{beamer}
\setbeamercovered{transparent}
\usepackage{listings} \begin{document} % Using typewriter font: \ttfamily inside \lstset
\begin{frame}[fragile]
\frametitle{Inserting source code}
\lstset{language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
morecomment=[l][\color{magenta}]{\#}
}
\begin{lstlisting}
#include<stdio.h>
#include<iostream>
// A comment
int main(void)
{
printf("Hello World\n");
return ;
}
\end{lstlisting}
\end{frame} \begin{frame}[fragile]
\frametitle{Inserting source code without setting typewriter}
\lstset{language=C++,
keywordstyle=\color{blue},
stringstyle=\color{red},
commentstyle=\color{green},
morecomment=[l][\color{magenta}]{\#}
}
\begin{lstlisting}
#include<stdio.h>
#include<iostream>
// A comment
int main(void)
{
printf("Hello World\n");
return ;
}
\end{lstlisting}
\end{frame}
\end{document}

第一种使用了\ttfamily,这个是一种打印机字体。

参考 https://www.tug.org/pracjourn/2006-1/schmidt/schmidt.pdf

https://tug.org/FontCatalogue/typewriterfonts.html

\ttfamilyselects a monospaced (“typewriter”) font family

3. listings样式2

参考 https://tex.stackexchange.com/questions/409705/c-code-change-the-font

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings} \colorlet{mygray}{black!}
\colorlet{mygreen}{green!!blue}
\colorlet{mymauve}{red!!blue} \lstset{
backgroundcolor=\color{gray!},
basicstyle=\ttfamily,
columns=fullflexible,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{mygreen},
extendedchars=true,
frame=single,
keepspaces=true,
keywordstyle=\color{blue},
language=c++,
numbers=none,
numbersep=5pt,
numberstyle=\tiny\color{blue},
rulecolor=\color{mygray},
showspaces=false,
showtabs=false,
stepnumber=,
stringstyle=\color{mymauve},
tabsize=,
title=\lstname
} \begin{document} \begin{lstlisting}
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h> int main()
{
char *outText; tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit();
} // Open input image with leptonica library
Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
api->SetImage(image);
// Get OCR result
outText = api->GetUTF8Text();
printf("OCR output:\n%s", outText); // Destroy used object and release memory
api->End();
delete [] outText;
pixDestroy(&image); return ;
}
\end{lstlisting} \end{document}

listings技巧的更多相关文章

  1. KEIL MDK 5.12帮你快速建工程模板的技巧

    KEIL 5帮你快速建工程模板的技巧 本人使用keil mdk 5.12有一段时间了,发现keil mdk 5.12里面驱动库比较方便.这个新功能可以节省我们的时间,也可以让初学者能尽快上手和掌握这个 ...

  2. 探究javascript对象和数组的异同,及函数变量缓存技巧

    javascript中最经典也最受非议的一句话就是:javascript中一切皆是对象.这篇重点要提到的,就是任何jser都不陌生的Object和Array. 有段时间曾经很诧异,到底两种数据类型用来 ...

  3. 前端极易被误导的css选择器权重计算及css内联样式的妙用技巧

    记得大学时候,专业课的网页设计书籍里面讲过css选择器权重的计算:id是100,class是10,html标签是5等等,然后全部加起来的和进行比较... 我只想说:真是误人子弟,害人不浅! 最近,在前 ...

  4. 前端网络、JavaScript优化以及开发小技巧

    一.网络优化 YSlow有23条规则,中文可以参考这里.这几十条规则最主要是在做消除或减少不必要的网络延迟,将需要传输的数据压缩至最少. 1)合并压缩CSS.JavaScript.图片,静态资源CDN ...

  5. 工欲善其事,必先利其器 之 VS2013全攻略(安装,技巧,快捷键,插件)!

    如有需要WPF工具的朋友可以移步 工欲善其事,必先利其器 之 WPF篇: 随着开发轨迹来看高效WPF开发的工具和技巧 之前一篇<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATI ...

  6. 15个关于Chrome的开发必备小技巧[译]

    谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...

  7. Git小技巧 - 指令别名及使用Beyond Compare作为差异比较工具

    前言 本文主要写给使用命令行来操作Git的用户,用于提高Git使用的效率.至于使用命令还是GUI(Tortoise Git或VS的Git插件)就不在此讨论了,大家根据自己的的喜好选择就好.我个人是比较 ...

  8. 【工具使用】mac电脑使用技巧

    本文地址 分享提纲: 1.  mac命令行和finder的交互 2. 一些mac的插件 3. 一些开发的配置 1.mac命令行和findder交互           1)命令行中打开当前文件夹: o ...

  9. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

随机推荐

  1. python实现ldap接入

    需要提前安装python-ldap模块 python接入ldap其实分了几个步骤: 1.使用一个管理员账户登陆到ldap 2.使用一个字段值是唯一的字段,去搜索到要验证用户的DN值(ldap搜索到的单 ...

  2. Java实习生入职测试

    网络上一度流行的Java实习生入职测试题,可以看看. 1.String类为什么是final的. 2.JDK8的HashMap的源码,实现原理,底层结构 3.反射中,Class.forName和clas ...

  3. 为python脚本增加命令行参数

    from argparse import ArgumentParser p = ArgumentParser() p.add_argument('-b', '--body', help='Return ...

  4. 解决ubuntu安装ssh服务无法打开解析包问题

    Windows下做Linux开发需要SSH强大功能的支持.安装SSH的过程会出现了很多问题,看完这篇文章可以让你少走些弯路,PS:折腾一下午的成果. Ubuntu的apt-get工具的牛逼之处简直无人 ...

  5. Excel单元格锁定及解锁

    Excel VBA 宏 学习使用: 一.工作表单元格的锁定: 1.选择需要锁定的单元格. 2.鼠标右键----设置单元格格式. 3.设置  “保护”--锁定 -- 确定. 4.回到表头,[审阅]--- ...

  6. maven仓库报错 sqljdbc4、ojdbc6、tomcat-jdbc-8.5.14

    报错:Cannot resolve com.microsoft.sqlserver:sqljdbc4:4.0  和  Missing artifact com.microsoft.sqlserver: ...

  7. ansible自动化运维03

    ansible自动化运维常用模块 常用模块实现的功能:安装软件包:修改配置文件:创建程序用户组:创建目录,并修改所属和权限:挂载:启动服务:测试. command模块: shell模块: 注意:com ...

  8. ubuntu 18.04 设置静态ip方法

    1. 前言 本教程将会演示如何设置Ubuntu16.04 Server版和Ubuntu18.04 Server版系统的静态固定IP地址. 2. 确认你要修改的网卡号 先确认你要修改的网卡号,假设你的服 ...

  9. matplotlib---画等高线

    contour - 绘制等高线 mp.contour(x, y, z, 等高线条数,colors=颜色, linewidth=线宽)#等高线绘制 contourf - 填充等高线 mp.contour ...

  10. 使用Supervisor管理Django应用进程

    官方文档 1.安装 pip install supervisor 2.使用说明 2.1  查看默认配置 echo_supervisord_conf 一般情况下,不需要去修改默认配置,而是将默认配置重定 ...