0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LaTeXをVScodeで書く(初心者のレポート向け)

Posted at

LaTeX を VScode で書く(初心者のレポート作成向け)

作成日:2021年4月17日
作成者:basao
(はじめての記事なので分かりにくいところも多いかと思います.あらかじめご了承ください.)

はじめに

「レポートをLaTeXで書いてみたい.でも,環境構築が……」
そんな人のために,LaTeXをVScodeで書くための環境構築を紹介する.
この記事の内容をすべて行えば,ひとまず困らずにレポート作成ができるだろう.

なお,今回は

  • VScode (text editer)
  • SumatraPDF (PDF viewer)

を用いた環境構築を行う.

この記事の内容をすべて実行すると以下のことができるようになる.

  • レポートのひな型を一瞬で作れる.
  • ショートカットキーですぐにビルドできる.
  • 作成されたPDFから修正したい点をダブルクリックするとコードの対象箇所にとべる.

少々長いがお付き合いいただきたい.

※作成者は Windos10 を使用しています.他のOSでは試せていないのでご了承ください.

各種ソフト

TeXLive (※作成段階で TexLive2021)

  1. TeXLiveのダウンロードページへ移動.
  2. install-tl-windows.exeをクリックし,インストーラをダウンロード.
  3. 先ほどダウンロードしたインストーラ(install-tl-windows.exe)を起動.
  4. 「TeXWorksをインストール」のチェックを外し,他は既定のままインストールすれば完了.(2時間程度,あるいはそれ以上かかります.)

Visual Studio code

  1. VScodeのダウンロードページへ移動.
  2. 自身の使用するOSに対応するインストーラをクリックしダウンロード.
  3. インストーラを起動.
  4. 全て既定の設定でインストールすれば完了.(すぐ終わるはず.)

SumatraPDF

  1. SumatraPDFのダウンロードページへ移動.
  2. SumatraPDF-3.2-64-install.exeをクリックし,インストーラをダウンロード.
  3. インストーラを起動.
  4. 全て既定の設定でインストールすれば完了.(すぐ終わるはず.)

拡張機能を追加

VScodeの拡張機能

VScodeを起動し,左にある田マークを押し,いかの拡張機能を入れられます.

  • TeX Workshop (必須)
  • CaTeX (任意, オススメ)
  • LaTeX Utilities (任意, 作者は使ったことない)

SumatraPDF のsyntax設定

  1. SumatraPDFを起動.
  2. 左上の三本バーをクリック.
  3. 設定のオプションをクリック.
  4. 逆順検索コマンドラインの設定のところに以下のリンクを記述.
    "C:\Users\**Username**\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd" -r -g "%f:%l"

ビルドの設定

設定

  1. VScodeを起動し,ctr + ,で設定を開く.
  2. 右上のファイルマークをクリックし,setting.jsonを開く.
  3. 最初の「 { 」の下に以下のコードを追加.
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": "./\\()\"'-:,.;<>~!@#$%^&*|+=[]{}`~?.。,、()「」[]{}《》てにをはがのともへでや",
  1. ctr + shift + pをタイプ.
  2. "Open key board shortcut"と打ち,それを開く.
  3. "Build LaTeX with recipe"と打ち,ショートカットキーを設定.(ctr + p, ctr + p)

試しにビルド

  1. 以下のサンプルファイル( sample.tex )を VScode で作成.
sample.tex
    \documentclass{ujarticle}

    \begin{document}

    \section{1章なんだな konnnitiha}
        やっほー.
        \begin{itemize}
            \item はじまりました!
        \end{itemize}

    \end{document}
  1. 先ほど作成したショートカットキー(ctr + p, ctr + p) を打ち,一番上のやつを選択.
  2. SumatraPDFが起動され,うまくビルドできていれば完了.
  3. 起動した SumatraPDF 上で修正したい点をダブルクリックし,VScodeの該当箇所にとべるか確認すること.

注意(慣れてる人向け)

  • ビルドのレシピは自由に作りましょう
  • BibTeXを含む場合は2つ目を選択しましょう
  • 日本語じゃない場合は試していません.

私の好きなパッケージ(自分用なので無視してOK)

  1. リンクから必要なフォルダをフォルダごとダウンロード.
  2. zipに圧縮されている場合は解凍する.
  3. 解凍したパッケージをフォルダごとC:\texlive\texmf-local\tex\latex\localの階層にコピペ.
  4. CommandPrompt で mktexlsr を実行.

スニペットの作成

  1. VScodeを起動.
  2. 左下の歯車をクリック.
  3. User Snippets をクリック.
  4. latex.json を選択し,開く.
  5. 最初の「 { 」の下に以下のコードを追加(自由に書き換えもOK).
  6. sampleファイル(sample.tex等)を作成してreportoutlinebibliographyなどと打つと補完されるよ!

※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}"
		]
	}

参考ページ

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?