Closed9
Ubuntu 24.04でLaTex環境を構築する

参考
- https://qiita.com/JJ1LIS/items/112253d036b3ed296da1
- https://qiita.com/Hiroya_W/items/9d589ec622cd2cfddd19
- https://qiita.com/rainbartown/items/d7718f12d71e688f3573
- https://zenn.dev/hash_yuki/articles/31855fbdb5fdf7
- https://qiita.com/fuku_uma/items/e5ad46125a9612320273
- https://texwiki.texjp.org/?TeX Live
- https://qiita.com/shun-shobon/items/91f08d02f2fd13c33d46

使用する環境
- Ubuntu 24.04
- VS Code
- Tex Live
- latexmkrc
- uplatex
- jlreq

Tex Liveのインストール
Texを使うにはとりあえず、Tex Liveを入れればいいらしい。
Tex LiveはLaTexのデファクト・スタンダードとなっているディストリビューション。
下記コマンドでインストールできる(インストールには1時間くらいかかる)。
多分必要パッケージのみに絞れば時間短縮できると思う。
apt
だと古いらしいけど多分問題ない。
sudo apt install -y texlive-full
インストールできたか確認。
uplatex --version
e-upTeX 3.141592653-p4.1.0-u1.29-230214-2.6 (utf8.uptex) (TeX Live 2023/Debian)
kpathsea version 6.3.5
ptexenc version 1.4.3
Copyright 2023 D.E. Knuth.
There is NO warranty. Redistribution of this software is
covered by the terms of both the e-upTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the e-upTeX source.
Primary author of e-upTeX: Japanese TeX Development Community.
latexmk --version
Latexmk, John Collins, 31 Jan. 2024. Version 4.83

VS Code 拡張機能:LaTeX Workshop
LaTeX Workshopをインストールする。
Tex Workshopではないので注意。
code --install-extension James-Yu.latex-workshop
Installing extensions...
Installing extension 'james-yu.latex-workshop'...
Extension 'james-yu.latex-workshop' v10.9.1 was successfully installed.

作業ディレクトリを作る
以降はこのディレクトリで作業する。
mkdir latex
cd latex
code latex

ビルド設定
デフォルトでは日本語対応していないので、設定を変更する。
英語圏だとpdftexによって、texから直接PDF出力するらしいが、pdftexは日本語対応していない。
日本語対応はplatex, uplatex, xelatex, lualatexなどがあるらしい。[1]
今回はuplatexを選定した。
.latexmkrc
ビルドはlatexmk[2]によってワンコマンドで実行できるようにする。
VS Code拡張機能があるからlatexmkの使用は必須ではないかも。
シンボリックリンクで管理する。
touch ~/.latexmkrc
ln -s ~/.latexmkrc .latexmkrc
.latexmkrc
#!/usr/bin/env perl
$pdf_mode = 3;
$latex = 'uplatex -synctex=1 -halt-on-error -file-line-error %O %S';
$bibtex = 'upbibtex %O %S';
$dvipdf = 'dvipdfmx %O -o %D %S';
$makeindex = 'mendex %O -o %D %S';
settings.json
.vscode/settings.json
{
"latex-workshop.intellisense.package.enabled": true,
"latex-workshop.latex.autoClean.run": "onBuilt",
"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"
],
"latex-workshop.latex.outDir": "build",
"latex-workshop.view.pdf.viewer": "tab",
"latex-workshop.latex.recipes": [
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
],
"latex-workshop.latex.tools": [
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-silent",
"-interaction=nonstopmode",
"-outdir=%OUTDIR%",
"%DOC%"
],
},
]
}

書いてみる
jlreqを使用すべきらしい[1]。
test.tex
\documentclass[uplatex,dvipdfmx]{jlreq}
\usepackage{amsmath,amssymb}
\usepackage{mathtools}
\title{\LaTeX テスト}
\author{吉川 輝}
\begin{document}
\maketitle
\section{第一部}
\subsection{第一章}
この文章は \LaTeX で書かれています。
$$
ax + by + c = 0
$$
\end{document}

表紙が指定されている場合
一旦内容を記入してpdfとして保存する。
\documentclass[uplatex,dvipdfmx]{jlreq}
\usepackage{pdfpages}
\begin{document}
% pdfをinclude
\includepdf[pages=-]{表紙.pdf}
% 目次を表示する
\tableofcontents
% ここから本文
\section{第一部}
\end{document}

ソースコードを書く
listings
を使用する。
\usepackage{listings,jlisting}
\begin{lstlisting}
#include <iostream>
int main() {
std::cout << "Hello world" << std::endl;
}
\end{lstlisting}
jlisting
をインストールしないと、日本語のコメントがバグる。
このスクラップは3ヶ月前にクローズされました