使用Sublime Text+LaTeXTools+Skim配置latex環境。
[TOC]
安裝MacTeX
MacTeX是mac上的MacTex分發軟件,包含眾多軟件包。
進入MacTex官網,下載MacTeX.pkg
文件安裝MacTeX。
安裝LaTeXTools、LaTexing
如果Sublime Text沒有安裝Package Control,先安裝Package Control。
打開 Sublime Text 3 選擇 View 中的 Show Console 。
這時sublime text下面會顯示一個輸入窗口,復制下面這段代碼到窗口里面并且回車執行,然后重啟 Sublime Text 3 。
import urllib.request,os,hashlib; h = 'df21e130d211cfc94d9b0905775a7c0f' + '1e3d39e33b79698005270310898eea76'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
安裝 LaTeXTools插件
按下 shift + ? + P
,輸入 Install Package
,回車。
輸入LaTeXTools
,回車安裝LaTeXTools插件。
安裝 LaTexing插件
LaTexing是LaTex自動填充的插件。
按下 shift + ? + P
,輸入 Install Package
,回車。
輸入LaTexing
,回車安裝該插件。
安裝Skim
安裝好后運行Skim,進入Skim——選項,點擊同步進行設置
勾選檢查文件變化,在PDF-Tex同步支持那里選擇sublime Text,這樣當你編譯tex后就會自動打開pdf預覽了。
配置中文文檔編寫環境
打開終端,運行:
sudo tlmgr update --self
sudo tlmgr install latexmk
在sublime Text里打開LaTeXTools.sublime-settings(也就是LaTeXTools的用戶設置,如果你是從舊版本升級上來或者擔心這個配置文件出現問題,可以依次點擊Preferences——Package Settings——LaTeXTools——Reconfigure LaTeXTools and migrate settings重建配置文件),在builder-settings下面新增兩項配置:
"program" : "xelatex",
"command" : ["latexmk", "-cd", "-e", "$pdflatex = 'xelatex -interaction=nonstopmode -synctex=1 %S %O'", "-f", "-pdf"],
另外注意之前應該有"builder": "default"(或直接設置為空或”traditional”)。
注意:中文編寫LaTex可能會出現排版的問題,解決辦法是在代碼的開頭加上!TEX program = xelatex
,然后添加\usepackage[UTF8]{ctex}
這樣一個package就好了。如圖,
LaTex編寫
建立第一個文檔
\documentclass{article}
\begin{document}
hello, world
\end{document}
文章標題、作者
\documentclass{article}
\author{My Name}
\title{The Title}
\begin{document}
\maketitle
hello, world % This is comment
\end{document}
章節和段落
"section"即章節的標記,在section前加一個"sub"前綴表示章節的子章節,如果加兩個"sub",表示子章節的子章節,以此類推。"paragraph"與"subparagraph"為段落表示,與section使用類似。
\documentclass{article}
\title{Hello World}
\begin{document}
\maketitle
\section{Hello China} China is in East Asia.
\subsection{Hello Beijing} Beijing is the capital of China.
\subsubsection{Hello Dongcheng District}
\paragraph{Tian'anmen Square}is in the center of Beijing
\subparagraph{Chairman Mao} is in the center of Tian'anmen Square
\subsection{Hello Guangzhou}
\paragraph{Sun Yat-sen University} is the best university in Guangzhou.
\end{document}
插入目錄
在\section之前的一行加入"\tableofcontents",即可生成目錄。
\documentclass{article}
\begin{document}
\tableofcontents
\section{Hello China} China is in East Asia.
\subsection{Hello Beijing} Beijing is the capital of China.
\subsubsection{Hello Dongcheng District}
\paragraph{Hello Tian'anmen Square}is in the center of Beijing
\subparagraph{Hello Chairman Mao} is in the center of Tian'anmen Square
\end{document}
數學公式
作為初學者先看一下基本的公式生成,用到相應公式時再去維基百科|數學公式查應該夠用了。
\documentclass{article}
\begin{document}
This is an equation \[f(x) = 3x + 7\] %數學公式的前后要加上 $ 或 \( 和 \),如果用 \[ 和 \],或者使用 $$ 和 $$,則改公式獨占一行;
This is an equation \(f(x) = 3x + 7\)
$$\sum_{i=1}^n a_i=0$$ %用 ^ 來表示上標,用 _ 來表示下標
$$f(x)=x^{x^x}$$
$$f(x_1,x_x,\ldots,x_n) = x_1^2 + x_2^2 + \cdots + x_n^2 $$ %\ldots 表示跟文本底線對齊的省略號;\cdots 表示跟文本中線對齊的省略號
\[a^{2}=\sqrt{b}+\sqrt[4]{y}\] %開方使用\sqrt[n]{x}命令
\end{document}
LaTex命令 | 預覽效果 |
---|---|
$ x_i $ |
|
$ x^2 $ |
|
$ x^ {y^z} $ |
|
$ \int_a^b f(x) $ |
|
$ \frac ab $ |
etexify LaTeX handwritten symbol recognition.:識別手寫的symbol然后給出相應的Latex代碼并且給出需要include的package,識別的準確度很高。
列表
列表使用enumitem包。導言區引用:
\usepackage{enumitem}
有序列表
\begin{enumerate}
\item one
\item two
\item three
\end{enumerate}
無序列表
\begin{itemize}
\item these components
\item reference implementation
\item which are built according
\item reference specification
\end{itemize}
插入圖片
將待插入的圖片姑且先命名為 figure1.jpg 建立一個新文檔,將以下內容復制進入文檔中,保存,保存類型選擇為 UTF-8,放在和圖片同一個文件夾中,編譯并觀察現象
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=4.00in,height=3.00in]{figure1.jpg}
\end{document}
插入表格
表格的幾種插入格式,注意觀察有無\hline 和有無\begin{center}的區別。注意觀察\begin{tabular} 后的 lcr 的區別,分別是 left 對齊,center 對齊和 right 對齊。
\documentclass{article}
\begin{document}
\begin{tabular}{|c|c|}
aaa & b \\
c & ddddd\\
\end{tabular}
\begin{tabular}{|l|r|}
\hline
aaaa & b \\
\hline
c & ddddd\\
\hline
\end{tabular}
\begin{center}
\begin{tabular}{|c|c|}
\hline
a & b \\ \hline
c & d\\
\hline
\end{tabular}
\end{center}
\end{document}
可使用在線LaTex表格生成器tablesgenerator。
版面設置
頁面模板
頁面模板在\documentclass
引用處設置,下面是一些常用的屬性:
- 字號大小:有四種可能的字號,分別是9pt, 10pt, 11pt, 12pt, 加粗的為默認值,默認值適用于大多數paper,無需修改。
- 草稿模式:同樣此處有四種可能的模式,分別是draft, draftcls, draftclsnofoot, final, 倘若使用draft模式則文章的行距被調整為二倍行距以方便加批注,默認為final即最終稿模式,此處也無需修改,保持默認即可。
- 模板類型:此處有5種可能的類型,分別是conference, journal, technote, peerreview, peerreviewca。
- 紙張類型:此處有三個可選項:letterpaper, a4paper, cspaper。
- 單雙面:oneside, twoside,默認為單面,暫且保持默認即可。
- 單雙欄:onecolumn, twocolumn,默認為雙欄,暫且保持默認即可。
向以太坊的黃皮書,就是9pt,單面,雙欄,a4紙的,設置如下:
\documentclass[9pt,oneside,twocolumn,a4paper]{article}
頁邊距
設置頁邊距,推薦使用geometry
宏包。可以在這里查看它的說明文檔。
比如我希望,將紙張的長度設置為 20cm、寬度設置為 15cm、左邊距 1cm、右邊距 2cm、上邊距 3cm、下邊距 4cm,可以在導言區加上這樣幾行:
\usepackage{geometry}
\geometry{papersize={20cm,15cm}}
\geometry{left=1cm,right=2cm,top=3cm,bottom=4cm}
頁面背景色
設置顏色相關的,比如字體顏色、文本背景或頁面背景顏色,都是要xcolor
宏包。見文檔wiki/LaTeX/Colors。
設置頁面背景顏色,先在導言區引用xcolor包:
\usepackage{xcolor}
\definecolor{pagecolor}{rgb}{1,0.98,0.9}
然后在\begin{document}
下添加\pagecolor
:
\pagecolor{pagecolor}
頁眉頁腳
設置頁眉頁腳,推薦使用fancyhdr
宏包。可以在這里查看它的說明文檔。
比如我希望,在頁眉左邊寫上我的名字,中間寫上今天的日期,右邊寫上我的電話;頁腳的正中寫上頁碼;頁眉和正文之間有一道寬為 0.4pt 的橫線分割,可以在導言區加上如下幾行:
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\author}
\chead{\date}
\rhead{152xxxxxxxx}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\headwidth}{\textwidth}
\renewcommand{\footrulewidth}{0pt}
行間距
我們可以通過 setspace宏包提供的命令來調整行間距。比如在導言區添加如下內容,可以將行距設置為字號的 1.5 倍:
\usepackage{setspace}
\onehalfspacing
具體可以查看該宏包的文檔。
段間距
我們可以通過修改長度 \parskip
的值來調整段間距。例如在導言區添加以下內容
\addtolength{\parskip}{.4em}
則可以在原有的基礎上,增加段間距 0.4em。如果需要減小段間距,只需將該數值改為負值即可。
參考文獻
bib
文件的寫法,推薦先通讀bibtex
官方的文檔。
示例references.bib
:
@book{adams1995hitchhiker,
title={The Hitchhiker's Guide to the Galaxy},
author={Adams, D.},
isbn={9781417642595},
url={http://books.google.com/books?id=W-xMPgAACAAJ},
year={1995},
publisher={San Val}
}
將其保存為references.bib后,在文章的tex文件中,最后引用:
\bibliographystyle{plain}
\bibliography{references}