Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created October 3, 2012 03:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellbind/3824883 to your computer and use it in GitHub Desktop.
Save bellbind/3824883 to your computer and use it in GitHub Desktop.
[tex][luatex][japanese]lualatex preamble for font embeded PDF
% lualatex hello-lualatex-embeded-font.tex
\documentclass{ltjsarticle}
\usepackage[ipa]{luatexja-preset}
\usepackage{luacode}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{moreverb}
\title{日本語UTF-8で\LaTeX : Lua\LaTeX}
\author{bellbind}
\date{2012年10月03日}
\begin{document}
\maketitle
{Lua\TeX}\footnote{\url{http://www.luatex.org/}}は、
{\TeX}拡張をプログラミング言語Lua\footnote{\url{http://www.lua.org/}}で
記述できるようにしたものです。
ディストリビューションtexlive 2012\footnote{\url{http://www.tug.org/texlive/}}
に標準で含まれており、
日本語文書でも利用可能です。
{Lua\LaTeX}での日本語文書用プリアンブルは以下のとおり。
\begin{lstlisting}[frame=single]
\documentclass{ltjsarticle}
\usepackage[ipa]{luatexja-preset}
\end{lstlisting}
{\tt \verb+\+usepackage[ipa]\{luatexja-preset\}}を用いれば、
日本語フォントのIPAフォントを埋め込んだPDFを生成します。
フォントを埋め込んでおけば、日本語フォントを持たないPDFリーダーでも
日本語文を表示することができます。
フォントは、{\tt ipa}、{\tt ipaex}、{\tt ms}、{\tt hiragino}等が指定可能です。
さらにOSに該当フォントをインストールしてある必要があります。
埋め込みフォント無指定の{\tt \verb+\+usepackage\{luatexja-preset\}}で使えば、
フォントを埋め込まないPDFが生成されます。
あとはtexファイルの文章をUTF-8で記述して、{\tt lualatex}コマンドで
コンパイルすれば直接PDFが出来上がります。
\begin{lstlisting}[frame=shadowbox]
lualatex foo.tex
\end{lstlisting}
ちなみにLua{\LaTeX}では、texソース中に、
Luaスクリプトを以下のように埋め込むことができます。
\begin{lstlisting}[frame=single]
\usepackage{luacode}
\luaexec{
tex.print("こんにちは{\\LaTeX}")
}
\end{lstlisting}
スクリプトコード中のバックスラッシュの扱いには注意が必要です。
\end{document}
% lualatex --shell-escape hello-texments-embeded-font.tex
\documentclass{ltjsarticle}
\usepackage[ipaex]{luatexja-preset}
\usepackage{luacode}
\usepackage{hyperref}
\usepackage{ascmac}
\newdimen\tbaselineshift
\usepackage{texments}
\usestyle{colorful}
\title{texmentsでシンタックスハイライティング}
\author{bellbind}
\date{2012年10月03日}
\begin{document}
\maketitle
\begin{abstract}
texlive 2012には標準でtexmentsパッケージが含まれています。
このパッケージを用いることで、文書中にシンタックスハイライトされた
各種ソースコードの埋め込みが可能になります。
\end{abstract}
\section{ソースコード埋め込みの例}
\begin{shadebox}
\begin{pygmented}{c}
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
\end{pygmented}
\end{shadebox}
\section{事前準備: pygmentsのインストール}
texmentsは内部でsyntax highlighterのpygments
\footnote{\url{http://pygments.org/}}を利用しています。
texmentsを使う前には、pygmentsパッケージをインストールし、
コマンドラインで{\tt pygmentize}コマンドを実行可能にしておく必要があります。
pygmentsはpythonで実装されているので、
pygmentsを入れるには、あらかじめpythonをインストールしておく必要があります。
pythonを入れ、パッケージ管理システムpipを使って、
以下のコマンドでインストールします。
\begin{screen}
\pygment{sh}{pip install pygments}
\end{screen}
\section{texments用プリアンブル}
\begin{shadebox}
\begin{pygmented}{tex}
\usepackage{texments}
\usestyle{colorful}
\end{pygmented}
\end{shadebox}
可能なスタイルの候補は、以下のコマンドでリストアップできます。
\begin{screen}
\pygment{sh}{pygmentize -L style}
\end{screen}
\section{スニペットの埋め込み}
複数行の場合: {\tt pygmented}環境
\begin{shadebox}
\includecode{tm-embed.tex}
\end{shadebox}
ワンライナーの場合: {\tt pygment}コマンド
\begin{shadebox}
\begin{pygmented}{tex}
\pygment{python}{print("Hello World")}
\end{pygmented}
\end{shadebox}
引数の言語の候補は、以下のコマンドで列挙できます。
\begin{screen}
\pygment{sh}{pygmentize -L lexer}
\end{screen}
\section{ソースファイルの埋め込み}
\begin{shadebox}
\begin{pygmented}{tex}
\includecode[python]{hello.py}
\end{pygmented}
\end{shadebox}
言語指定オプションがない場合、ファイルの拡張子で言語判定してくれます。
\section{コンパイル実行}
シェル実行させる{\tt {\textbackslash}write18}コマンドを使っているので、
{\tt lualatex}実行には{\tt --shell-escape}オプションが必須です。
\begin{screen}
\pygment{sh}{lualatex --shell-escape foo.tex}
\end{screen}
\appendix
\section{luatexでascmacで枠付け}
luatexでascmacパッケージを利用するには、
プリアンブルで少し工夫が必要です。
\begin{shadebox}
\begin{pygmented}{tex}
\usepackage{ascmac}
\newdimen\tbaselineshift
\end{pygmented}
\end{shadebox}
\end{document}
\begin{pygmented}{c}
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
\end{pygmented}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment