LoginSignup
1
1
はじめての記事投稿
Qiita Engineer Festa20242024年7月17日まで開催中!

初心者のtex解説,布教活動×vscode

Last updated at Posted at 2024-06-27

初心者目線でtex解説とvscodeとtexの導入についてまとめました。この記事を機にtexに興味を持ち、導入してくれたら幸いです!

対象者

  • vscodeでtexを導入したい人
  • 大学のレポートでwordを使っているけど、なんかストレスを感じている人
  • texがなんなのか概要を知りたい人
  • texを使う上での言葉の意味をなんとなく知りたい人
  • texを使う上でぶち当たる問題の主な理由と解決策を知りたい人

少しtexを使い慣れた初心者がイメージで解説する記事なので内容がフワッとしています。そこのところ勘弁してください。
より詳しく正しい内容を知りたい方はご自身でググってください!!

texってなんなのさ

texは一言でいえば、「文書を作成するツール」です。wordとはまた違いますが、同じ用途で使うものになります。下みたいにコードを書いたらそれがPDF出力されて文書が作成されます。

スクリーンショット 2024-06-27 225335.png

wordに比べてよい点

僕が感じているのは以下の5つです。

1.数式、、、きれい
2.作業量が減り、ストレス減った
3.ページ番号、数式番号勝手に振ってくれる
4.レポートの仕上がりが綺麗で評価UP!
5. 簡単なのにプログラマーみたいでなんかかっこいい!!

2については後で話す「VSCode」で使える機能のことで、スニペットやgithubcopilotでchatGPTみたいに勝手に作文してくれる機能で効率が爆上がりしました。

texでの作業フロー

以下が僕が実際にtexで作業するときのフローになります。

1.VScodeにて適当にフォルダを作成し、その中に「~.tex」のtexファイルを作成します。「~」はなんでも大丈夫です。下のような感じ。
スクリーンショット 2024-06-27 230931.png
2.texファイルを編集します。まずスニペットを使って骨組みを1秒で作る。わからん時はググって、パッケージを見つけたりする。

#1.ドキュメントクラスの指定(ドキュメントについては後で説明)
\documentclass[12pt,a4paper]{jsarticle} 

#2.用いるパッケージの宣言(パッケージについては後で説明)
\usepackage[dvipdfmx]{graphicx}
\usepackage[dvipdfmx]{color}
\usepackage{here}
\usepackage[top=30truemm,bottom=30truemm,left=25truemm,right=25truemm]{geometry}
\usepackage{graphicx,amsmath,mathtools,siunitx,amssymb, amsfonts, latexsym,fancyhdr,lastpage,float}
\usepackage[hang,small,bf]{caption}
\usepackage[subrefformat=parens]{subcaption}
\captionsetup{compatibility=false}
\usepackage{indentfirst}
\usepackage{hyperref}
\renewcommand{\labelenumi}{(\arabic{enumi})}

#3.文書を書いていく
\begin{document}
\section{目的}
texをつかってレポートをかくすばらしさを伝えたい
\section{解説}
\begin{equation}
y = ax + b
\end{equation}
\begin{equation}
  \int_{a}^{b} f(x)dx=F(b)-F(a)
\end{equation}
\section{実験}
\subsection{結果}
\subsubsection{なんか}
\begin{thebibliography}{99}
\bibitem{citekey}hohoohohoho---\url{https://www.??????.com}
\end{thebibliography}
\end{document}

3.pdf出力します。初めてpdf出力する際は、「ctrl+alt+b」二回目以降は「ctrl+s」を実行します。
このとき、エラーが生じる場合があります。対応策を下の方にまとめたので、よければ参考にしてください!

スクリーンショット 2024-06-28 000258.png

4.2と3を文書が完成するまで繰り返します、、エラーチェックのためpdf出力はこまめにやるのがおすすめです。

texとVScodeを導入する

僕はこの記事で、latexmkrcファイルのコード内容とsetting.jsonのコード内容以外を参考にしました。
(同じlatexmkrcファイルにしたらpdf出力の度にpdfをみるための外部アプリケーション(adobe)が発動してまう、、、理由不明)
スニペットとgithubcopilotはかなり役立つのでやってほしい!!

以下が僕が友達からもらったlatexmkrcファイルとsetting.jsonです。
上記の記事でうまくいかない人は参考にしてみてください。
setting.jsonは文法エラーに気を付けてください。{}の中に,区切りで設定を列挙していきます。

.latexmkrc
#!/usr/bin/env perl

# LaTeX
$latex = 'platex -synctex=1 -halt-on-error -file-line-error %O %S';
$max_repeat = 5;

# BibTeX
$bibtex = 'pbibtex %O %S';
$biber = 'biber --bblencoding=utf8 -u -U --output_safechars %O %S';

# index
$makeindex = 'mendex %O -o %D %S';

# DVI / PDF
$dvipdf = 'dvipdfmx %O -o %D %S';
$pdf_mode = 3;

# preview
$pvc_view_file_via_temporary = 0;
if ($^O eq 'linux') {
    $dvi_previewer = "xdg-open %S";
    $pdf_previewer = "xdg-open %S";
} elsif ($^O eq 'darwin') {
    $dvi_previewer = "open %S";
    $pdf_previewer = "open %S";
} else {
    $dvi_previewer = "start %S";
    $pdf_previewer = "start %S";
}

# clean up
$clean_full_ext = "%R.synctex.gz"
setting.json
```以下の行からコピペしてください
{
"editor.guides.bracketPairs": true,
    "editor.linkedEditing": true,
    "[dart]": {
        "editor.formatOnSave": false,
        "editor.formatOnType": true,
        "editor.rulers": [
            80
        ],
        "editor.selectionHighlight": false,
        "editor.suggest.snippetsPreventQuickSuggestions": false,
        "editor.suggestSelection": "first",
        "editor.tabCompletion": "onlySnippets",
        "editor.wordBasedSuggestions": "off",
        "editor.defaultFormatter": "Dart-Code.dart-code",
        "editor.formatOnPaste": false,
    },
    "editor.unicodeHighlight.nonBasicASCII": false,
    "editor.fontFamily": "Source Han Code JP",
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
    "terminal.integrated.fontSize": 12,
    "terminal.integrated.enableMultiLinePasteWarning":"never",
    "code-runner.runInTerminal": true,
    "emmet.preferences": {

    
    },
    "code-runner.executorMap": {
        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o bin\\$fileNameWithoutExt && $dir\\bin\\$fileNameWithoutExt",
        "cpp": "(if not exist bin mkdir bin) & cd $dir && g++ $fileName -o bin\\$fileNameWithoutExt && $dir\\bin\\$fileNameWithoutExt",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "python -u",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "scheme": "csi -script",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run",
        "lisp": "sbcl --script",
        "kit": "kitc --run",
        "v": "v run",
        "sass": "sass --style expanded",
        "scss": "scss --style expanded",
        "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
        "FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "sml": "cd $dir && sml $fileName"
    },
    "git.autofetch": true,
    "git.enableSmartCommit": true,
    "git.confirmSync": false,
    "terminal.integrated.defaultProfile.windows": "Command Prompt",
    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "args": [
                "chcp 65001"
            ],
        }
    },
    "vsicons.dontShowNewVersionMessage": true,
    "[python]": {
        "editor.formatOnType": true
    },

    
    // ---------- Language ----------

    "[tex]": {
        // スニペット補完中にも補完を使えるようにする
        "editor.suggest.snippetsPreventQuickSuggestions": false,
        // インデント幅を2にする
        "editor.tabSize": 2
    },

    "[latex]": {
        // スニペット補完中にも補完を使えるようにする
        "editor.suggest.snippetsPreventQuickSuggestions": false,
        // インデント幅を2にする
        "editor.tabSize": 2,
        "editor.defaultFormatter": "James-Yu.latex-workshop"
    },

    "[bibtex]": {
        // インデント幅を2にする
        "editor.tabSize": 2
    },


    // ---------- LaTeX Workshop ----------

    // 使用パッケージのコマンドや環境の補完を有効にする
    "latex-workshop.intellisense.package.enabled": true,

    // 生成ファイルを削除するときに対象とするファイル
    // デフォルト値に "*.synctex.gz" を追加
    "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",
        "*.snm",
        "*.nav",
        "*.dvi",
        "*.synctex.gz"
    ],

    // 生成ファイルを "out" ディレクトリに吐き出す
    "latex-workshop.latex.outDir": "out",
    "latex-workshop.linting.chktex.enabled": false,
    "latex-workshop.message.latexlog.exclude": [".*"],

    // ビルドのレシピ
    "latex-workshop.latex.recipes": [
        {
            "name": "latexmk",
            "tools": [
                "latexmk"
            ]
        },
    ],

    // ビルドのレシピに使われるパーツ
    "latex-workshop.latex.tools": [

        {
            "name": "latexmk",
            "command": "latexmk",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-halt-on-error",
                "-pdfdvi",
                "-outdir=%OUTDIR%",
                "%DOC%"
            ],
            "env": {}
        },
    ],
    "cmake.configureOnOpen": true,
    "editor.inlineSuggest.enabled": true,
    "github.copilot.enable": {
        "*": true,
        "plaintext": false,
        "markdown": false,
        "scminput": false,
        "dart": true
    },
    "genieai.promptPrefix.addTests": "あなたはプロのプログラマーです。以下のコードに対するテストを実装してください。テストの説明は日本語で記述してください。",
    "editor.parameterHints.enabled": false,
    "editor.acceptSuggestionOnCommitCharacter": false,
    "editor.unicodeHighlight.ambiguousCharacters": false,
    "latex-workshop.linting.chktex.convertOutput.column.enabled": false,
    "latex-workshop.linting.chktex.exec.args": [
        "-wall",
        "-n22",
        "-n30",
        "-e16",
        "-q",
        "-n24"
    ],
    "explorer.autoReveal": false,
    "python.terminal.activateEnvironment": false,
    "workspaceExplorer.workspaceStorageDirectory": "E:\\Shortcuts",
    "window.zoomLevel": 1,
    "cmake.showOptionsMovedNotification": false,


    "workbench.colorCustomizations": {
        "editorStickyScroll.background": "#37567f",
        "editorStickyScrollHover.background": "#4e72a2"
    },
    "dart.vmAdditionalArgs": [
        "-Dfile.encoding=UTF-8"
    ]
    }

用語説明ふわっと

  • tex・・・文書を書くのに用いる鉛筆的な奴
  • VSCode・・・フォルダやファイルを操作するために必要な机
  • texlive・・・パッケージ(texでつかうツールが集まっている本みたいなもの)をPCにインストールするためのアプリケーション
  • スニペット・・・決めた言葉を入力するとあらかじめ設定したコードを勝手に書いてくれる機能
  • ドキュメントクラス・・・どんな文書を書くのかを指定する
  • パッケージ・・・texで使う機能
  • ビルド・・・プログラミングしたtexファイルをpdfに反映させること

ドキュメントクラスとパッケージの説明が以下になります。


\documentclass[12pt,a4paper]{jsarticle}  
#日本語の文書でフォント12pt,A4サイズのドキュメントクラス

\usepackage{amsmath}
#これからtexで使うパッケージ、下のequationを使うために宣言する。
PCにインストールしていないときはtexliveで「amsmath」と検索し、
インストールする必要がある

\begin{document}
\begin{equation} 
  \int_{a}^{b} f(x)dx=F(b)-F(a)
\end{equation}
\end{document}

  • githubcopilot・・・vscodeで使えるchatgpt的な奴、質問に答えてくれる、、勝手に作文してくれる、tabキーを押しておわり、、、本当におすすめ
  • latexmkrcファイル・・・tex文書の設定のファイル?詳しいことは知りません
  • setting.json・・・vscodeの設定をコードで書いたもの

エラー対応と理由集

まずやること

  • logファイル(実行履歴)で「間違っていますよ」と言われている行を特定する
  • ぐぐる
  • githubcopilotやchatgptに添削してもらう、エラーの文を貼り付ける

対応1
使いたいツールがあったけど、パッケージをtexliveでインストールしていなかった

対応2
そもそも文法間違えていた。({がないとか、打ち間違えとか)
こまめにctrl+sをおして、チェックするのがおすすめ

対応3
ドキュメントクラスが対応していないものになっている

対応4
setting.jsonの文法を間違えている

使っている外部サイト

エクセルのデータを表に変換してくれる便利なサイトです。

後は、githubcopilotやchatgptでも作文してくれたり、いろいろお世話になってます。

最後に

texは最初の方は、不慣れで時間がかかると思いますが、慣れると時短で綺麗なレポートが書けます。
スニペットやコードは誰かのモノをパクッて、よく使う定型文をスニペットにしたりするとかなり時短になるのでお勧めです。
word信仰者の皆さん、、wordを卒業して、texに入信してみてはどうでしょうか??

最後まで読んでいただきありがとうございました!!

1
1
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
1
1