自用的一套LaTeX文档模式备忘
这里记录一份自用的LaTeX文档模式以备忘。
环境:
操作系统:Ubuntu 18.04.3
编译方式:LuaLaTeX
目录结构:
目录说明:
1、database是一个项目存放目录,有多少项目就会有多少这样的文件夹。这里是名为综合资料的项目;
2、package是自定义LaTeX宏包、类的存放区;
3、resource是素材区,存放图片、音频、视频等;
4、tool_lib是工具区,主要是lua源代码、c/c++编写的链接库等。
主要文件源代码:
1、综合资料.tex
documentclass{book}%
%
% 注意宏包顺序,有可能会报错
usepackage{ctex}% 中文支持
usepackage{geometry}% 用于页面设置
usepackage[dvipsnames, svgnames, x11names]{xcolor} % 颜色支持
usepackage{graphics}% 图形支持
usepackage[colorlinks,linkcolor=blue]{hyperref}
usepackage{enumerate}% 枚举支持
usepackage{listings}% 代码显示支持
usepackage{tabularx}% 表格支持
usepackage{lakeside-cover}% 自定义封面包
%
% 设置为A4纸,并设置四周边距
geometry{a4paper, left = 3.17cm, right = 3.17cm, top = 2.54cm, bottom = 2.54cm}%
%
% 以下两行命令用于解决LuaLaTeX编译模式下listings宏包不能正常工作的问题
ewdimencht%
ewdimencdp%
%
% 代码显示样式设置
lstset{%
breaklines,% 自动换行
basicstyle=small,% 设置字体大小
frame=single,% 单线框
numbers=left,% 行号在左边
numberstyle= iny,% 行号字体大小
keywordstyle = color[RGB]{0, 0, 160},% 关键字颜色
commentstyle = color[RGB]{160, 160, 160},% 注释颜色
stringstyle = color[RGB]{0, 0, 255},% 字符串颜色
}%
%
setlength{parindent}{2em}% 缩进为两个字符宽度
%
hyphenpenalty = 100% 断字设置,值越大,断字越少。
%
% 与主文件不在同一目录时,在这里以及后面的include命令中都要指明路径
includeonly{
ubuntu/ubuntu,%
git/git,%
}
%
% ------------------ 开始 -------------------
egin{document}%
% ------------------ 封面 -------------------
egin{titlepage}%
ShowCover[%
version=1.0.0,%
author = 搜集整理:陆巍,%
title1 = LAKESIDE ROBOT STUDIO,%
title2 = 综合资料集,%
]{December 2, 2019}{Honeydew2}{../resource/cover/hongshan02.jpg}%
end{titlepage}
%
% ------------------ 前言 -------------------
frontmatter% 关闭章节序号,页码使用罗马数字
estoregeometry% 恢复导言区指定的页面布局。
pagecolor{white}% 页面颜色
centerline{fseriesLarge 前quad 言
ormalsize
ormalfont}%
quadpar
略……%
%
%
% ------------------ 目录 -------------------
ableofcontents % 生成目录
%
%
% ------------------ 正文 -------------------
mainmatter%
%
include{ubuntu/ubuntu}%
%
include{git/git}%
%
end{document}%
代码中已经有详细说明,不再赘述。
2、lakeside-cover.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% lakeside-cover.sty
%
% 封面包
% 生成书本封面。
%
% Copyright: free
% Author: 陆巍
% Email: weissman.lu@outlook.com
% Version: 1.0.0
% Date: 2019-12-02
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%
% 下面两行是声明类的固定格式
NeedsTeXFormat{LaTeX2e}[2007/10/19]%
ProvidesPackage{lakeside-cover}[2019/12/02]%
%
%
RequirePackage{overpic}% 覆盖图片支持
RequirePackage{lakeside-tools}%
%
%
%
%
% ====================== 定义区 ======================
%
% 封面定义命令
% 页面颜色、图片暂时还不能使用lua命令去赋值
% 封面图片尺寸为800×480像素
ewcommand{ShowCover}[4][]{%
pagecolor{#3}
ewgeometry{left = 0cm, top = 2cm, bottom = 0cm}%
% 封面上层
egin{flushright}
HugeLaTeX\[0.7cm]%
Largecolor{gray} Version GetKey{#1}{version}{0.0.0} \[0.2cm]
#2
ormalsizecolor{black}\[0.2cm]%
LAKESIDE ROBOT STUDIO\[0.2cm]%
GetKey{#1}{author}{作者:陆巍}\%
end{flushright}
% 在图片上加文字
egin{overpic}[width = 21cm]{#4}%
color{white}put(70, 57){ extit{Hongshan Park, Anshun, Guizhou}}%
end{overpic}%
egin{flushright}%
quad\[1.0cm]%
%常规字体设置对英文无效
font
m="Latin Modern Roman Demi" at 25pt
m GetKey{#1}{title1}{test}
ormalsize\%
heitiHuge GetKey{#1}{title2}{test}\[1.5cm]%
end{flushright}%
hspace{0cm}fcolorbox{gray}{gray}{%
egin{minipage}[c][2cm][s]{5cm}%
hfill%
end{minipage}%
}%
hspace{0.1cm}fcolorbox{blue!40!black}{blue!40!black}{%
egin{minipage}[l][2cm][c]{15.4cm}%
color{white}
ormalsize%
extit{mbox{qquad} https://gitee.com/lakesiderobot}\%
mbox{qquad} extit{Email: weissman.lu@outlook.com}%
end{minipage}%
}%
enewcommand{aselinestretch}{1.0}%
}%
此LaTeX类文件用于生成文档封面。
3、lakeside-tools.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% lakeside-tools.sty
%
% 工具包文件。
% 由各种小工具组成的命令集。
%
% Copyright: free
% Author: 陆巍
% Email: weissman.lu@outlook.com
% Version: 1.0.0
% Date: 2019-12-02
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%
% ====================== 环境设置区 ======================
%
% 下面两行是声明类的固定格式
NeedsTeXFormat{LaTeX2e}[2007/10/19]%
ProvidesPackage{lakeside-tools}[2019/12/02]%
%
% 把以下三个库加入xcolor中(调用时)
PassOptionsToPackage{dvipsnames, svgnames, x11names}{xcolor}%
% 需求的宏包
RequirePackage{luacode}% lua支持
RequirePackage{xcolor}%
%
%
%
%
% ====================== 定义区 ======================
%
%
ewcommand{GetKey}[3]{%
luaexec{
require("../tool_lib/lua/string")
local str = GetKey("#2", "#1", "#3")
tex.sprint(str) % 不能把GetKey函数直接放在这里输出,否则会被莫名其妙地在结尾加上数字1
}
}%
此LaTeX类文件提供各种小工具命令。当前内容中调用了Lua语言编写的函数。
4、string.lua
-- @brief 提取关键字内容
-- @param key 关键字
-- @param str 被提取字符串
-- @param default 默认内容
-- @return 返回关键字对应内容
function GetKey(key, str, default)
-- 前后添加逗号,规范数据流格式
str = ", " .. str .. ","
-- 正是前面对输入参数流前后加入逗号保证了以下语句的适用性
local str1 = string.gsub(str, ".*,%s*" .. key .. "%s*=%s", "")
if (str1 == str) then
return default
else
return string.gsub(str1, "%s*,.*", "")
end
end
此文件用于存放字符串处理函数。LuaLaTeX编译方式下会把Lua源代码编译,c/c++编写的源代码则不行,一般是编译成链接库文件后以供调用。另一个lua程序是我用来记录跟踪日志的,与LaTeX文档没什么关系,这里不再列出源代码。
下面放一张此模式下编译的文档封面:
README.md文件是代码托管网站中自动生成的文件,与latex无关,不做介绍。所有文件用git管理,并在代码托管网站中建立相应的私有仓库存放。