如何编写Latex宏包(一)

Latex宏包就是模板,分为class(以.cls为后缀,在documentclass命令中使用)和style(以.sty为后缀,在usepackage命令中使用),如常见的book,article类;以及graphicx,caption包等。Latex提供了专门的工具doc/docstrip来编写宏包。

简单的包完全可以自己动手写,先看一个简单的例子,我们的目的是最终写出一个完整的毕业论文模板thesis.cls。

编写thesis.cls内容如下:

\NeedsTeXFormat{LaTeX2e}[2007/10/19]
\ProvidesClass{thesis}
[2011/04/05 v0.1 Thesis Template for Doctor]

\LoadClass[cs4size,a4paper,fancyhdr,fntef, UTF8]{ctexbook}[2007/10/19]

\RequirePackage{amsmath,amsthm,amsfonts,amssymb,bm}
\RequirePackage{apjfonts, epigraph, titlesec, textcomp}
\RequirePackage{natbib}

\renewcommand\title[1]{\def\IHEP@value@title{#1}}
\renewcommand\author[1]{\def\IHEP@value@author{#1}}

\renewcommand\maketitle{%
      \clearpage
      \thispagestyle{empty}
      \vspace*{50pt}   
      \begin{center}
      \bf\songti\zihao{-1}中国科学院研究生院\\博士学位论文
      \vskip 50pt
      \bf\heiti\zihao{3} \underline{~\IHEP@value@title~}
      \vskip 20pt
      \bf\kaishu\zihao{4} \underline{~\IHEP@value@author~}
      \end{center}
}

\makeatletter
\def\IHEP@value@title{~~~~~(论~文~题~目)~~~~~}
\def\IHEP@value@author{~~~~~(作~者~姓~名)~~~~~}
\makeatother

\endinput

写测试文件test.tex,内容如下

\documentclass{thesis}
\begin{document}
\title{编写论文模板}
\author{Liyropt}
\maketitle
\end{document}

将thesis.cls和test.tex放在同一目录下,用Latex编译,效果为

注:这里用Linux下的TexLive2009编译,一般编译会报错,可能的原因是缺少中文字体,将windows下的simsun.ttc,simhei.ttf拷贝到当前目录即可。

posted @ 2011-04-07 15:20  liyropt  阅读(12452)  评论(1编辑  收藏  举报