listings技巧
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技巧的更多相关文章
- KEIL MDK 5.12帮你快速建工程模板的技巧
KEIL 5帮你快速建工程模板的技巧 本人使用keil mdk 5.12有一段时间了,发现keil mdk 5.12里面驱动库比较方便.这个新功能可以节省我们的时间,也可以让初学者能尽快上手和掌握这个 ...
- 探究javascript对象和数组的异同,及函数变量缓存技巧
javascript中最经典也最受非议的一句话就是:javascript中一切皆是对象.这篇重点要提到的,就是任何jser都不陌生的Object和Array. 有段时间曾经很诧异,到底两种数据类型用来 ...
- 前端极易被误导的css选择器权重计算及css内联样式的妙用技巧
记得大学时候,专业课的网页设计书籍里面讲过css选择器权重的计算:id是100,class是10,html标签是5等等,然后全部加起来的和进行比较... 我只想说:真是误人子弟,害人不浅! 最近,在前 ...
- 前端网络、JavaScript优化以及开发小技巧
一.网络优化 YSlow有23条规则,中文可以参考这里.这几十条规则最主要是在做消除或减少不必要的网络延迟,将需要传输的数据压缩至最少. 1)合并压缩CSS.JavaScript.图片,静态资源CDN ...
- 工欲善其事,必先利其器 之 VS2013全攻略(安装,技巧,快捷键,插件)!
如有需要WPF工具的朋友可以移步 工欲善其事,必先利其器 之 WPF篇: 随着开发轨迹来看高效WPF开发的工具和技巧 之前一篇<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATI ...
- 15个关于Chrome的开发必备小技巧[译]
谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...
- Git小技巧 - 指令别名及使用Beyond Compare作为差异比较工具
前言 本文主要写给使用命令行来操作Git的用户,用于提高Git使用的效率.至于使用命令还是GUI(Tortoise Git或VS的Git插件)就不在此讨论了,大家根据自己的的喜好选择就好.我个人是比较 ...
- 【工具使用】mac电脑使用技巧
本文地址 分享提纲: 1. mac命令行和finder的交互 2. 一些mac的插件 3. 一些开发的配置 1.mac命令行和findder交互 1)命令行中打开当前文件夹: o ...
- Function.prototype.toString 的使用技巧
Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...
随机推荐
- Redis(六)Lua脚本的支持
Redis为什么需要Lua脚本的支持 当应用需要Redis完成一些Redis命令不支持的特性时,要么扩展Redis client或者更甚至编写c扩展Redis server.这都大大造成了应用的实现的 ...
- 《STL源码剖析》——Array
array array本身内容较少,日常使用也不是很多,里面也没有很高深的技巧 1 array的基本架构 了解array的架构需要一个额外的语法知识: int a[100]; int [100]b; ...
- node、npm、gulp安装
1.先安装node.js ,官网下载地址:https://nodejs.org/en/ 2.安装完node之后,npm自动就安装了.可以直接在visual studio code 通过命令查看 nod ...
- U9 DLL读取配制文件
在网上大多数的例子都是如何访问程序运行的目录下的配制文件,但是U9BP里要将DLL放到生产环境,我并不知道生产环境运行的目录,所以只能DLL读取当前目录XML. 过下面几个方法都可以拿到程序所在的文件 ...
- 理解了quote和 symbol-list的 关系
'x === (quote x) '(x) === (list 'x)
- python 动态创建变量 获取变量名
参考链接:https://www.cnblogs.com/technologylife/p/9211324.html 参考链接(未)(使用inspect 获取变量名):https://blog.csd ...
- 纯C语言实现线性链表
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode{ ElemT ...
- AD活动目录操作软件设计节选
AD操作解决二级目录用户删除问题 public string DeleteADAccountOU(string commonName, string ouName) { DataTable dt = ...
- Promise 封装 ajax
Promise 封装ajax 成链式结构: var url = 'http'; function(method, url) { return new Promise(function(res, ret ...
- day 67
目录 Vue框架 Vue的简介 Vue的使用 插值表达式 文本指令 事件指令 属性指令 Vue框架 Vue的简介 Vue是一套构建用户界面的框架,与Angular.React两个框架相比,Vue吸取了 ...