LaTeXとVSCodeとtextlintで作る論文執筆環境

この記事はImaizumi Lab Advent Calendarの2日目です。

はじめに

論文執筆シーズンがやってまいりました。

というわけでこの記事では、初めて論文を書く後輩に向けて私の執筆環境を紹介できればと思います。

私自身執筆環境は試行錯誤している真っ只中なので、いい環境が作れたらまた更新します。

VScode+LaTeX

まず、TeXVSCodeをインストールします。

Macの場合、

brew cask install mactex
brew cask install visual-studio-code

でインストールできます。

流れとしては、

となります。

この記事の通りに進めています(素晴らしい記事をありがとうございます!) qiita.com

多分上記の記事の通りにやればエラーが出ないはずなのですが、筆者の環境では実行時にコマンドがないと怒られてしまうため(which platexとかしたらちゃんとパスが通っているのに...)、応急処置としてフルパスで指定しています。

こんな感じです↓

[~/.latexmkrc]
  $latex                         = '/usr/local/texlive/2020/bin/x86_64-darwin/uplatex %O -synctex=1 -interaction=nonstopmode %S';
  $pdflatex                      = '/usr/local/texlive/2020/bin/x86_64-darwin/pdflatex %O -synctex=1 -interaction=nonstopmode %S';
  $lualatex                      = '/usr/local/texlive/2020/bin/x86_64-darwin/lualatex %O -synctex=1 -interaction=nonstopmode %S';
  $xelatex                       = '/usr/local/texlive/2020/bin/x86_64-darwin/xelatex %O -no-pdf -synctex=1 -shell-escape -interaction=nonstopmode %S';
  $biber                         = '/usr/local/texlive/2020/bin/x86_64-darwin/biber %O --bblencoding=utf8 -u -U --output_safechars %B';
  $bibtex                        = '/usr/local/texlive/2020/bin/x86_64-darwin/upbibtex %O %B';
  $makeindex                     = '/usr/local/texlive/2020/bin/x86_64-darwin/upmendex %O -o %D %S';
  $dvipdf                        = '/usr/local/texlive/2020/bin/x86_64-darwin/dvipdfmx %O -o %D %S';
  $dvips                         = '/usr/local/texlive/2020/bin/x86_64-darwin/dvips %O -z -f %S | convbkmk -u > %D';
  $ps2pdf                        = '/usr/local/texlive/2020/bin/x86_64-darwin/ps2pdf %O %S %D';
[~/settings.json]
(略)
"latex-workshop.latex.tools": [
        {
          "name": "Latexmk (XeLaTeX)",
          "command": "/usr/local/texlive/2020/bin/x86_64-darwin/latexmk",
          "args": [
            "-f", "-gg", "-pv", "-xelatex", "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%"
          ]
        },
        {
          "name": "Latexmk (upLaTeX)",
          "command": "/usr/local/texlive/2020/bin/x86_64-darwin/latexmk",
          "args": [
            "-f", "-gg", "-pv", "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%"
          ]
        },
        {
          "name": "Latexmk (pLaTeX)",
          "command": "/usr/local/texlive/2020/bin/x86_64-darwin/latexmk",
          "args": [
            "-f", "-gg", "-pv", "-latex='/usr/local/texlive/2020/bin/x86_64-darwin/platex'", "-latexoption='-kanji=utf8 -no-guess-input-env'", "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%"
          ]
        },
        {
          "name": "Latexmk (LuaLaTeX)",
          "command": "/usr/local/texlive/2020/bin/x86_64-darwin/latexmk",
          "args": [
            "-f", "-gg", "-pv", "-lualatex", "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%"
          ]
        }
    ],
(略)

textlint

textlintは日本語文章の校正ツールです。

主に技術文書として相応しくない表現や、単語の用法のミスなどを指摘してくれます。

f:id:RussianBlue25:20201202200052p:plain
textlintの警告の例

インストールは以下の通りです*1texファイルのあるディレクトリで実行してください。

事前にnpmコマンドが実行できる状態にしておいてください。(Node.jsについては本稿の範囲外とします)

npm install --save-dev textlint
npm install --save-dev textlint-filter-rule-comments
npm install --save-dev textlint-rule-preset-ja-spacing
npm install --save-dev textlint-rule-preset-ja-technical-writing
npm install --save-dev textlint-plugin-latex2e

次に定義ファイルを作ります。

./node_modules/.bin/textlint --init

で.textlintrcが作られます。

筆者の.textlintrcはこんな感じです。一部のルールを無視するようにしています。

[.textlintrc]
{
    "plugins": [
        //latex用プラグインを有効化 これは絶対必要
        "latex2e"                                                                                               
    ],
    "rules": {
        "preset-ja-spacing": true,
        "preset-ja-technical-writing": {
            "no-doubled-joshi": false,
            "ja-no-mixed-period": {
                "periodMark": ".",
            },
            "max-kanji-continuous-len": false,
            "sentence-length": false,
        },
        "preset-ja-engineering-paper": {
            "prh": false,
        }
    },
    "filters": {
        "comments": {
            // enable comment directive
            // if comment has the value, then enable textlint rule
            "enablingComment": "textlint-enable",
            // disable comment directive
            // if comment has the value, then disable textlint rule
           "disablingComment": "textlint-disable"
        }
    }
}

textlintを無視する

%textlint-disable
(無視したい文章)
%textlint-enable

でいけます。表などの前後で指定してやるといいでしょう。また、特定のルールのみを無視することもできます。

GitHub

端末は締め切り直前に壊れるものと相場は決まっています。リモートにしっかり保存しておきましょう。

GitHubに作成する場合は必ずプライベートリポジトリで作成してください。

gitの使い方やGitHubの使い方は本稿の範囲外とします。

.gitignore

gitの管理から外したいファイルを書いておきます。私のケースだとこんな感じです。

node_modules/*                                                                                                  
*.dvi
*.aux
*.fdb_latexmk
*.fls
*.log
*.pdf
*.synctex.gz
*.pptx
.DS_Store
*.eps
*.png

画像は基本的には管理から外していますが、すぐに作れない画像なんかは

git add -f hogehoge

でaddしてgitの管理下に置くこともあります。

git hook

リポジトリにpushする前にtextlintのチェックが通るかを検査します。通らなければpushは実行されません。

pushが実行されるかどうかにこだわりがなければ、Github Actionsを利用してもいいと思います。無料枠を使い潰すことはおそらくないと思うので...

cp .git/hooks/pre-push.sample .git/hooks/pre-push

とコピーし、以下のように追記します。

[.git/hooks/pre-push]
#句読点を変更
sed -e 's/、/,/g' main.tex > main2.tex
sed -e 's/。/./g' main2.tex > main3.tex
cp main3.tex main.tex
rm main2.tex main3.tex
#textlintを実行
./node_modules/.bin/textlint main.tex
#問題なかった時のみpushが実行される
exit $?

git hookを無視する

git hookを無視したい時には

git push --no-verify

でいけます(本稿のようにpush前に実行するようになっている場合)。

git hookの意味が全くないですが、テストを通さずにpushしたい時もあると思うので。

textlintでエラーが出た場合

textlintでTeXをチェックしている時、

'b' is not iterable (うろおぼえです)

のようなエラーが出てtextlintが動かないことがたまにあります。

この場合は\begin{comment}-\end{comment}がないかを確認してみてください。

私の環境で動かなかった時はコメントが原因で、削除すると動きました。

(2020/12/29追記)

textlint-plugin-latex2eのコントリビュータのkn1chtさんが修正されて、v1.0.4以降では'b' is not iterableは出なくなったようです。

おまけ:その他VSCodeのおすすめプラグイン

TODO Highlight

TODO:、FIXME: と入力したらハイライトしてくれます。

コメント部分がわかりづらいので入れておくといいと思います。

f:id:RussianBlue25:20201202134242p:plain
ハイライトの例

*1:グローバルインストールしてもいいのかも