跳至主要內容

使用 pLaTeX 製作日文 PDF 的方法

· 11 分鐘閱讀

整理了使用 pLaTeX 製作日文 PDF 文件的步驟。從範本的構成到編譯,以及常見錯誤的處理都會說明。

pLaTeX 是什麼

pLaTeX 是支援日文組版的 LaTeX 實作。使用 platex 指令編譯 .tex 檔,然後用 dvipdfmx 轉成 PDF。

因為包含在 TeX Live 中,如果已安裝 TeX Live 就可以立即使用。TeX Live 的安裝步驟請參考「在 Linux 上安裝 TeX Live 2026」。

選擇文件類別

在日文文件中可使用以下類別。

類別用途
jarticle論文、報告等短篇文件
jbook書籍、有章節的長篇文件
jreport技術報告(含 abstract + chapter 結構)
beamer投影片(簡報)

範本(jarticle)

使用 jarticle 的基本範例如下。

顯示範例程式碼
% pLaTeX 範例文件 — 主要功能概覽
\documentclass[a4paper,12pt]{jarticle}

%% ========== 套件 ==========
\usepackage{amsmath, amssymb} % 數學式強化
\usepackage{graphicx} % 插入圖像
\usepackage{color} % 顏色設定
\usepackage{fancyhdr} % 頁首/頁尾
\usepackage{geometry} % 頁面版面
\usepackage{enumerate} % 列舉自訂
\usepackage{url} % URL 顯示
\usepackage{multicol} % 分欄
\usepackage{booktabs} % 高品質表格線
\usepackage{array} % 表格欄格式擴充
\usepackage{verbatim} % verbatim 環境
\usepackage{ascmac} % 框線環境(screen, itembox 等)
\usepackage{okumacro} % Ruby・圓點等(pLaTeX 常用)
\usepackage{setspace} % 行距調整
\usepackage{listings} % 原始碼清單
\usepackage{xcolor} % 顏色(listings 用)
\usepackage{caption} % 標題樣式

%% ========== 頁面版面 ==========
\geometry{top=25mm, bottom=25mm, left=25mm, right=25mm}
\setlength{\headheight}{17pt}
\addtolength{\topmargin}{-5pt}

%% ========== 頁首/頁尾 ==========
\pagestyle{fancy}
\fancyhf{}
\lhead{pLaTeX 範例文件}
\rhead{\today}
\cfoot{\thepage}
\renewcommand{\headrulewidth}{0.4pt}

%% ========== listings 設定 ==========
\lstset{
basicstyle=\ttfamily\small,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{green!50!black},
stringstyle=\color{red!70!black},
numbers=left,
numberstyle=\tiny\color{gray},
frame=single,
breaklines=true,
tabsize=4,
}

%% ========== 標題資訊 ==========
\title{\textbf{pLaTeX 主要功能範例}\\[5pt]
\large --- 日文 \LaTeX 的典型用法 ---}
\author{範例太郎\thanks{範例大學 資訊工程系}
\and 範例花子}
\date{\today}

%% ================================================================
\begin{document}
%% ================================================================

\maketitle
\thispagestyle{fancy}

\begin{abstract}
本文檔是示範 pLaTeX 主要功能的範例。
依序說明文件類別與套件的讀入、日文組版特有的功能、
數學式、表格、圖形、交叉參照、腳註、Ruby、圓點、
原始碼清單、分欄、自訂指令等內容。
\end{abstract}

\tableofcontents
\newpage

%% ================================================================
\section{日文組版的基本}
%% ================================================================

\subsection{和文・歐文的混合}

pLaTeX 可以自然地混合日文與歐文。
例如「Hello, World!」或 \texttt{LaTeX2e} 類的
ASCII 文字會自動插入適當的空白。

全形字與半形字之間的間距調整由 pLaTeX 自動處理:
日本語English日本語、數字123日本語、記號\%日本語。

\subsection{Ruby(振假名)}

使用 \texttt{okumacro} 套件的 \verb|\ruby| 指令可以加上 Ruby(振假名)。

\begin{center}
\ruby{漢字}{かんじ}\ruby{情報処理}{じょうほうしょり}
\ruby{自然言語}{しぜんげんご}\ruby{処理}{しょり}
\end{center}

\subsection{圓點(傍點)}

使用 \texttt{okumacro}\verb|\kenten| 可以加上圓點(傍點)。

\begin{center}
\kenten{重要な箇所}には圏点を打つことができる。
\end{center}

\subsection{字體大小}

{\tiny 極小} {\scriptsize 腳註} {\footnotesize} {\small 稍小}
{\normalsize 標準} {\large 稍大} {\Large} {\LARGE 很大}
{\huge 超大} {\Huge 最大}

\subsection{文字裝飾}

\begin{itemize}
\item \textbf{粗體(Bold)}
\item \textit{斜體(英文字)}
\item \textsl{斜向}
\item \textsc{Small Caps}
\item \texttt{等寬字體(Typewriter)}
\item \underline{底線文字}
\item \textcolor{red}{紅色文字}
\item \textcolor{blue!70!black}{藍色文字}
\item \colorbox{yellow}{標示底色}
\end{itemize}

%% ================================================================
\section{文件結構}
%% ================================================================

\subsection{標題層級}

在 pLaTeX(\texttt{jarticle})中可以使用以下標題層級:
\verb|\section|、\verb|\subsection|、\verb|\subsubsection|、
\verb|\paragraph|、\verb|\subparagraph|。

\subsubsection{子子節範例}
這是子子節的範例。

\paragraph{段落標題}
這是段落標題的範例。接續的內文沒有縮排。

\subsection{腳註}

可以在內文中加入腳註\footnote{這是腳註的文字。會自動配置在頁底。}
也可以使用多個腳註\footnote{第二個腳註。編號會自動遞增。}

\subsection{交叉參照}

加上 \verb|\label| 之後,
可以用 \verb|\ref| 或 \verb|\pageref| 來參照。
例:下一節是第~\ref{sec:math}~節(第~\pageref{sec:math}~頁)。

%% ================================================================
\section{數學式環境}
\label{sec:math}
%% ================================================================

\subsection{行內數學式與顯示數學式}

行內數學式:$E = mc^2$$\alpha + \beta = \gamma$
$\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$

顯示數學式:
\[
\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}
\]

\subsection{equation・align 環境}

帶編號的數學式(\texttt{equation}):
\begin{equation}
\label{eq:euler}
e^{i\pi} + 1 = 0
\end{equation}
式~\eqref{eq:euler} 為歐拉恆等式。

多行對齊(\texttt{align}):
\begin{align}
(a+b)^2 &= a^2 + 2ab + b^2 \\
(a-b)^2 &= a^2 - 2ab + b^2 \\
(a+b)(a-b) &= a^2 - b^2
\end{align}

\subsection{矩陣・向量}

\begin{equation}
A = \begin{pmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{pmatrix},\quad
\det(A) = a_{11}a_{22} - a_{12}a_{21}
\end{equation}

\subsection{分段、分數、極限}

\begin{equation}
f(x) = \begin{cases}
x^2 & (x \geq 0) \\
-x^2 & (x < 0)
\end{cases}, \qquad
\lim_{n\to\infty} \left(1 + \frac{1}{n}\right)^n = e
\end{equation}

%% ================================================================
\section{表格(tabular 環境)}
%% ================================================================

\subsection{基本表格}

\begin{table}[h]
\centering
\caption{各城市氣溫資料(示意)}
\label{tab:temp}
\begin{tabular}{lrrr}
\toprule
城市 & 最高氣溫 (°C) & 最低氣溫 (°C) & 平均氣溫 (°C) \\
\midrule
東京 & 35.2 & 25.1 & 29.8 \\
大阪 & 36.4 & 26.3 & 31.0 \\
札幌 & 28.7 & 19.5 & 23.9 \\
那覇 & 32.1 & 27.4 & 29.8 \\
\bottomrule
\end{tabular}
\end{table}

\subsection{複雜表格(\texttt{array} 擴充・儲存格合併)}

\begin{table}[h]
\centering
\caption{程式語言比較}
\label{tab:lang}
\begin{tabular}{|l|c|c|c|}
\hline
語言 & 型別 & 範式 & 主要用途 \\
\hline\hline
Python & 動態 & 多範式 & AI/資料分析 \\
Rust & 靜態 & 系統程式 & 系統開發 \\
Haskell & 靜態 & 函數式 & 研究/金融 \\
JavaScript & 動態 & 多範式 & Web 前端 \\
\hline
\end{tabular}
\end{table}

像表~\ref{tab:temp} 與表~\ref{tab:lang} 可以透過 \verb|\label/\ref| 進行參照。

%% ================================================================
\section{列表環境}
%% ================================================================

\subsection{項目符號列表(itemize)}

\begin{itemize}
\item 第一個項目
\item 第二個項目
\begin{itemize}
\item 嵌套項目 A
\item 嵌套項目 B
\end{itemize}
\item 第三個項目
\end{itemize}

\subsection{編號列表(enumerate)}

\begin{enumerate}[(1)] % enumerate 套件指定格式
\item 最初的步驟
\item 下一個步驟
\item 最後的步驟
\end{enumerate}

\subsection{說明列表(description)}

\begin{description}
\item[pLaTeX] 支援日文的 \LaTeX 系統
\item[upLaTeX] 支援 Unicode 的 pLaTeX 後繼版
\item[LuaLaTeX] 可使用 Lua 腳本的 \LaTeX 系統
\end{description}

%% ================================================================
\section{verbatim・原始碼}
%% ================================================================

\subsection{verbatim 環境}

\begin{verbatim}
#include <stdio.h>
int main(void) {
printf("Hello, pLaTeX!\n");
return 0;
}
\end{verbatim}

\subsection{用 listings 做語法高亮}

\begin{lstlisting}[language=Python, caption={費波那契數列(Python)}]
def fibonacci(n: int) -> int:
"""使用遞迴計算費波那契數列"""
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)

# 輸出前 10 項
for i in range(10):
print(f"F({i}) = {fibonacci(i)}")
\end{lstlisting}

%% ================================================================
\section{框線・強調框}
%% ================================================================

可以使用 \texttt{ascmac} 套件的 \texttt{itembox}\texttt{screen} 等。

\begin{itembox}[l]{重要要點}
\begin{itemize}
\item pLaTeX 使用 \texttt{jarticle} / \texttt{jbook} 文件類別
\item 使用 \texttt{okumacro} 可以用 Ruby 與圓點
\item\texttt{platex + dvipdfmx} 產生 PDF
\end{itemize}
\end{itembox}

\vspace{5pt}

\begin{screen}
\texttt{screen} 環境:終端機風格的框線。用來顯示原始碼範例或輸出範例。
\end{screen}

%% ================================================================
\section{分欄}
%% ================================================================

可用 \texttt{multicol} 套件從某處開始切成兩欄。

\begin{multicols}{2}
\noindent
這是兩欄版面的左欄。
pLaTeX 也能自然處理日文的分欄。
即使是長文也會自動均分,
適合做報章雜誌風格的排版。

\columnbreak

\noindent
這是右欄。
可以用 \verb|\columnbreak| 強制換欄。
數學式 $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$
也可以行內使用。
\end{multicols}

%% ================================================================
\section{行距・間距調整}
%% ================================================================

\subsection{水平間距}

字與字之間的空格:a\,b(\verb|\,| 細空白)、
a\enspace b(\verb|\enspace|)、
a\quad b(\verb|\quad|)、
a\qquad b(\verb|\qquad|)。

\subsection{垂直間距}

可以用 \verb|\vspace| 新增垂直間距。

\vspace{5mm}
在此插入自上方 5mm 的垂直間距後的文字。
\vspace{5mm}

\subsection{行距}

{\setstretch{1.8}
此段落用 \texttt{setspace} 套件的
\texttt{setstretch} 將行距設為 1.8 倍。
在日文文件中,行距約 1.5〜2.0 較為易讀。
}

%% ================================================================
\section{自訂指令與環境}
%% ================================================================

\subsection{自訂指令定義}

\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\highlight}[1]{\colorbox{yellow!60}{#1}}
\newcommand{\term}[1]{\textbf{\textit{#1}}} % 用語強調

範例:$\R$(實數集)、$\N$(自然數)。
\highlight{被標示的文字}
\term{機器學習}(Machine Learning)是人工智慧的一個領域。

\subsection{自訂環境定義}

\newenvironment{mybox}[1]{%
\begin{center}\begin{tabular}{|p{0.85\linewidth}|}
\hline\vspace{1pt}
\textbf{#1}\\[2pt]
}{%
\\\hline\end{tabular}\end{center}
}

\begin{mybox}{自製的框線}
使用 \verb|\newenvironment| 可以定義自訂環境。
可以接受參數以利彈性重複使用。
\end{mybox}

%% ================================================================
\section{參考文獻}
%% ================================================================

文獻引用可用 \verb|\cite| 指令進行~\cite{knuth1984,lamport1994,okumura2020}
文件末會輸出參考文獻清單。

%% ================================================================
%% 參考文獻清單(thebibliography 環境)
%% ================================================================
\begin{thebibliography}{99}

\bibitem{knuth1984}
D.~E. Knuth,
\textit{The \TeX book},
Addison-Wesley, 1984.

\bibitem{lamport1994}
L.~Lamport,
\textit{\LaTeX: A Document Preparation System}, 2nd ed.,
Addison-Wesley, 1994.

\bibitem{okumura2020}
奥村晴彦・黒木裕介,
\LaTeXe 美文書作成入門』第 8 版,
技術評論社, 2020.

\end{thebibliography}

\end{document}

編譯流程

基本(platex + dvipdfmx)

為了正確解決交叉參照與目錄,請先執行 platex 兩次,然後用 dvipdfmx 轉成 PDF。

platex -interaction=nonstopmode -kanji=utf8 document.tex
platex -interaction=nonstopmode -kanji=utf8 document.tex
dvipdfmx document.dvi

ptex2pdf

也可以用將 platex + dvipdfmx 二步驟合併成一個指令的 ptex2pdf

基本指令

ptex2pdf -l -ot "-kanji=utf8 -interaction=nonstopmode" document.tex
選項含意
-l使用以 latex 為基礎的引擎(platex)
-ot "..."傳給 platex 的選項
-kanji=utf8指定輸入編碼為 UTF-8
-interaction=nonstopmode發生錯誤時不停止繼續處理

步驟

  1. 執行 ptex2pdf 兩次(為了解決交叉參照與目錄)

    ptex2pdf -l -ot "-kanji=utf8 -interaction=nonstopmode" document.tex
    ptex2pdf -l -ot "-kanji=utf8 -interaction=nonstopmode" document.tex
  2. 確認結果

    • 成功:會產生 document.pdf
    • 發生錯誤:檢查 .log 中以 ! 開頭的行及其前後 3 行
    • 只有警告:檢查 Overfull/Underfull hbox、未定義參照等
  3. 若需要 BibTeX(當有 .bib 檔或包含 \bibliography 指令時)

    ptex2pdf -l -ot "-kanji=utf8 -interaction=nonstopmode" document.tex
    bibtex document
    ptex2pdf -l -ot "-kanji=utf8 -interaction=nonstopmode" document.tex
    ptex2pdf -l -ot "-kanji=utf8 -interaction=nonstopmode" document.tex

    在執行 bibtex 後再次執行 ptex2pdf 兩次是為了解決參考文獻清單與內文引用編號的交叉參照。

常見錯誤與處理

Undefined control sequence

! Undefined control sequence.
l.42 \somecommand

代表使用了未定義的指令。通常是忘了載入某個套件或是打錯字造成的。

Missing $ inserted

! Missing $ inserted.

表示在數學模式外使用了 _(下標)或 ^(上標)等數學符號。請用 $...$ 將其包起來。

File not found

! LaTeX Error: File `image.png' not found.

表示 \includegraphics 指定的檔案路徑錯誤。請確認相對於 .tex 的路徑是否正確。

Overfull \hbox

Overfull \hbox (12.3pt too wide) in paragraph at lines 55--60

這是警告不是錯誤。表示行超出指定寬度。常見原因為長 URL 或長英文字,通常可用 \allowbreak 或用 \url{}(url 套件)來解決。

檢查日誌

編譯後產生的 .log 檔會記錄錯誤與警告的詳細資訊。錯誤行通常以 ! 開頭,警告則包含 Warning 字樣。

grep -n "^!" document.log        # 抽出錯誤行
grep -n "Warning" document.log # 抽出警告行

刪除中間檔案

編譯會產生許多中間檔。若想保留 PDF 並清理其他檔案,可執行下列指令。

rm -f document.aux document.log document.dvi \
document.toc document.lof document.lot \
document.out document.bbl document.blg \
document.synctex.gz

若要全部刪除(包含 PDF),請再加上 .pdf

コメント

読み込み中...

コメントを投稿する