Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created August 11, 2012 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellbind/3322504 to your computer and use it in GitHub Desktop.
Save bellbind/3322504 to your computer and use it in GitHub Desktop.
[tex]embed source code syntax highlighting with texments
% lualatex --shell-escape hello-texments.tex
\documentclass{ltjsarticle}
\usepackage{luatexja-fontspec}
\usepackage{luacode}
\usepackage{hyperref}
\usepackage{ascmac}
\newdimen\tbaselineshift
\usepackage{texments}
\usestyle{colorful}
\title{texmentsでシンタックスハイライティング}
\author{bellbind}
\date{2012年8月11日}
\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}
@bellbind
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment