制作代碼模板的 LaTex 模板
阿新 • • 發佈:2017-11-20
偶數 ... ger xelatex center uri jin 模板 cos
Tex 真的是一個用起來非常舒服的排版工具(對於排版要求高的人來說),去比賽前一天放棄了markdown轉pdf來生成代碼模板,現學Tex(其實美賽已經用過了:P)。
推薦一個鏈接:TeX — Beauty and Fun,可以了解一下Tex。
我用的工具是Mac下的TexShop,排版時選擇XeLaTeX。
基本模板:
% !TEX encoding = UTF-8 Unicode
\documentclass[a4paper,11pt,twoside,fontset = fandol,UTF8]{ctexbook} % 頁面A4紙大小,11 磅大小的字體,式樣為雙面,字體集為Fandol,編碼為UTF8,文檔類型為cTex的book(支持中文)
\usepackage{geometry}
\geometry{a4paper,scale=0.8} % A4紙大小,縮放80%
\usepackage{hyperref} %超鏈接
\usepackage{listings} %代碼塊
\usepackage{courier} %字體
\usepackage{fontspec} %字體
\usepackage{fancyhdr} % 頁眉頁腳相關宏包
\usepackage{lastpage} % 引用最後一頁
\usepackage{amsmath,amsthm,amsfonts,amssymb,bm} %數學
\usepackage{graphicx} % 圖片
\usepackage{subcaption} % 圖片描述
\usepackage{longtable,booktabs} % 表格
\setmonofont{Consolas} %設置字體為Consolas
\lstset{ %設置代碼塊
basicstyle=\footnotesize\ttfamily, % 基本風格
numbers=left, % 行號
numbersep=10pt, % 行號間隔
tabsize=2, % 縮進
extendedchars=true, % 擴展符號?
breaklines=true, % 自動換行
language=C++,
frame=leftline, % 框架左邊豎線
xleftmargin=19pt, % 豎線左邊間距
showspaces=false, % 空格字符加下劃線
showstringspaces=false, % 字符串中的空格加下劃線
showtabs=false, % 字符串中的tab加下劃線
}
\pagestyle{fancy} % 頁眉頁腳風格
\fancyhf{} % 清空當前設置
\fancyfoot[C]{\thepage\ / \pageref{LastPage}}%頁腳中間顯示 當前頁 / 總頁數,把\label{LastPage}放在最後
\fancyhead[LO,RE]{\thepage}% 頁眉奇數頁左邊,偶數頁右邊顯示當前頁
\begin{document}
\begin{titlepage} % 封面
\centering
\vspace*{\baselineskip}
\rule{\textwidth}{1.6pt}\vspace*{-\baselineskip}\vspace*{2pt}
\rule{\textwidth}{0.4pt}\\[\baselineskip]
{\LARGE Algos @BY 2017\\[\baselineskip]\small for ACM ICPC}
\\[0.2\baselineskip]
\rule{\textwidth}{0.4pt}\vspace*{-\baselineskip}\vspace{3.2pt}
\rule{\textwidth}{1.6pt}\\[\baselineskip]
\scshape
\begin{figure}[!htb]
\centering
\includegraphics[width=0.3\textwidth]{icpc} % 當前tex文件同一目錄下名為icpc的任意格式圖片
\end{figure}
\vspace*{3\baselineskip}
Edited by \\[\baselineskip] {向餓勢力低頭\par}
{Team \Large Bow to hungrY \normalsize{at BUPT}\par }
\vfill
{\scshape 2017} \\{\large BEIJING}\par
\end{titlepage}
\newpage % 封面背後空白頁
\tableofcontents % 目錄
\chapter{計算幾何} % 第一章
\setcounter{page}{1} % 這裏頁數從1開始計算
\section{ 幾何基礎} % 第一節
\begin{lstlisting} % 代碼塊
#include <bits/stdc++.h>
using namespace std;
#define sqr(x) (x)*(x)
typedef double dd;
const dd EPS = 1e-8;
const dd PI = acos(-1);
int sgn(dd x) {return x<-EPS?-1:x>EPS;}
struct Po {
dd x,y;
Po(dd x=0,dd y=0):x(x), y(y) {}
Po operator -(const Po&b)const {return Po(x-b.x,y-b.y);}
Po operator +(const Po&b)const {return Po(x+b.x,y+b.y);}
Po operator *(dd b)const {return Po(x*b,y*b);}
Po operator /(dd b)const {return Po(x/b,y/b);}
bool operator ==(const Po&b)const {return x==b.x&&y==b.y;}
dd operator ^(const Po&b)const {return x*b.y-y*b.x;}
dd operator *(const Po&b)const {return x*b.x+y*b.y;}
dd dis(const Po &p){return sqrt(sqr(x-p.x)+sqr(y-p.y));}
};
//叉積|a||b|sin:為0:oa共線ob,大於0:ob在oa的逆時針方向
dd xmul(Po a,Po b,Po o) {return (a-o)^(b-o);}
//點積|a||b|cos:為0:oa垂直ob
dd mult(Po a,Po b,Po o) {return (a-o)*(b-o);}
\end{lstlisting}
\section{ 向量變換} % 第二節
\begin{lstlisting}
struct Po {
..
Po rotL(){return Po(-y,x);}//逆時針90度
Po rotR(){return Po(y,-x);}
dd len(){return sqrt(x*x+y*y);}
Po trunc(dd r){//截短為r
dd l=len();if(!sgn(l))return *this;
r/=l;
return Po(x*r,y*r);
}
//繞p逆時針轉ang度
Po rotate(Po p,dd ang){
Po v=*this-p;
dd c=cos(ang),s=sin(ang);
return Po(p.x+v.x*c-v.y*s,p.y+v.x*s+v.y*c);
}
};
\end{lstlisting}
% ...其它內容
\end{document}
\label{LastPage}
章節層次
- chapter
- section
- subsection
圖片
如果需要幾張圖片並排顯示,可用subfigure宏包。
\begin{figure}[h]
\centering
\begin{subfigure}[h]{0.2\textwidth}
\includegraphics[width=\textwidth]{平移} % 名為'平移'的圖片文件放在同一目錄
\end{subfigure}
~
\begin{subfigure}[h]{0.2\textwidth}
\includegraphics[width=\textwidth]{縮放}
\end{subfigure}
~
\begin{subfigure}[h]{0.5\textwidth}
\includegraphics[width=\textwidth]{旋轉}
\end{subfigure}
\ \begin{subfigure}[h]{0.8\textwidth}
\includegraphics[width=\textwidth]{3D繞軸翻轉}
\end{subfigure}
\end{figure}
表格
用了longtable宏包
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\begin{longtable}[]{@{}llll@{}}
\toprule
\(r\cdot 2 ^ k + 1\) & r & k & g\tabularnewline
\midrule
\endhead
3 & 1 & 1 & 2\tabularnewline
5 & 1 & 2 & 2\tabularnewline
17 & 1 & 4 & 3\tabularnewline
97 & 3 & 5 & 5\tabularnewline
193 & 3 & 6 & 5\tabularnewline
257 & 1 & 8 & 3\tabularnewline
7681 & 15 & 9 & 17\tabularnewline
12289 & 3 & 12 & 11\tabularnewline
40961 & 5 & 13 & 3\tabularnewline
65537 & 1 & 16 & 3\tabularnewline
786433 & 3 & 18 & 10\tabularnewline
5767169 & 11 & 19 & 3\tabularnewline
7340033 & 7 & 20 & 3\tabularnewline
23068673 & 11 & 21 & 3\tabularnewline
104857601 & 25 & 22 & 3\tabularnewline
167772161 & 5 & 25 & 3\tabularnewline
469762049 & 7 & 26 & 3\tabularnewline
1004535809 & 479 & 21 & 3\tabularnewline
2013265921 & 15 & 27 & 31\tabularnewline
\bottomrule
\end{longtable}
代碼
用的是listings宏包。具體配置可看wiki-Source_Code_Listings,要求多的可查閱文檔
Markdown 轉換到 LaTex
原來的 Markdown 的代碼模板裏大部分就是 ###
標題和代碼塊,手動加入 LaTex 裏太辛苦了,用 typora 轉的 LaTex 內容太復雜了,所以我寫了個 c++ 程序來轉:P,不過也許 python更適合寫這種程序。
效果圖
制作代碼模板的 LaTex 模板