WindowsでVSCodeを使用する際のLaTeXの日本語文字化けと表示問題の解決方法

1. VSCodeの設定ファイル(settings.json)の構成

LaTeX Workshop拡張機能の動作を最適化するために、settings.jsonファイルに以下の設定を追加します。これにより、コンパイルプロセス、エラーハンドリング、およびビューアの動作が制御されます。

//------------------------------LaTeX 設定----------------------------------
// 自動コンパイルの有効化
"latex-workshop.latex.autoBuild.enabled": false,
// コンテキストメニューの表示
"latex-workshop.showContextMenu": true,
// パッケージからの自動補完機能
"latex-workshop.intellisense.package.enabled": true,
// エラーメッセージの非表示設定
"latex-workshop.message.error.show": false,
"latex-workshop.message.warning.show": false,
// コンパイルツールとコマンドの定義
"latex-workshop.latex.compilerTools": [
    {
        "name": "compilerXeLaTeX",
        "command": "xelatex",
        "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "%DOCFILE%"
        ]
    },
    {
        "name": "compilerPdfLaTeX",
        "command": "pdflatex",
        "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "%DOCFILE%"
        ]
    },
    {
        "name": "compilerLatexMk",
        "command": "latexmk",
        "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "-pdf",
            "-outdir=%OUTDIR%",
            "%DOC%"
        ]
    },
    {
        "name": "compilerLuaLaTeX",
        "command": "lualatex",
        "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "-shell-escape",
            "%DOCFILE%"
        ]
    },
    {
        "name": "compilerBibTeX",
        "command": "bibtex",
        "args": [
            "%DOCFILE%"
        ]
    }
],
// コンパイルシーケンス(レシピ)の定義
"latex-workshop.latex.buildSequences": [
    {
        "name": "buildPdfLaTeX",
        "tools": [
            "compilerPdfLaTeX"
        ]
    },
    {
        "name": "buildLuaLaTeX",
        "tools": [
            "compilerLuaLaTeX"
        ]
    },
    {
        "name": "buildXeLaTeX",
        "tools": [
            "compilerXeLaTeX"
        ]
    },
    {
        "name": "buildBibTeX",
        "tools": [
            "compilerBibTeX"
        ]
    },
    {
        "name": "buildLatexMk",
        "tools": [
            "compilerLatexMk"
        ]
    },
    {
        "name": "buildXeLaTeXWithBibTeX",
        "tools": [
            "compilerXeLaTeX",
            "compilerBibTeX",
            "compilerXeLaTeX",
            "compilerXeLaTeX"
        ]
    },
    {
        "name": "buildPdfLaTeXWithBibTeX",
        "tools": [
            "compilerPdfLaTeX",
            "compilerBibTeX",
            "compilerPdfLaTeX",
            "compilerPdfLaTeX"
        ]
    }
],
// ファイルクリーンアップの対象ファイルタイプ
"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"
],
// コンパイル失敗時の自動クリーンアップ
"latex-workshop.latex.autoClean.run": "onFailed",
// 最後に使用したレシピをデフォルトとして使用
"latex-workshop.latex.recipe.default": "lastUsed",
// PDFビューアの設定
"latex-workshop.view.pdf.viewer": "tab",
"latex-workshop.view.pdf.ref.viewer": "auto",
"editor.renderControlCharacters": false

2. LaTeXソースファイルへのパッケージの追加

LaTeXドキュメントの先頭(例えば\documentclassの直後)に、以下の行を追加します。これは日本語を正しく処理するために必要なctexパッケージを読み込みます。

\usepackage[documentEncoding]{ctex}

ここで、documentEncodingは、VSCodeの右下隅に表示されるファイルのエンコーディング(例:GBK、UTF8など)と一致させる必要があります。不一致があると、文字化けやコンパイルエラーの原因となります。

タグ: LaTeX VSCode ctex GBK UTF-8

6月4日 22:32 投稿