jlisting †Listings パッケージについて †好き好きLaTeX2e/初級編でも言及していますが、プログラムのソースコードなどを載せるのに、Listings パッケージを使うことが出来ます。しかし、素で使うと 日本語 TeX (pTeX) 上では正常に処理できません。そこで、吉永徹美氏が作成されたマクロを併用することでこれを使えるようにします。そのために、以下のファイルをダウンロードしてください。
jlisting.sty が入っていますので,適当に \usepackage{listings,jlisting}
してください. p $l.zip gunzip jlisting.sty.gz yes | latex $l.ins cp $l.pdf ~/ cd mktexlsr Windows でのインストールについては例えばこちらをご参照ください. あとは、いつもどおりに lstlistings 環境と使うなりしてください。 Listings の設定 †私好みの設定です。言語 (language) に関してはその都度変更できるので、 標準の言語を C ({C}) に設定しておきます。 背景色は余り必要ないと思うですが、なんとなく .85 で gray をブレンドします。 \lstset{%
language={C},
backgroundcolor={\color[gray]{.85}},%
basicstyle={\small},%
identifierstyle={\small},%
commentstyle={\small\itshape},%
keywordstyle={\small\bfseries},%
ndkeywordstyle={\small},%
stringstyle={\small\ttfamily},
frame={tb},
breaklines=true,
columns=[l]{fullflexible},%
numbers=left,%
xrightmargin=0zw,%
xleftmargin=3zw,%
numberstyle={\scriptsize},%
stepnumber=1,
numbersep=1zw,%
lineskip=-0.5ex%
}
ソースコード見出し・目次 †もちろん、英語になっているので、ソースコード用のキャプションとか、 ソースコード目次は日本語にします。 \def\lstlistingname{ソースコード}
\def\lstlistlistingname{ソースコード目次}
あと、ソースコード目次を出す時に章毎にスキップを入れてほしいので jsbook の場合は次のように \@chapter を変更します。 \def\@chapter[#1]#2{%
\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\refstepcounter{chapter}%
% \typeout{\@chapapp\thechapter\@chappos}%
\addcontentsline{toc}{chapter}%
{\protect\numberline{\@chapapp\thechapter\@chappos}#1}%
\else\addcontentsline{toc}{chapter}{#1}\fi
\else
\addcontentsline{toc}{chapter}{#1}%
\fi
\chaptermark{#1}%
\addtocontents{lof}{\protect\addvspace{10\p@}}%
\addtocontents{lot}{\protect\addvspace{10\p@}}%
\addtocontents{lol}{\protect\addvspace{10\p@}}%
\if@twocolumn
\@topnewpage[\@makechapterhead{#2}]%
\else
\@makechapterhead{#2}%
\@afterheading
\fi}
要するに章毎にスペースを入れるために \addtocontents{なんちゃら}{\protect\addvspace{10\p@}}%
というものが必要になるということです。 さらに、「\l@なんちゃら」という目次の番号用の幅を指定する 命令もちょっと和文用に修正する必要があります。 %\renewcommand*{\l@subsection}{\@dottedtocline{2}{4.683zw}{4.2zw}}
\let\l@lstlisting\l@figure
こんな感じで listings の設定は良いと思います。 ちょっと別ファイルで存在する C# のコードを載せる必要があるので %%% \sharpCcode{number}{caption}{label}
\newcommand\sharpCcode[3]{%
\lstinputlisting[%
{language=[sharp]C},tabsize=3,numbers=left,stepnumber=5,caption={#2},%
label={list:#3}]{dir/#1.cs}%
}
等とすると良いでしょう。通常はタブが使われます。そのままタブを出力すると大抵は行を超えるので tabsize を 3 スペース分にします。 設定例 その 1 (SciLab?) †\lstset{%
frame=single,%
backgroundcolor={\color[gray]{.85}},%
keywordstyle={\bfseries \color[cmyk]{1,0,0,0}},%
stringstyle={\ttfamily \color[cmyk]{0,1,0,0}},%
commentstyle={\itshape \color[cmyk]{1,0,1,0}},%
identifierstyle={\color[cmyk]{.6,.8,.2,0}},%
breaklines=true,%
xleftmargin=2zw,%
xrightmargin=2zw,%
framerule=.2pt,%
columns=[l]{fullflexible},%
numbers=left,%
stepnumber=5,%
numberstyle={\scriptsize},%
numbersep=1zw,%
language={Scilab},%
lineskip=-.5ex%
}
設定例 その 2 (SciLab?) †\lstset{%
frame=single,%
% backgroundcolor={\color[gray]{.85}},%
keywordstyle={\ttfamily \color[cmyk]{1,0,0,0}},%
stringstyle={\ttfamily \color[cmyk]{0,1,0,0}},%
commentstyle={\itshape \color[cmyk]{1,0,1,0}},%
identifierstyle={\ttfamily\color[cmyk]{.6,.8,.2,0}},%
basicstyle={\ttfamily},
% showspaces,
breaklines=true,%
xleftmargin=2zw,%
xrightmargin=2zw,%
framerule=.2pt,%
columns=[l]{fullflexible},%
numbers=left,%
stepnumber=5,%
numberstyle={\scriptsize},%
numbersep=1zw,%
language={Scilab},%
lineskip=-.5ex,%
}
Lisp (Rodney Brooks バージョン) †Rodney Brooks の subsumption architecture の解説で登場した ソースコード用に morekeywords を追加している. \begin{lstlisting}[language=Lisp,%
morestring={[b]*},%
morekeywords={inputs,outputs,states,defmodule,nil,event-dispatch,%
conditional-dispatch,instance-vars,defwire,output,inhibit,suppress,%
}]
設定例 3 (Java) †\lstset{%
frame=single,
backgroundcolor={\color[gray]{.9}},
stringstyle={\ttfamily \color[rgb]{0,0,1}},
commentstyle={\itshape \color[cmyk]{1,0,1,0}},
identifierstyle={\ttfamily},
keywordstyle={\ttfamily \color[cmyk]{0,1,0,0}},
basicstyle={\ttfamily},
breaklines=true,
xleftmargin=0zw,
xrightmargin=0zw,
framerule=.2pt,
columns=[l]{fullflexible},
numbers=left,
stepnumber=5,
numberstyle={\scriptsize},
numbersep=1em,
language={Java},
lineskip=-0.5zw,
morecomment={[s][{\color[cmyk]{1,0,0,0}}]{/**}{*/}},
}
設定例 4 (Scheme) †\documentclass[dvips,english]{jsarticle}
\usepackage{color,graphicx}
\usepackage[margin=2.5cm]{geometry}
\usepackage{type1cm}
\usepackage{listings,jlisting}
\lstdefinelanguage{Scheme}{%
morekeywords={begin,call-with-current-continuation,call/cc,%
call-with-input-file,call-with-output-file,case,cond,do,%
delay,else,force,for-each,if,lambda,let,let-syntax,let*,%
letrec,letrec-syntax,and,or,not,map,syntax,syntax-rules,%
with-mode,with-input-from-file,with-input-fromport,%
with-output-to-file,with-output-to-port,define%
},%
sensitive,%
alsodigit=-,%
morecomment=[l]{;},%
morecomment=[s]{\#|}{|\#},%
morestring=[b]{"}%
}[keywords,comments,strings]%"
\lstdefinelanguage[MIT]{Scheme}{%
morekeywords=[1]{begin,call-with-current-continuation,call/cc,%
call-with-input-file,call-with-output-file,case,cond,do,%
delay,else,force,for-each,if,lambda,let,let-syntax,let*,%
letrec,letrec-syntax,and,or,not,map,syntax,syntax-rules,%
with-mode,with-input-from-file,with-input-fromport,%
with-output-to-file,with-output-to-port,define%
},%
morekeywords=[2]{fluid-let,%
in-package,local-declare,macro,make-environment,named-lambda,%
using-syntax,with-input-from-string,with-output-to-string,%
with-values,syntax-table-define,list-transform-positive,%
list-transform-negative,list-search-positive,list-search-negative,%
access-components,assignment-components,combination-components,%
comment-components,conditional-components,disjunction-components,%
declaration-components,definition-components,delay-components,%
in-package-components,lambda-components,lambda-components*,%
lambda-components**,open-block-components,pathname-components,%
procedure-components,sequence-components,unassigned?-components,%
unbound?-components,variable-components,%
\#t,\#f,\#e,\#i,\#b,\#o,\#d,\#l,\#s,\#x,\#!\#*,\#@,\#=,\#\#,%
access,cons-stream,declare,default-object?,define-integrable,%
define-structure,define-syntax,er-macro-transformer,%
non-hygienic-macro-transformer,quasiquote,quote,%
rsc-macro-transformer,sc-macro-transformer,set!,%
eq?,eqv?,equal?,%
gcd,modulo,imag-part,numerator,inexact->exact,quotient,abs,%
lcm,rationalize,angle,magnitude,real-part,ceiling,make-polar,%
remainder,denominator,make-rectangular,round,expt,max,truncate,%
floor,min%
}%
sensitive=true,%
alsodigit=-,%
morecomment=[l]{;},%
morecomment=[s]{\#|}{|\#},%
morestring=[b]{"}% "
}
\lstset{%
frame=single,%
keywordstyle={\ttfamily \color[cmyk]{1,0,0,0}},%
stringstyle={\ttfamily \color[cmyk]{0,1,0,0}},%
commentstyle={\itshape \color[cmyk]{1,0,1,0}},%
identifierstyle={\ttfamily\color[cmyk]{.6,.8,.2,0}},%
basicstyle={\ttfamily},
breaklines=true,%
columns=[l]{fullflexible},%
numbers=left,%
stepnumber=5,%
numberstyle={\scriptsize},%
numbersep=1em,%
language={[MIT]Scheme},%
}
\newcommand*\schemesrc[1]{\lstinputlisting[caption={問題~#1}]{#1.scm}}
\begin{document}
\schemesrc{1.01}
\schemesrc{1.02}
\schemesrc{1.03}
\schemesrc{1.04}
\schemesrc{1.05}
\schemesrc{ex1.1.7}
\end{document}
MIT Scheme の keyword 作るの面倒だなぁ.どっかにないのかな. docstrip のように .dtx から .sty と .dvi を作る感じ †\documentclass{jsarticle}
\usepackage[margin=2cm]{geometry}
\usepackage{fancybox,listings,jlisting,color,url,type1cm}
\makeatletter
% 作業用のファイルハンドラ
\newwrite\tmp@outfile
\def\filename@tmp{\jobname.tmp}
% クラスファイル用のハンドラ
\newwrite\class@outfile
\def\filename@class{\jobname.sty}
% ファイルを開く・閉じる
\AtBeginDocument{\immediate \openout \class@outfile \filename@class\relax}
\AtEndDocument{\immediate \closeout \class@outfile}
% code 環境の新設
\def\code{\par\narrowbaselines%
\@bsphack
\begingroup
\immediate\openout\tmp@outfile \filename@tmp\relax % 中途ファイル
\def \EveryVerbatimCodes{\EveryVerbOutCodes}%
\Get@Verbatim \The@Verbatim}
\def\endcode{%
\Verbatim@ObeyThings
\def \Verbatim@Par{^^J}%
\def \Verbatim@Space{\space}%
\def \Verbatim@Tab{\space}%
\def \@lquote{\string`}%
\ThisVerb \relax \gdef \ThisVerb{}%
\let \Every@VerbatimLine \EveryVerbOutLine
\immediate \write \tmp@outfile{\The@Verbatim}%
\immediate \write \class@outfile{\The@Verbatim}%
\immediate \closeout \tmp@outfile
\endgroup
\The@VerbatimErr
\@esphack
\stepcounter{lstnumber}%
\lstinputlisting[firstnumber=last]{\jobname.tmp}%
}
% listings の設定をします
\lstset{%
language={[LaTeX]TeX},%
columns=fullflexible,%
basicstyle={\small \ttfamily \color[cmyk]{0,0,0,1}},%
identifierstyle={\small \ttfamily \color[cmyk]{.6,.8,.2,0}},%
commentstyle={\small \ttfamily \color[cmyk]{1,0,1,0}},%
keywordstyle={\small \ttfamily \bfseries \color[cmyk]{1,0,0,0}},%
ndkeywordstyle={\small \ttfamily \color[cmyk]{1,0,0,0}},%
stringstyle={\small \ttfamily \color[cmyk]{0,1,0,0}},%
xrightmargin=2zw,xleftmargin=3zw,%
numbers=left,stepnumber=1,numberstyle={\scriptsize},numbersep=1zw,%
breaklines=true,
lineskip=-0.5ex%
}
\def\lstlistlistingname{ソースコード目次}
\def\lstlistingname{ソースコード}
\let \l@lstlisting \l@figure
\setcounter{lstnumber}{1}%
\makeatother
\begin{document}
%
ここにまずは自分自身の宣言.
\begin{code}
\ProvidesPackage{benri}[2006/12/18 v1.0 benri package by Thor]
\end{code}
%
Dvipdfmx 用のおまじない.
\begin{code}
\ifnum 42146=\euc"A4A2
\AtBeginDvi{\special{pdf:tounicode EUC-UCS2}}%
\else
\AtBeginDvi{\special{pdf:tounicode 90ms-RKSJ-UCS2}}%"
\fi
\end{code}
%
\end{document}
文芸的ソースコード (FIPA ACL like) †次のように ACL を定義しておけば,文芸的に (literal) に ソースコードを記述する事が出来る. 例えば, ~A とソースコード中に記述すれば, $\neg$A に置換できる. \usepackage{listings,jlisting}
\def\lstlistingname{規則}
\lstdefinelanguage{ACL}{%
morekeywords={accept-proposal,agree,cancel,cfp,confirm,disconfirm,%
failure,inform,inform-if,inform-ref,not-understood,propagate,%
propose,proxy,query-if,query-ref,refuse,reject-proposal,%
request,request-when,request-whenever,subscribe%
},%
sensitive,%
alsodigit=-,%
mathescape,%
literate=%
{Ai} {{$i$}}1% agent i
{Aj} {{$j$}}1% agent j
{_i} {{${}_i$}}1% index i
{_j} {{${}_j$}}1% index j
{~} {{$\neg$}}1% negation (unary operator)
{@a} {{$\alpha$}}1% action
{@p} {{$\phi$}}1% proposition
{@ps}{{$\psi$}}1% another proposition
{:=} { {$\gets$} }1% substitution (binary operator)
{:==}{ {$\equiv$} }1% equivalence (binary operator)
{-->}{{$\rightarrow$}}1% rightarrow
{<--}{{$\leftarrow$}}1% leftarrow
{/\\}{{ $\wedge$ }}1% wedge (binary operator)
{\\/}{{ $\vee$ }}1% vee (binary operator)
{\\<}{{$\langle$}}1% langle (open delimiter)
{\\>}{{$\rangle$}}1% rangle (close delimiter)
{<=} {{ $\leq$ }}1% leq (binary operator)
{<>} {{ $\ne$ }}1% neq (binary operator)
{>=} {{ $\geq$ }}1% geq (binary operator)
}[keywords]%"
\lstset{%
frame=tb,%
keywordstyle={\small\rmfamily \bfseries},%\color[cmyk]{0,1,0,0}},%
identifierstyle={\small \rmfamily},%
basicstyle={\small \rmfamily},
breaklines=true,%
columns=[l]{fullflexible},%
% numbers=left,stepnumber=1,numberstyle={\scriptsize},numbersep=1zw,%
language={ACL},%
lineskip=-.3ex%
}
実際の例を以下に示します.基本的なパフォーマティブの一覧です. \begin{lstlisting}
\<Ai, accept-proposal (Aj, \<Aj, @a\>, @p)\>
\<Ai, agree (Aj, \<Ai, @a\>, @p)\>
\<Ai, cancel (Aj, @a)\>
\<Ai, cfp (Aj, \<Aj, @a\>, Ref $x\,\phi(x)$)\>
\<Ai, confirm (Aj, @p)\>
\<Ai, disconfirm (Aj, @p)\>
\<Ai, failure (Aj, @a, @p)\>
\<Ai, inform (Aj, @p)\>
\<Ai, inform-if (Aj, @p)\>
\<Ai, inform-ref (Aj, Ref $x\,\delta(x)$)\>
\<Ai, not-understood (Aj, @a, @p)\>
\<Ai, propagate (Aj, Ref $x\,\delta(x)$, \<Ai, cact\>, @p)\>
\<Ai, propose (Aj, \<Ai, @a\>, @p)\>
\<Ai, proxy (Aj, Ref $x\,\detal(x)$, \<Aj, cact\>, @p)\>
\<Ai, query-if (Aj, @p)\>
\<Ai, query-ref (Aj, Ref $x\,\delta(x)$)\>
\<Ai, reject-proposal (Aj, \<Aj, @a\>, @p, @ps)\>
\<Ai, request (Aj, @a)\>
\<Ai, request-when (Aj, \<Aj, @a\>, @p)\>
\<Ai, request-whenever (Aj, \<Aj, @a\>, @p)\>
\<Ai, subscribe (Aj, Ref $x\,\delta(x)$)\>
\end{lstlisting}
\begin{lstlisting}
\<Ai, propose (Aj, \<Ai, @a\>, @p)\> :==
\<Ai, inform (Aj, lets (@a), @p)\> |
\<Ai, inform (Aj, \<Aj, should (@a)\>)\> |
\<Ai, query-if (Aj, \<Aj, can (@a)\>)\> |
\<Ai, query-if (Aj, ~\<Aj, can (@a)\>)\> |
\<Ai, query-if (Aj, \<Aj, @a\>)\> |
\<Ai, query-if (Aj, ~\<Aj, @a\>)\> |
\<Ai, inform (Aj, \<Aj, must (@a)\>)\> |
\<Ai, inform (Aj, \<Aj, haveto (@a)\>)\>
\end{lstlisting}
Literal Pascal †% [方言]{定義する言語}[方言]{継承する言語}{定義内容}[継承させる要素?]
\lstdefinelanguage[Literal]{Pascal}[Standard]{Pascal}
{
mathescape,
literate=
{:=} { {$\gets$} }1% substitution (binary operator)
{:==}{ {$\equiv$} }1% equivalence (binary operator)
{-->}{{$\rightarrow$}}1% rightarrow
{<--}{{$\leftarrow$}}1% leftarrow
{<=} {{ $\leq$ }}1% leq (binary operator)
{<>} {{ $\ne$ }}1% neq (binary operator)
{>=} {{ $\geq$ }}1% geq (binary operator)
}
\lstset{%
language={[Literal]Pascal},
basicstyle={\small},%
identifierstyle={\small},%
commentstyle={\small\itshape},%
keywordstyle={\small\bfseries},%
ndkeywordstyle={\small},%
stringstyle={\small\ttfamily},
frame={tb},
breaklines=true,
columns=[l]{fullflexible},%
numbers=left,%
xrightmargin=0zw,%
xleftmargin=2zw,%
numberstyle={\scriptsize},%
stepnumber=1,
numbersep=1zw,%
lineskip=-0.5ex%
}
|