LaTeX を VScode で書く(初心者のレポート作成向け)
作成日:2021年4月17日
作成者:basao
(はじめての記事なので分かりにくいところも多いかと思います.あらかじめご了承ください.)
はじめに
「レポートをLaTeXで書いてみたい.でも,環境構築が……」
そんな人のために,LaTeXをVScodeで書くための環境構築を紹介する.
この記事の内容をすべて行えば,ひとまず困らずにレポート作成ができるだろう.
なお,今回は
- VScode (text editer)
- SumatraPDF (PDF viewer)
を用いた環境構築を行う.
この記事の内容をすべて実行すると以下のことができるようになる.
- レポートのひな型を一瞬で作れる.
- ショートカットキーですぐにビルドできる.
- 作成されたPDFから修正したい点をダブルクリックするとコードの対象箇所にとべる.
少々長いがお付き合いいただきたい.
※作成者は Windos10 を使用しています.他のOSでは試せていないのでご了承ください.
各種ソフト
TeXLive (※作成段階で TexLive2021)
- TeXLiveのダウンロードページへ移動.
- install-tl-windows.exeをクリックし,インストーラをダウンロード.
- 先ほどダウンロードしたインストーラ(install-tl-windows.exe)を起動.
- 「TeXWorksをインストール」のチェックを外し,他は既定のままインストールすれば完了.(2時間程度,あるいはそれ以上かかります.)
Visual Studio code
- VScodeのダウンロードページへ移動.
- 自身の使用するOSに対応するインストーラをクリックしダウンロード.
- インストーラを起動.
- 全て既定の設定でインストールすれば完了.(すぐ終わるはず.)
SumatraPDF
- SumatraPDFのダウンロードページへ移動.
- SumatraPDF-3.2-64-install.exeをクリックし,インストーラをダウンロード.
- インストーラを起動.
- 全て既定の設定でインストールすれば完了.(すぐ終わるはず.)
拡張機能を追加
VScodeの拡張機能
VScodeを起動し,左にある田マークを押し,いかの拡張機能を入れられます.
- TeX Workshop (必須)
- CaTeX (任意, オススメ)
- LaTeX Utilities (任意, 作者は使ったことない)
SumatraPDF のsyntax設定
- SumatraPDFを起動.
- 左上の三本バーをクリック.
- 設定のオプションをクリック.
- 逆順検索コマンドラインの設定のところに以下のリンクを記述.
"C:\Users\**Username**\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd" -r -g "%f:%l"
ビルドの設定
設定
- VScodeを起動し,
ctr + ,
で設定を開く. - 右上のファイルマークをクリックし,
setting.json
を開く. - 最初の「 { 」の下に以下のコードを追加.
setting.json
// ---------- Language ----------
"[tex]": {
"editor.suggest.snippetsPreventQuickSuggestions": false,
"editor.tabSize": 4
},
"[latex]": {
"editor.suggest.snippetsPreventQuickSuggestions": false,
"editor.tabSize": 4
},
"[bibtex]": {
"editor.tabSize": 4
},
"window.zoomLevel": 1.5,
"latex-workshop.view.pdf.viewer": "external",
"latex-workshop.view.pdf.external.synctex.command": "C:/Users/**Username**/AppData/Local/SumatraPDF/SumatraPDF.exe",
"latex-workshop.view.pdf.external.synctex.args": [
"-reuse-instance",
"%PDF%",
"-forward-search",
"%TEX%",
"%LINE%",
"-inverse-search",
"\"C:\\Users\\tsuba\\AppData\\Local\\Programs\\Microsoft VS Code\\bin\\code.cmd\" -r -g \"%f:%l\""
],
"latex-workshop.latex.autoBuild.run": "onFailed",
"latex-workshop.latex.clean.fileTypes": [
"*.blg", "*.idx", "*.ind", "*.lof", "*.lot", "*.out", "*.acn", "*.acr", "*.alg", "*.glg", "*.glo", "*.gls", "*.ist", "*.fls", "*.log", "*.fdb_latexmk",
// for Beamer files
"_minted*", "*.nav", "*.snm", "*.vrb",
],
"latex-workshop.synctex.afterBuild.enabled": true,
"latex-workshop.latex.tools": [
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"-interaction=nonstopmode",
"%DOCFILE%"
]
},
{
"name":"ptex2pdf",
"command": "ptex2pdf",
"args": [
"-interaction=nonstopmode",
"-l",
"-ot",
"-kanji=utf8 -synctex=1",
"%DOC%"
]
},
{
"name":"ptex2pdf (uplatex)",
"command": "ptex2pdf",
"args": [
"-interaction=nonstopmode",
"-l",
"-u",
"-ot",
"-kanji=utf8 -synctex=1",
"%DOC%"
]
},
{
"name": "pbibtex",
"command": "pbibtex",
"args": [
"-kanji=utf8",
"%DOCFILE%"
]
}
],
"latex-workshop.latex.recipes": [
{
"name": "ptex2pdf (uplatex) *2",
"tools": [
"ptex2pdf (uplatex)",
"ptex2pdf (uplatex)"
]
},
{
"name": "ptex2pdf (uplatex) -> pbibtex -> ptex2pdf (uplatex) *2",
"tools": [
"ptex2pdf (uplatex)",
"pbibtex",
"ptex2pdf (uplatex)",
"ptex2pdf (uplatex)"
]
},
{
"name": "pbibtex",
"tools": [
"pbibtex"
]
},
{
"name": "ptex2pdf -> pbibtex -> ptex2pdf*2",
"tools": [
"ptex2pdf",
"pbibtex",
"ptex2pdf",
"ptex2pdf"
]
},
{
"name": "latexmk",
"tools": [
"latexmk (upLaTeX)"
]
},
{
"name": "pdflatex*2",
"tools": [
"pdflatex",
"pdflatex"
]
},
{
"name": "pdflatex -> bibtex -> pdflatex*2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
},
{
"name": "ptex2pdf*2",
"tools": [
"ptex2pdf",
"ptex2pdf"
]
},
],
"latex-workshop.message.latexlog.exclude": [
"(Font shape `(JY1|JT1|JY2|JT2)(/.*)(sl|it|sc)'.*|Some font shapes were not available.*)"
],
"editor.multiCursorModifier": "ctrlCmd",
"editor.formatOnPaste": true,
"extensions.autoUpdate": false,
"editor.wordSeparators": "./\\()\"'-:,.;<>~!@#$%^&*|+=[]{}`~?.。,、()「」[]{}《》てにをはがのともへでや",
-
ctr + shift + p
をタイプ. - "Open key board shortcut"と打ち,それを開く.
- "Build LaTeX with recipe"と打ち,ショートカットキーを設定.(
ctr + p, ctr + p
)
試しにビルド
- 以下のサンプルファイル( sample.tex )を VScode で作成.
sample.tex
\documentclass{ujarticle}
\begin{document}
\section{1章なんだな konnnitiha}
やっほー.
\begin{itemize}
\item はじまりました!
\end{itemize}
\end{document}
- 先ほど作成したショートカットキー(
ctr + p, ctr + p
) を打ち,一番上のやつを選択. - SumatraPDFが起動され,うまくビルドできていれば完了.
- 起動した SumatraPDF 上で修正したい点をダブルクリックし,VScodeの該当箇所にとべるか確認すること.
注意(慣れてる人向け)
- ビルドのレシピは自由に作りましょう.
- BibTeXを含む場合は2つ目を選択しましょう.
- 日本語じゃない場合は試していません.
私の好きなパッケージ(自分用なので無視してOK)
- リンクから必要なフォルダをフォルダごとダウンロード.
- zipに圧縮されている場合は解凍する.
- 解凍したパッケージをフォルダごと
C:\texlive\texmf-local\tex\latex\local
の階層にコピペ. - CommandPrompt で
mktexlsr
を実行.
スニペットの作成
- VScodeを起動.
- 左下の歯車をクリック.
- User Snippets をクリック.
- latex.json を選択し,開く.
- 最初の「 { 」の下に以下のコードを追加(自由に書き換えもOK).
- sampleファイル(sample.tex等)を作成して
report
,outline
,bibliography
などと打つと補完されるよ!
※1 biliography は別途 BibTeX の知識が必要になります.
※2 ここで示すスニペットは好きなパッケージをすべてインストールしないとビルドできません.
インストールしていない人は下まで進んでください.
latex.json
"report" : {
"prefix": "report",
"description": "レポート用テンプレート"
"body": [
"\\documentclass[a4paper,dvipdfmx,uplatex]{ujarticle}",
"",
"\\usepackage[top=40truemm,bottom=40truemm,left=30truemm,right=30truemm]{geometry}",
"\\usepackage{amsmath,amssymb}",
"\\usepackage{graphicx}",
"\\usepackage{color}",
"\\usepackage{ascmac}",
"\\usepackage{bm}",
"\\usepackage{float}",
"\\usepackage{framed}",
"\\usepackage{algorithm}",
"\\usepackage{algorithmic}",
"\\usepackage{rmss_symbol}",
"\\usepackage{url}",
"\\usepackage{hyperref}",
"",
"\\newtheorem{definition}{定義}",
"\\newtheorem{proposition}{命題}",
"\\newtheorem{theorem}{定理}",
"\\newtheorem{lemma}{補題}",
"\\newtheorem{assumption}{仮定}",
"",
"",
"\\begin{document}",
"",
"",
"\\vspace{1zh}",
"\\begin{center}",
"\\LARGE{",
"",
"\tタイトル",
"",
"}",
"\\end{center}",
"\\vspace{1zh}",
"\\begin{flushright}",
"",
"\t作成日:\\\\\\",
"\t交通制御学研究室 名前",
"",
"\\end{flushright}",
"\\vspace{1zh}",
"",
"",
"",
"",
"\\end{document}"
],
},
"outline" : {
"prefix": "outline",
"description": "概要のテンプレート",
"body": [
"",
"{\\normalsize %",
"\\begin{center}%",
"\\begin{minipage}{.8\\textwidth}%",
"\\vspace{1.0em} %",
"%",
"\\begin{center}",
"\\textbf{概要}",
"\\end{center}",
"",
"",
"\t概要を書きましょう.",
"",
"",
"\\end{minipage}%",
"\\end{center}%",
"}%",
""
]
},
"bibliography" : {
"prefix": "bibliography",
"description": "BibTeXを使うためのテンプレート",
"body": [
"\\bibliographystyle{unsrt}",
"\\bibliography{source.bib}"
]
}
- 以下のコードはパッケージをダウンロードしていない場合のスニペット.
インストールしていないパッケージの3行を削除したもの.
latex.json
"report" : {
"prefix": "report",
"description": "レポート用テンプレート"
"body": [
"\\documentclass[a4paper,dvipdfmx,uplatex]{ujarticle}",
"",
"\\usepackage[top=40truemm,bottom=40truemm,left=30truemm,right=30truemm]{geometry}",
"\\usepackage{amsmath,amssymb}",
"\\usepackage{graphicx}",
"\\usepackage{color}",
"\\usepackage{ascmac}",
"\\usepackage{bm}",
"\\usepackage{float}",
"\\usepackage{url}",
"\\usepackage{hyperref}",
"",
"\\newtheorem{definition}{定義}",
"\\newtheorem{proposition}{命題}",
"\\newtheorem{theorem}{定理}",
"\\newtheorem{lemma}{補題}",
"\\newtheorem{assumption}{仮定}",
"",
"",
"\\begin{document}",
"",
"",
"\\vspace{1zh}",
"\\begin{center}",
"\\LARGE{",
"",
"\tタイトル",
"",
"}",
"\\end{center}",
"\\vspace{1zh}",
"\\begin{flushright}",
"",
"\t作成日:\\\\\\",
"\t交通制御学研究室 名前",
"",
"\\end{flushright}",
"\\vspace{1zh}",
"",
"",
"",
"",
"\\end{document}"
],
},
"outline" : {
"prefix": "outline",
"description": "概要のテンプレート",
"body": [
"",
"{\\normalsize %",
"\\begin{center}%",
"\\begin{minipage}{.8\\textwidth}%",
"\\vspace{1.0em} %",
"%",
"\\begin{center}",
"\\textbf{概要}",
"\\end{center}",
"",
"",
"\t概要を書きましょう.",
"",
"",
"\\end{minipage}%",
"\\end{center}%",
"}%",
""
]
},
"bibliography" : {
"prefix": "bibliography",
"description": "BibTeXを使うためのテンプレート",
"body": [
"\\bibliographystyle{unsrt}",
"\\bibliography{source.bib}"
]
}