码迷,mamicode.com
首页 > 其他好文 > 详细

制作代码模板的 LaTex 模板

时间:2017-11-19 22:47:52      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:封面   相关   show   部分   sub   pap   com   def   int   

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 模板

标签:封面   相关   show   部分   sub   pap   com   def   int   

原文地址:http://www.cnblogs.com/flipped/p/7862508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!