前言
呵呵, 終于要開始寫論文了.
我很討厭觸碰從來沒接觸過的東西, 比如Latex, 之前從來沒用過, 其實我心理還是想用word來寫論文的, 畢竟我這個方向不會寫巨復雜的公式. 考慮到種種原因, 還是硬著頭皮學習了一下.
作為萬里長征的第一步, 搭建開發環境總是最讓人頭大的. 這兩天琢磨了4個開發環境, 并成功的編譯了USENIX的論文模板:
Templates for Conference Papers
當然,本文僅僅對剛剛開始使用Latex的同學具有一定的參考價值,可以稍微降低學習成本。
此外,網上有很多相關的環境配置的博客,但是可能已經過去太久了,像VS Code、Intellij Idea等相關插件都優化了,已經不需要用戶進行復雜的配置了,傻瓜式的鼠標點兩下就行了。
本文給出了4種方式來寫Latex,分別是:
- Overleaf
- ShareLatex
- VS Code + LaTeX Workshop
- Intellij Idea + TeXiFy
至于孰優孰劣,本文不做評價,全憑個人喜好,比如我,就是Jetbrains 的忠實擁躉。
最后,實現的目標是,能成功的編譯USENIX的論文模板輸出PDF文件,并實現正反向的搜素(即在PDF中定位tex文檔,和從tex文檔定位PDF文件)。說白了也是淺嘗輒止,入個門而已。
對于想簡單入門Latex語法的同學,可以看附錄一, 我對模板內容進行了全面的注釋。
(截至2021年11月10日, 下述所有的軟件及其插件,都是官網的最新版!)
Overleaf
簡介
Overleaf是一個在線的Latex編輯器:
操作
登錄其官網,然后注冊賬戶登錄即可。在操作界面:
我們可以創建啥也沒有的空白項目,或者從本地/Github上傳已有的項目(Github需要收費),它還貼心的給我們準備了很多模板。但是我們在這里選擇創建一個空白模板:
功能很少,也非常的一目了然,我們自己導入USENIX的論文模板:
編譯后顯示:
這是因為我們導入了宏包usenix2019_v3.sty,但是其實USENIX模板的名字其實是usenix-2020-09.sty,因此只需要重命名一下就好了:
可以看到圖片、引用文獻都是正常的!但是有4個警告,我們暫且不管,我將在文末的附錄中貼上最終我注釋修改過的模板,以供大家入門Latex的語法。
此時,你將緩緩打出不屑。是的就是這么簡單,Overleaf其實已經很不錯了,我身邊的同學都在用,而且還能share,讓老師幫你修改等等。如果你對本地編譯Latex毫無興趣,那么就可以到此打住了!
ShareLatex
簡介
你可以簡單的認為ShareLatex就是Overleaf的開源版,即你可以自己搭建Overleaf服務,這樣你的數據就掌握在你的手上,不用擔心Overleaf網不好,網速太慢,泄密等問題。
關于ShareLatex和Overleaf的更多信息,可以查閱:
ShareLaTeX, Online LaTeX Editorwww.sharelatex.com/[圖片上傳失敗...(image-8a07b5-1636597623220)]
環境搭建
這里使用官方文檔[1][2]推薦的傻瓜模式進行部署,詳細的文檔參閱文末參考文獻。
Prerequisites
- docker
- docker-compose
配置
(1) 克隆Overleaf Toolkit到本地(服務器):
git clone https://github.com/overleaf/toolkit.git ./overleaf
cd ./overleaf
(2) 初始化配置文件
bin/init
此時將生成三個配置文件:
ls config/
overleaf.rc variables.env version
(3) 啟動
bin/up
注意,第一次運行時需要拉去Docker 鏡像,比較慢,成功之后將不會退出命令行,可以用***Ctrl + C ***退出,此時將關閉啟動的容器,然后可以用:
bin/start
bin/stop
來啟動和關閉容器服務。
up或start 之后,將會啟動三個容器:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c5c888f7b8b sharelatex/sharelatex:3.0.1 "/sbin/my_init" 3 days ago Up 2 hours 0.0.0.0:80->80/tcp sharelatex
07ae53497628 mongo:4.0 "docker-entrypoint.s…" 3 days ago Up 2 hours (healthy) 27017/tcp mongo
fbc9980d58b3 redis:5.0 "docker-entrypoint.s…" 3 days ago Up 2 hours 6379/tcp redis
我們需要重點關注sharelatex的容器端口,如果你按上述過程執行,那么將是:
127.0.0.1:80 -> 80/tcp
這意味這僅僅監聽本地的請求,如果你想監聽所有用戶的請求,那么需要修改其監聽地址為:
0.0.0.0:80 -> 80/tcp
這需要修改config/overleaf.rc文件:
之后需要使用bin/up重啟容器,修改配置后必須使用bin/up才能生效!!!
(4) 創建管理賬號
瀏覽器中打開:http://localhost/launchpad
(5)訪問
之后的操作就等同于Overleaf了,我們將得到一個非常類似的界面:
安裝完整texlive
當我們編譯模板時會發現:
提示缺少宏包:microtype.sty
這其實是系統宏包,為什么會這樣呢?
這是因為我們的sharelatex僅僅安裝了最小化的TeXLive[3]如果需要完整的環境,需要我們手動下載:
(1) 啟動服務后,執行:
docker exec sharelatex tlmgr update
docker exec sharelatex tlmgr install scheme-full
(2) 提交容器鏡像
docker commit sharelatex sharelatex/sharelatex:with-texlive-full
(3) 修改配置文件中sharelatex的版本為with-texlive-full
如果你熟悉docker,那么我相信,你很清楚這是在干嘛!!!
VS Code + LaTeX Workshop
簡介
VS Code的大名,就不用過多介紹了吧。
安裝TeXLive,VS Code以及LaTeX Workshop插件的過程就不贅述了,如果這都搞不定,那可以私信我(只幫女生)。此外,推薦安裝完整版的TeXLive,浪費點空間沒事的!
這個時候, 我們新建一個文件夾, 然后放入模板文件, 然后用VS Code打開:
不需要任何配置, 我們就可以直接編譯和預覽, 我相信這才是我們的VS Code.
配置
我們現在來關注一下命令欄窗口:
主要來看Build和View兩個功能:
BUILD
Build無非就是編譯Tex文件, 每一個綠色的三角都是一個編譯方式, 可見我們有多條路可選, 而我們默認的方式是使用latexmk編譯器, 此外我們還可以看到pdflatex這個編譯器, 他需要配合bibtex進行四次編譯, 記住這兩點!
我們可以打開默認的設置(defaultSettings.json, 按F1,然后鍵入defaultSettings), 查看:
放在"latex-workshop.latex.recipes"第一個的既是默認Build方式.
這里有一個問題, 那就是默認情況下這個配置是不支持中文路徑的, 因此如果你不想改配置, 那么就不要把文件放在中文路徑中, 但是如果你的用戶名就是中文, 沒辦法了,那么就要修改這個配置.
但是注意我們目前打開的是defaultSettings.json, 即VS的默認配置文件,它是只讀的, 如果我們要修改, 需要在settings.json中覆蓋默認設置.
因此你只需要把latex-workshop.latex.tools對應的配置中的%DOC%修改為%DOCFILE%,然后將其放入settings.json(按F1, 鍵入settings)中即可,也可以直接用我的寫好的, 見附錄二.
此外, 放我們編譯之后, 會產生很多的臨時文件:
此時,我們可以使用clean up清除:
那些臨時文件將被清除也在默認配置文件中, 但是我們這次通過"文件->首選項->設置"來查看:
也可以通過搜索"latex-workshop.latex.clean.fileTypes"來定位, 也可以在默認配置文件中查看.
請注意, 我們需要保留*.gz文件:
它是我們進行正反向搜索用的.
我們也可以配置自動清理:
至于其他的配置, 大家自己查閱把, 本文僅僅是帶你入門.
注意, 我認為修改配置還是用第二種方式, 除非配置過于復雜, 否則不要自己去修改settings.json.
VIEW
查看PDF有三種方式
- 可以再VS Code中直接查看, 對應了"VSCocd tab"
- 可以在瀏覽器中查看, 對應了"browser"
- 可以使用外部的查看器, 比如SumatraPDF
顯然, 我們推薦大家使用前兩種! 對于第三種, 需要自己的額外的配置, 可以自己去研究!
正反搜索
從PDF到VSCode
按住Alt鍵, 鼠標點擊即可定位到VSCode的位置
從VSCode到PDF
使用快捷鍵: Ctrl+Alt+J 即可跳轉到VSCode光標所在的PDF對應的位置
Intellij Idea + TeXiFy
簡介
Intellij Idea的大名,就不用過多介紹了吧。
安裝TeXLive,Intellij Idea以及TeXiFy插件的過程就不贅述了,如果這都搞不定,那可以私信我(只幫女生)。此外,推薦安裝完整版的TeXLive,浪費點空間沒事的!
此外,不一定非要是Intellij Idea,Pycharm等Jetbrains的IDE都是可以的,只要能下載TeXiFy插件。
配置
要想正常的編譯, 需要我們來定義配置文件, 這是Jetbrains IDE的標準模式, 當然你也可以點擊上圖中在\begin{document}前面的綠色箭頭來自動生產配置文件:
點擊運行之后你會發現, 生成了一個out目錄, 里面包含了最終的pdf文件!但是很可惜, 這種方式生成的pdf的參考文獻是有問題的!
pdfLatex
我們點開自動生成的配置文件:
發現他其實是使用了pdfLatex這個編譯器, 但是我們在VSCode中說過了, 這個編譯器要配合BibTex才行, 但是IDEA沒有給我們自動生產(有時候也會自動生成, 但是配置有問題, 很奇怪).
這時候需要我們自己配置一個:
然后再在pdfLatex中指定BibTex:
這個時候, 你會發現有4個輸出終端, 這是符合預期的最終的pdf也是正常的:
有時候, IDEA會給我們自動生產BIbTex的配置, 但是是有問題的, 你只要改成我上面的配置即可. (僅僅是簡單的去掉了環境變量)
Latexmk
當然, 你可能還記得VSCode的默認編譯器是Latexmk, 而它不需要這么復雜的路徑, 我們的IDEA當然也是支持的, 你只需要修改一下:
你會發現也是正常的.....
因為我怕你學會了這個就不會去看pdfLatex的配置了.
View
最后就是查看PDF, 顯然我們不能在文件夾中去查看pdf. IDEA暫時不能像VSCode那樣支持三種方式, 目前僅僅支持第三方工具:SumatraPDF
下載安裝之后, 重啟IDEA,就會在配置中找到:
此時點擊運行, 就會直接調用SumatraPDF
正反搜索[4]
從PDF到IDEA
在IDEA中,選中"Tools->LaTeX->Configure Inverse Search";
然后在PDF中雙擊即可跳轉到IDEA.
配置操作只需要執行一次.
從IDEA到PDF
使用快捷鍵: "Ctrl+Alt+Shift+." 即可跳轉到IDEA光標所在的PDF對應的位置
后記
截至2021年11月10日, 上述所有的軟件及其插件,都是官網的最新版!
附錄一
我注釋過的模板文件
% documentclass 用于指明文檔的類型,如book、article等
% {}中是必須參數,即必須指定類型
% []中是可選參數,在這里:
% letterpaper, 顯然就是紙張類型
% twocolumn, 表示兩列,論文格式
% 10pt, 字體大小,這個大小并不是隨意指定的,過大會無效
\documentclass[letterpaper,twocolumn,10pt]{article}
% usepackage, 用于引入宏包,可以是自定義的或者系統包
% usenix, 對應了同目錄下的usenix.sty, 是usenix指定的格式
% tikz,amsmath 包用于實現畫圖
\usepackage{usenix}
\usepackage{tikz}
\usepackage{amsmath}
% 這個命令是VScode的?警告建議添加的
% 大體意思是,"\nonfrenchspacing is active. 調整字間距將會禁用它, 可添加如下指令規避"
\microtypecontext{spacing=nonfrench}
% filecontents 宏應該是以下內容生成到指定的文件中
% 文件名也就是{\jobname.bib} 其中\jobname 對了tex文件的名稱:usenix-copy
% bib其實是參考文獻的意思, .bib是專門存放參考文獻的文件格式
% 以下將會生成一個名為usenix-copy.bib的文件存放參考文獻, 但是其實我們可以直接在此文件中寫入, 因此下面的內容可以直接去掉
% bib文件最終會生產blb文件,此文件會被反向查找定位
% 在文章的最后, 需要用以下指令來引入參考文獻:
% \bibliographystyle{plain}
% \bibliography{\jobname}
%
% 此外, 作者名的格式必須嚴格寫,每個作者名之間要用`.`即英文句點隔開
%
% \usepackage{filecontents}
% \begin{filecontents}{\jobname.bib}
% @inproceedings{arpachiDusseau18:osbook,
% author = {Edward Oakes. Leon Yang. Dennis Zhou. Kevin Houck. Tyler Harter. Andrea C. Arpaci-Dusseau. and Remzi H. Arpaci-Dusseau},
% title = {SOCK: Rapid Task Provisioning with Serverless-Optimized Containers},
% booktitle = {USENIX Annual Technical Conference (USENIX ATC)},
% year = 2018
% }
% @inproceedings{waldspurger02,
% author = {Edward Oakes. Leon Yang. Dennis Zhou. Kevin Houck. Tyler Harter. Andrea C. Arpaci-Dusseau. and Remzi H. Arpaci-Dusseau},
% title = {SOCK: Rapid Task Provisioning with Serverless-Optimized Containers},
% booktitle = {USENIX Annual Technical Conference (USENIX ATC)},
% year = 2018,
% note = {\url{https://www.usenix.org/conference/atc18/presentation/oakes}}
% }
% \end{filecontents}
% document的開始
\begin{document}
% 一開始的論文的標題, 包括了\date, \title, \author 等
% ATC中不要求寫\date 而且指定了\title, \author的格式
% 最后要通過\maketitle講上述給顯示出來
%
% \\ 是強制換行
%
% 1\. \tiny 2\. \scriptsize 3\. \footnotesize 4\. \small 5\. \normalsize
% 6\. \large 7\. \Large 8\. \LARGE 9\. \huge 10\. \Huge 依次表示了字體大小
%
% \bf是加粗, 用法是 {\bf text}
% 加粗還可以是\textbf{text}, 更推薦使用此
%
% \rm 是將斜體變為正體
%don't want date printed
\date{}
% make title bold and 14 pt font (Latex default is non-bold, 16 pt)
\title{\Large \bf Formatting Submissions for a USENIX Conference:\\
An (Incomplete) Example}
%for single author (just remove % characters)
\author{
{\rm Your N.\ Here}\\
Your Institution
\and
{\rm Second Name}\\
Second Institution
% copy the following lines to add more authors
% \and
% {\rm Name}\\
%Name Institution
} % end author
\maketitle
%-------------------------------------------------------------------------------
% abstract, 摘要有自己的關鍵字和格式,和section相區別
\begin{abstract}
Your abstract text goes here. Just a few facts. Whet our appetites.
Not more than 200 words, if possible, and preferably closer to 150.
\end{abstract}
%-------------------------------------------------------------------------------
% 每個section都會自己編號,如果使用 section* 可以不參與編號
% 可以使用\subsection \subsubsection 等
\section{Introduction}
A paragraph of text goes here. Lots of text. \\Plenty of interesting
text. {\bf Text} \textbf{text} text text text text text text text text text text text
text text text text text text text text text text text text text text
text text text text text text text text text text text text text text
text text text text text text text.
More fascinating text. Features galore, plethora of promises.
%-------------------------------------------------------------------------------
% \footnote是注腳
% \begin{verbatim} code \end{verbatim} 之間可以寫代碼
% \cite{} 可以引用參考文獻, 可引用多個,用`,`隔開
% `~` 是個經常出現,他表示一個不可打斷的空格,后面跟的必須和前面的單詞在同一行,其本身就是一個空格哦
% \ref{label} 用于引用圖片,章節等,被引的對象必須定義\label{},\ref{}同樣會根據類型來自動編號
\section{Footnotes, Verbatim, and Citations}
Footnotes should be places after punctuation characters, without any
spaces between said characters and footnotes, like so.
\footnote{Remember that USENIX format stopped using endnotes and is
now using regular footnotes.} And some embedded literal code may
look as follows.\footnote{Remember that USENIX format stopped using endnotes and is
now using regular footnotes.}
\begin{verbatim}
int main(int argc, char *argv[])
{
return 0;
}
\end{verbatim}
Now we're going to cite somebody. Watch for the cite tag. Here it
comes. Arpachi-Dusseau and Arpachi-Dusseau coauthored an excellent OS
book, which is also really funnnnnnnny\cite{arpachiDusseau18:osbook,waldspurger02,waldspurger02,waldspurger02}, and
Waldspurger got into the SIGOPS hall-of-fame due to his seminal paper
about resource management in the ESX hypervisor~\cite{waldspurger02}.
The tilde character (\~{}) in the tex source means a non-breaking
space. This way, your reference will always be attached to the word
that preceded it, instead of going to the next line.
And the 'cite' package sorts your citations by their numerical order
of the corresponding references at the end of the paper, ridding you
from the need to notice that, e.g, ``Waldspurger'' appears after
``Arpachi-Dusseau'' when sorting references
alphabetically~\cite{waldspurger02,arpachiDusseau18:osbook}.
It'd be nice and thoughtful of you to include a suitable link in each
and every bibtex entry that you use in your submission, to allow
reviewers (and other readers) to easily get to the cited work, as is
done in all entries found in the References section of this document.
Now we're going take a look at Section~\ref{sec:figs}, but not before
observing that refs to sections and citations and such are colored and
clickable in the PDF because of the packages we've included.
%-----------------------------------------------------------------
% \label{sec:figs} 用于給本章節編號.
% \label{fig:vectors} 給\caption,即圖片的標題編號,
% \textbackslash 就是 `\`符號
% \textrm、\textsf、\texttt 都是設置字體:
% \textrm{} :Roman Family 羅馬字體
% \textsf{}:Scan Serif Family 無襯線字體
% \texttt{}:Typewriter Family 打字機字體
% \noindent 即不要縮進,表示承接上一段
% description, enumerate, itemize 都是列表,分別表示:
% description, 解說列表,可自定義標簽(文字或者圖標)
% enumerate, 有序列表
% itemize, 無序列表
% \includegraphics 用于插入圖片,可以插入pdf, 是放入矢量圖的神器
\section{Floating Figures and Lists}
\label{sec:figs}
\begin{figure}
\begin{center}
\begin{tikzpicture}
\draw[thin,gray!40] (-2,-2) grid (2,2);
\draw[<->] (-2,0)--(2,0) node[right]{$x$};
\draw[<->] (0,-2)--(0,2) node[above]{$y$};
\draw[line width=2pt,blue,-stealth](0,0)--(1,1)
node[anchor=south west]{$\boldsymbol{u}$};
\draw[line width=2pt,red,-stealth](0,0)--(-1,-1)
node[anchor=north east]{$\boldsymbol{-u}$};
\end{tikzpicture}
\end{center}
\caption{\label{fig:vectors} Text size inside figure should be as big as
caption's text. Text size inside figure should be as big as
caption's text. Text size inside figure should be as big as
caption's text. Text size inside figure should be as big as
caption's text. Text size inside figure should be as big as
caption's text. }
\end{figure}
% \begin{figure}
% \begin{center}
% \includegraphics{fig-1.pdf}
% \end{center}
% \caption{\label{fig:pdf} Text size inside figure should be as big as
% caption's text. Text size inside figure should be as big as
% caption's text. Text size inside figure should be as big as
% caption's text. Text size inside figure should be as big as
% caption's text. Text size inside figure should be as big as
% caption's text. }
% \end{figure}
% \begin{figure}
% \begin{center}
% \includegraphics[width=\linewidth]{1.pdf}
% \end{center}
% \caption{\label{fig:pdf-p1} Text size inside figure should be as big as
% caption's text. Text size inside figure should be as big as
% caption's text. Text size inside figure should be as big as
% caption's text. Text size inside figure should be as big as
% caption's text. Text size inside figure should be as big as
% caption's text. }
% \end{figure}
Here's a typical reference to a floating figure:
Figure~\ref{fig:vectors}. Floats should usually be placed where latex
wants then. Figure\ref{fig:vectors} is centered, and has a caption
that instructs you to make sure that the size of the text within the
figures that you use is as big as (or bigger than) the size of the
text in the caption of the figures. Please do. Really.
In our case, we've explicitly drawn the figure inlined in latex, to
allow this tex file to cleanly compile. But usually, your figures will
reside in some file.pdf, and you'd include them in your document
with, say, \textbackslash{includegraphics}.
Lists are sometimes quite handy. If you want to itemize things, feel
free:
\begin{description}
\item[fread] a function that reads from a \texttt{stream} into the
array \texttt{ptr} at most \texttt{nobj} objects of size
\texttt{size}, returning returns the number of objects read.
\item[Fred] a person's name, e.g., there once was a dude named Fred
who separated usenix.sty from this file to allow for easy
inclusion.
\end{description}
\noindent
The noindent at the start of this paragraph in its tex version makes
it clear that it's a continuation of the preceding paragraph, as
opposed to a new paragraph in its own right.
\subsection{LaTeX-ing Your TeX File}
%-----------------------------------
People often use \texttt{pdflatex} these days for creating pdf-s from
tex files via the shell. And \texttt{bibtex}, of course. Works for us.
\subsubsection{test}
textbackslash
%-------------------------------------------------------------------------------
\bibliographystyle{plain}
\bibliography{\jobname}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
附錄二
支持中文路徑的latex-workshop.latex.tools, 僅僅是修改了默認配置中的%DOC%為%DOCFILE%.
然后將其放入settings.json中.
"latex-workshop.latex.tools": [
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"-outdir=%OUTDIR%",
"%DOCFILE%"
],
"env": {}
},
{
"name": "lualatexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-lualatex",
"-outdir=%OUTDIR%",
"%DOCFILE%"
],
"env": {}
},
{
"name": "latexmk_rconly",
"command": "latexmk",
"args": [
"%DOCFILE%"
],
"env": {}
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOCFILE%"
],
"env": {}
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
],
"env": {}
},
{
"name": "rnw2tex",
"command": "Rscript",
"args": [
"-e",
"knitr::opts_knit$set(concordance = TRUE); knitr::knit('%DOCFILE_EXT%')"
],
"env": {}
},
{
"name": "jnw2tex",
"command": "julia",
"args": [
"-e",
"using Weave; weave(\"%DOC_EXT%\", doctype=\"tex\")"
],
"env": {}
},
{
"name": "jnw2texmintex",
"command": "julia",
"args": [
"-e",
"using Weave; weave(\"%DOC_EXT%\", doctype=\"texminted\")"
],
"env": {}
},
{
"name": "tectonic",
"command": "tectonic",
"args": [
"--synctex",
"--keep-logs",
"%DOC%.tex"
],
"env": {}
}
],
參考
- ^Overleaf Quick Start Guide https://github.com/overleaf/overleaf/wiki/Quick-Start-Guide
- ^Overleaf Toolkit https://github.com/overleaf/toolkit
- ^TeXLive https://www.tug.org/texlive/
- ^SumatraPDF support https://github.com/Hannah-Sten/TeXiFy-IDEA/wiki/SumatraPDF-support