註: 操作此文的前提假設:已熟練使用 LaTeX。 本文操作示例基於 Mac OS。

前言:

剛讀本科時,因為比賽需要開始初學LaTeX,最開始使用的是 Lyx,Lyx 算是從 MS WORD 的過渡軟體,適合初學者使用,由於自己很愛折騰,還換過WinEdt, texmate, sublime text3, 再後來就一直使用 texstudio,

(有關各類 LaTeX 編輯器的比較及介紹可參考:i.e. 有哪些好的 LaTeX 編輯器?)

不過,到了這學期,由於Econometrics-II 需要 Julia 編程,便開始使用 Atom; 後來發現 Atom 有很多不錯的包,安裝便捷,介紹文檔和 GitHub 主頁都在上面,而且也可以自己掛載場外的包(恕我無知,其實很多編輯器也都可以= =),

關於 Atom 編輯器的更多介紹可轉至:

如何評價 GitHub 發布的文本編輯器 Atom? Atom、Sublime Text、VSCode 三者比較,各有哪些優勢和弱勢?

簡要總結就是,編輯器這東西各有千秋,選擇取決於你的偏好、「第一次」等。而 Atom 集成了許多流行文本編輯器的優點,知乎 er 上所說的 Atom 槽點是卡, 但我這全班都在用 Atom ,還真沒看誰卡了,可能大家載入的包都比較少吧 = =


正文

Atom 跨越 Win、Linux、Mac OS 三大平臺, 而且可定製性高。之所以使用 Atom 來編譯 LaTeX 文檔,是因為它提供了許多方便下載且功能強大的包。


0. 軟體下載

首先,你得有 LaTeX 的編譯環境和 Atom 軟體:

CTEX: 包括 TeXLive (Unix/Linux/Windows)、MacTeX (Mac OSX)、MiKTeX (Windows)

Atom:看這主頁,就饒有興緻!

接下來,就是將 Atom 配置成 美麗而強壯的 LaTeX 編輯器。


1. LaTeX 相關基礎包

Atom 下載 package 的方法一般有很多種,常用的有:

  1. 終端執行:apm install NAME_OF_PACKAGE
  2. 進入Atom界面,打開 Preferences - Settings - Install Packages (In Mac: cmd + , ), 下載編譯 LaTeX 所必須的包,如下圖 :


其中,

  • atom-latex :用於支持 LaTeX 語言, 合成 PDF 文檔;
  • language-latex :語言高亮插件,也包括 LaTeX 代碼自動補全
  • pdf-view :用於在 atom 中查看 pdf 輸出
  • (可選)latextools :來自 Sublime Text 的 工具包, 主要包括 自動補全、提示標籤和引用等(部分和 atom-latex功能重疊,所以可選擇不安裝)

Ps:

atom-latex 也可以使用 latex 包替換;更加詳細的下載過程我就不放圖了,如果你不懂的話,可以參考:將Atom配置成為強大且優雅的LaTeX編輯器;另外,Windows 用戶下載相關包可以參考 Atom-LaTex 編輯器設置

安裝好以上的基礎包後,就可以成功編譯 LaTeX 文檔了,功能基本和其他編輯器是一樣的了,結果如下圖:


LaTeX 額外加成包具體指南

對於研究生們來說,碼字首先是 focus ,然後就是圖個快,作業打碼要想快,那就得多來點自動格式化和自動補全,以及拼寫檢查~ 要是想上課直接開 LaTeX 記筆記,那這些工具就更加需要了。Atom 在這一點有許多包可供選擇(當然,其他編輯器也有相應的插件):


其中,

  • linter-spell 是橋接 Hunspell、Aspell 等字典來提供拼寫檢查。

具體操作 follow: Installing hunspell on MacOS, 這裡邊兒有非常詳細的步驟了;相應的,windows 用戶可谷歌 install hunspell on Windows。

但你沒有字典則得從 這裡 下載字典先:

下載好一個 .oxt 文件後,可以進行解壓:

cd ~/your/directoryunzip dict-en.oxt -d dict-encp dict-en/en_US.aff dict-en/en_US.dic ~/Library/Spellingcp dict-en/en_GB.aff dict-en/en_GB.dic ~/Library/Spellingrm -r dict-en

然後 check hunspell 是否能load 字典:hunspell -D

如果成功,會出現以下結果 /Library/Spelling/en_US.dic
  • linter-spell-latex: 支持 LaTeX 拼寫檢查

成功載入這個包,需要在 core packages 中的 spell-check - settings - Grammers中添加: text.tex.latex, 如圖:


  • linter-chktex: 提供 LaTeX 代碼修改建議!

需要 linter, linter-ui-default 等包,當你下載 linter-chktex 並安裝後, Atom 會自動提示你安裝它所需要的其他插件。有時候也可能需要為 linter-chktex 設置 executable path,即匹配你的chktex環境變數(一般在 /usr/local/bin 中),具體設置可以參考其 Github 的 readme


  • 關於我 Disable 的插件:atom-beautify,是提供自動格式化的,它需要你手動安裝 latexindent.pl 。

其實 LaTeX 的自動格式化,有多種方法,每一種方法需要自動格式化的設置,我使用的是 latexindent, 當然還有 latexmk 等。

首先你需要安裝 latexindent:

cd /Library/TeX/texbingit clone https://github.com/cmhughes/latexindent.pl.git

如果有 homebrew, 也可以通過 brew install latexindent.pl 來安裝, 注意需要保證 latexindent.pl, LatexIndent/*.pm, defaultSettings.yaml 三者在同一文件夾(我的放在: /Libraray/TeX/texbin), 安裝成功後,在命令窗口進行如下操作可以進行自動格式化:

latexindent.pl myfile.tex

若運行失敗,出現cannot locate module (沒有安裝 module) 的錯誤,則調用 latexindent-module-install.pl安裝缺少的模塊:

cd /Library/TeX/texbinsudo perl /helper-scripts/latexindent-module-installer.pl

也可以單獨安裝你缺少的模塊,i.e. :

sudo perl -MCPAN -e install "Log::Dispatch::File"


基於 latexindent, 便可以自動格式化我們的 LaTeX 文檔了,我這裡介紹三種方法:

方法一: 在 terminal 運行 latexindent.pl myfile.tex --overwrite


方法二: 在 atom-beautify 中設置啟動 LaTeX-latexindent, 具體請參考 readme

不過使用 atom-beautify 可能會提示一些錯誤信息,其中有的還是 bug, 因為 atom-beautify 即將更新到一個新的插件:Unibeautify 了。

如果出現 Error: spawn EACCES

這可能是需要授予 atom 及 atom-beautify 許可權(executable),詳細解決辦法參考:Stackoverflow - Error: spawn EACCES:

sudo chmod -R a+rwx /usr/local/bin/defaultSettings.yamlsudo chmod a+x /usr/local/bin/latexindent.pl

也可能是需要安裝 uncrustify:

cd ~/git clone https://github.com/uncrustify/uncrustifycd uncrustifymkdir buildcd buildcmake ..cmake --build .sudo make install


方法三: 使用 arara, 這個比較省事,只需要在 LaTeX 文檔開頭寫幾句話就好:

% arara: pdflatex: { shell : yes }% arara: indent% arara: indent: {overwrite: true, silent: on}documentclass{artilce}...

最新的 arara 安裝如下:

cd ~/Applicationsgit clone https://github.com/cereda/arara.git

which indent.yaml

arara myfile.texecho export "JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.bash_profile

也可以下載 arara 3.0版, 不過3.0版本中沒有 indent.yaml, 但LaTeX中運行

% arara: indent: {overwrite: true, silent: on}

卻需要它, 參考arara-issues about indent.yaml,你可以自己寫入一個indent.yaml 放入 /Applications/arara/rules 中:

cd ~/Applicationsgit clone https://github.com/cereda/arara.gitwhich indent.yamlarara myfile.texecho export "JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.bash_profilent flag: "@{isTrue(parameters.silent, -s)}"- identifier: overwrite flag: "@{isTrue(parameters.overwrite, -w)}"- identifier: trace flag: "@{isTrue(parameters.trace, -t)}"- identifier: settings flag: > @{ check = parameters.containsKey(where); location = check ? parameters.where : ; if ([ local, onlydefault ].contains(parameters.settings)) { return isTrue(parameters.settings == local, isTrue(check, -l=.concat(location), -l), -d); } }- identifier: cruft flag: "-c=@{parameters.cruft}"- identifier: output flag: "-o @{parameters.output}"- identifier: where flag: > @{ check = parameters.containsKey(settings); setting = check ? parameters.settings : ; }


如果你成功搞定了以上的配置,你就可以飛速地在 atom 中編輯 LaTeX 啦!另外,你還可以在 atom 中下載其他插件加強基礎配置,例如漢化插件: simplified-chinese-menu、file-icon、atom-terminal-panel、script等等;

關於更多插件推薦請參見:

Atom 必備插件 Atom 有什麼優秀的插件


最後,附一個我常用來寫作業的 LaTeX 模板,僅供參考~

由於我們作業要求一定把公式、圖、代碼都在同一個pdf 中列印出來,於是不得不用 LaTeX 把它們組合起來。其中,若想實現代碼高亮的話,我用的是 mcode.sty, 因為大多數時候我的作業需要粘貼 MATLAB 代碼;其他語言代碼,你可以使用 minted 宏包 或者 自己定義代碼高亮內容,具體請參考:LaTeX 之代碼語法高亮 - lstlisting LaTeX 使用 minted 宏包--代碼高亮

% arara: pdflatex: { shell : yes }% arara: indent: {overwrite: true, silent: on}documentclass{article}[a4paper]usepackage[english]{babel}usepackage{booktabs, graphicx} %replacing hlineusepackage{longtable}usepackage{dcolumn} %centering based on dotsusepackage[utf8]{inputenc}usepackage{adjustbox}usepackage{blkarray}usepackage{amsmath,amsfonts,amssymb,amsthm} % For mathematicsusepackage{hyperref} % For cross-referenceusepackage{float} % For include graphicsusepackage{caption,subfigure}%belows are for highlighting codesusepackage{listings}usepackage{xcolor}usepackage{textcomp}usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}usepackage[section]{placeins}setlength{parindent}{4 pt}%if seperate: select specific section.tex:% includeonly{file1,file2} itle{Problem Set - X}author{name hanks{address exttt{email}}}egin{document}maketitleegin{abstract} Your abstract here.end{abstract}pagebreaksection{Part I}subsection{Question. 1}Assume that $ extbf{Ax}= extbf{b}$, where $ extbf{A}in mathbb{R} ^ {n imes n}$. Some text with a reference to the following group~eqref{eq:group}; now the equations%% Equations %%egin{subequations}label{eq:group} egin{align} A & = B label{eq:A} \ C & = D label{eq:B} end{align}end{subequations}and a referencea to the first equation~eqref{eq:A}.Also the second~eqref{eq:B}.egin{equation}label{2} egin{cases} x_n=b_n^{(n)}/a_{nn}^{(n)}, \\ x_i=(b_i^{(i)}-sumlimits_{j=i+1}^n{a_{ij}^{(i)}x_j})/a_{ii}^{(i)} end{cases}end{equation}egin{equation*} y=xend{equation*}[ egin{bmatrix} 3.01 & 6.03 & 1.99 \ 1.27 & 4.16 & -1.23 \ 0.987 & -4.81 & 9.34 end{bmatrix} egin{bmatrix} x_1 \ x_2 \ x_3 end{bmatrix} = egin{bmatrix} 1 \ 1 \ 1 end{bmatrix}]%% Figures %%% egin{figure}[H]% Requires usepackage{float}% centering% includegraphics[width_=120mm]{flowfig}\% caption{Figure.1}label{fig.1}% end{figure}%% Input Codes %%% lstset{numbers=left,numberstylex= iny}% lstinputlisting[firstline=6, lastline=15]{/Users/name/Desktop/FILENAME.M}% or:lstset{numbers=left,numberstylex= iny}egin{lstlisting}[xleftmargin=2em,xrightmargin=2em,aboveskip=1em]clc;clear;x=randn(500,1); % §mcommentfont ...$x^2+y=1$§for i = 1:3 if i >= 5 disp(cool);end{lstlisting}%% Table %%% some trivials:% table diagbox: usepackage{diagbox}% diagbox{Player A}{Player B}% rotate table: usepackage{rotating}% egin{sidewaystable} centering% To make width equal to text:% LTleft=0pt% LTright=0pt% egin{longtable}{@{extracolsep{fill}}}% Online LaTeX Table: http://www.tablesgenerator.com/latex_tables% or:% LTleft=0pt% setlength{ abcolsep}{1pt}% input{table.tex} % I usually input my Stata table here.% for more info about Stata output from esttab, see:% http://repec.org/bocode/e/estout/esttab.htmlend{document}


Thanks

Edited by econdoggy

推薦閱讀:

相关文章