LoginSignup
2
3

More than 1 year has passed since last update.

【MacOS】VScodeでTexを使う

Last updated at Posted at 2023-02-19

はじめに 

本ページでは、VScodeを使った環境構築について説明した備忘録である。主に「ゼロからVSCodeでTeX環境構築(Mac Big Sur)」を 参考にしており、基本的にはその流れに従うこととする。詳細については、そちらのページを参考にする形として、主に作業内容などについて記載する。

動作環境

  • Mac mini M2 Pro(2023)
  • macOS 13.2.1 (22D68)
  • VScode 1.75.1 (Universal)

ターミナル

以下では、ターミナルで実行すべき操作について主に書いていく。

Homebrewのインストール

Homebrewのページに記載されている以下のコマンドをターミナルで実行する。

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Screenshot 2023-02-19 at 11.08.54.png
少し時間がかかりますが、おとなしく待ちましょう。以下のような結果が出力されたら、その指示に従って、コマンドを実行しましょう。なお、以下のコマンドはその一例である。

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/[username]/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"

コマンドを打ったのちに、Homebrewが動作していることを確認するために、以下のコマンドを打ってみましょう。

$ brew --version
Homebrew 4.0.1

上記のような結果が得られば、Homebrewのインストールは終了である。

MacTexのインストール

MacTexについて詳しいことは、「MacTex」を参照されたい。

GUI-Applications なしの場合

以下のコマンドをターミナルで実行する。

$ brew install --cask mactex-no-gui

1つ目のコマンドの実行後、
🍺 mactex-no-gui was successfully installed!
と出力されたら、インストールは終了である。

VScode

以下では、VScodeで行う操作について主に書いていく。

VSCodeで拡張機能(LaTeX Workshop)をインストールする

VScodeの拡張機能である(Latex Workshop)をインストールする。
image.png

setiing.jsonの編集

  1. VScodeを立ち上げ、左下の設定ボタン(歯車のマーク)をクリックし、設定を開く。
  2. 設定の右上、
    image.png
    ボタンをクリックし、setiing.jsonを開く。
    ここで、必要なものを追加、編集していく。
  3. 追記が必要なのは以下の内容である。
   {

		"latex-workshop.latex.outDir": "./out",	
		"latex-workshop.latex.autoClean.run": "onBuilt",
		"latex-workshop.latex.autoBuild.run": "onSave",
		

		"latex-workshop.view.pdf.viewer": "tab",
	
		"latex-workshop.latex.recipes": [

			{
				"name": "latexmk (lualatex)🧋",
				"tools": [
					"lualatexmk"
				]
			},
			
			{
				"name": "latexmk 🔃",
				"tools": [
					"latexmk"
				]
			},
			{
				"name": "latexmk (latexmkrc)",
				"tools": [
					"latexmk_rconly"
				]
			},
			{
				"name": "pdflatex ➞ bibtex ➞ pdflatex × 2",
				"tools": [
					"pdflatex",
					"bibtex",
					"pdflatex",
					"pdflatex"
				]
			},
			{
				"name": "Compile Rnw files",
				"tools": [
					"rnw2tex",
					"latexmk"
				]
			},
			{
				"name": "Compile Jnw files",
				"tools": [
					"jnw2tex",
					"latexmk"
				]
			},
			{
				"name": "tectonic",
				"tools": [
					"tectonic"
				]
			}
			
		],
	
		"latex-workshop.latex.tools": [
			{
				"name": "lualatexmk",
				"command": "latexmk",
				"args": [
					"-synctex=1",
					"-file-line-error",
					"-interaction=nonstopmode",
					"-halt-on-error",
					"-lualatex",
					"-outdir=%OUTDIR%",
					"%DOC%"
				],
				"env": {}
			},

			{
				"name": "latexmk",
				"command": "latexmk",
				"args": [
					"-synctex=1",
					"-interaction=nonstopmode",
					"-file-line-error",
					"-pdf",
					"-outdir=%OUTDIR%",
					"%DOC%"
				],
				"env": {}
			},
			{
				"name": "latexmk_rconly",
				"command": "latexmk",
				"args": [
					"%DOC%"
				],
				"env": {}
			},
			{
				"name": "pdflatex",
				"command": "pdflatex",
				"args": [
					"-synctex=1",
					"-interaction=nonstopmode",
					"-file-line-error",
					"%DOC%"
				],
				"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": {}
			}
			
		],

setting.jsonを編集した後に保存し、VScodeを再起動すると良い。

注意事項

  • SyncTexについて
    この設定では、表示しているpdfの必要な部分をcmdを押しながらクリックすることで、codeの該当部分に飛ぶ設定になっている。この機能は文書が長くなればなるほど寵愛する機能であるため、ぜひ慣れておくと良いだろう。

Sample

これらの設定を使った、Sampleを以下に示す。

\documentclass{ltjsarticle}

\begin{document}
これはSampleです。
\end{document}

これをコンパイル(cmd+s)して、pdfを表示(cmd+op+v)で以下の画像が表示されれば環境構築は完了である。
Screenshot 2023-02-19 at 11.49.33.png

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3