# VSCode + $\LaTeX$ Workspace + TexLive
###### tags: `NKUST` `碩士`
:::danger
**本篇已過時,請閱讀最新的[文件](https://hackmd.io/@yuhao-kuo/ryBAReRQY)。**
:::
:::info
環境摘要: ubuntu 18.04 LTS
:::
## 請先安裝下列套件
* [perl](https://www.perl.org/get.html)
* [latexmk](https://mg.readthedocs.io/latexmk.html#installation)
* [vscode](https://code.visualstudio.com/)
* TexLive
## TexLive安裝
```
$ sudo apt install texlive-full
```
```
$ sudo apt install perl-tk
```
```
$ sudo apt install perl-doc
```
```
$ sudo apt-get install biber
```
## vscode 中的組件
* [LateX Workspace](https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop)
* [LateX language support](https://marketplace.visualstudio.com/items?itemName=torn4dom4n.latex-support)
## 設定
:::success
你可以直接設定到 .vscode/settings.json 中,也可以將此設定加入到您的 VSCode 設定中(如下圖)。
:::

因為目前不清楚有多少人想用按鈕的方式進行編譯,因此之後會加入一個相依於vscode-latex-workshop 的設定檔。
## 套件設定檔
:::warning
此設定檔暫時失效!
:::
:::success
如果您的環境一直會編譯失敗,我們建議您採用下列的作法,將 `vscode` 的預設值改為我們建議的環境。
:::
請建立在 `.vscode` 目錄中的 `setting.json` 中。
```josn
{
"latex-workshop.latex.recipes": [
{
"name": "xelatex",
"tools": [
"xelatex"
]
},
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
{
"name": "pdflatex -> bibtex -> pdflatex*2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
}
],
"latex-workshop.latex.tools": [
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"%DOC%"
]
},
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
]
}
],
"latex-workshop.view.pdf.viewer": "tab",
"latex-workshop.latex.clean.fileTypes": [
"*.aux",
"*.bbl",
"*.blg",
"*.idx",
"*.ind",
"*.lof",
"*.lot",
"*.out",
"*.toc",
"*.acn",
"*.acr",
"*.alg",
"*.glg",
"*.glo",
"*.gls",
"*.ist",
"*.fls",
"*.log",
"*.fdb_latexmk"
]
}
```
```json=
{
"latex-workshop.latex.outDir": "%DIR%/build", // 輸出在main目錄底下的build
"latex-workshop.latex.external.build.command": "make",
"latex-workshop.latex.external.build.args": ["all"],
"latex-workshop.view.pdf.viewer": "tab",
"latex-workshop.latex.clean.fileTypes": [
"*.aux",
"*.bbl",
"*.blg",
"*.idx",
"*.ind",
"*.lof",
"*.lot",
"*.out",
"*.toc",
"*.acn",
"*.acr",
"*.alg",
"*.glg",
"*.glo",
"*.gls",
"*.ist",
"*.fls",
"*.log",
"*.fdb_latexmk",
"*.xwm",
"*.sta",
"*.bcf",
"*.run",
"*.xml",
"*.gz"
]
}
```
## 編譯教學
請搭配Makefile進行編譯
:::success
Makefile 檔案內容
:::
```Makefile=
# 輸出檔案名稱(不需加入副檔名)
DOCNAME=main
# 輸出檔案存放位置(預設為build)
OUTDIR=build
# LaTeX Compile
LATEXCOMPILE=xelatex
# bib Compile
BIBCOMPILE=biber
# shell
SHELL:=/bin/bash
# include make script
include ../Scripts/clean.mk
include ../Scripts/build.mk
all:
@make buildlbll
```
build.mk
```=
DOC=${DOCNAME}.tex
DOCFILE=${DOCNAME}
LATEXARGS=-synctex=1 -interaction=nonstopmode -file-line-error --output-directory=${OUTDIR}
BIBARGS=--output-directory=${OUTDIR}
buildlatex:
${LATEXCOMPILE} ${LATEXARGS} ${DOC}
buildbib:
${BIBCOMPILE} ${BIBARGS} ${DOCFILE}
buildlbll:
@mkdir -p ${OUTDIR}
@make buildlatex
@make buildbib
@make buildlatex
@make buildlatex
```
clean.mk
```
SHELL:=/bin/bash
distclean:
ifeq ($(shell [ -e $(OUTDIR) ] && echo "1" || echo "0"), 1)
@$(shell rm -rf ${OUTDIR})
@echo -n ""
else
@echo -n ""
endif
clean:
ifeq ($(shell [ -e $(OUTDIR) -o -d $(OUTDIR) ] && echo "1" || echo "0"), 1)
@$(shell mkdir -p /tmp/nkustthesislatex.tmp)
@$(shell cp $(OUTDIR)/*.pdf /tmp/nkustthesislatex.tmp/.)
@$(shell rm -rf ${OUTDIR})
@$(shell mkdir $(OUTDIR))
@$(shell mv /tmp/nkustthesislatex.tmp/*.pdf $(OUTDIR)/.)
@$(shell rm -rf /tmp/nkustthesislatex.tmp)
@echo -n ""
else
@echo -n ""
endif
```
當您儲存的時候就會自動編譯 LateX 檔案。
## 參考資料
* [VS Code 配置LaTeX环境(MikTex)](http://blog.xhyeax.com/2019/09/14/vscode-latex/)
* [Linux 配置 VS Code Latex环境](https://www.jianshu.com/p/ebebf8cfff2e)