制作代碼模板的 LaTex 模板


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[a4paper,scale=0.8,hcentering,bindingoffset=8mm]{geometry} % 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}
代碼內容
\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更適合寫這種程序。

效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM