LaTeX Source Code Listing

카테고리 없음 2010. 3. 10. 17:27

LaTeX Source Code Listing

LaTeX에서 소스코드를 넣어야 할때가 많았는데 그동안 fancyvrb만 쓰다가 캡션을 넣을수 없어서 http://faq.ktug.or.kr/faq/SourceCodeListing을 참고하여 하나 만들어봤습니다. 이쁘게 잘나오는군요.
float에서 나오는 형식대로 만들어봤습니다. float경우는 떠다니는 객체이기 때문에 소스코드가 1페이지 이상되면 위치를 결정하기가 힘든반면에 아래의 방식을 쓰면 원하는 위치에 넣을수 있다는 장점이 있습니다.

\newcounter{mysourcecounter}[section]
\setcounter{mysourcecounter}{1}
\makeatletter
\newcommand{\listofsources}{\@starttoc{xmp}}
\newenvironment{Source}[1]{\addcontentsline{xmp}{source}{#1}
\vskip 1em
\noindent\hrule height 1pt
\vskip 2pt
\noindent{\bf Source \thesection .\themysourcecounter} #1\stepcounter{mysourcecounter}
\vskip 2pt
\noindent\hrule height 0.4pt
\begin{list}{}{\setlength\leftmargin{0pt}}\item[]}
{\end{list}
\vskip 2pt
\noindent\hrule height 1pt
\vskip 1em}
\newcommand{\l@source}[2]{\par\noindent#1 \dotfill {\itshape #2}}
\makeatother

사용법은 \begin{Source}{캡션} 이런식입니다.
아래는 간단한 예제.. documentclass같은건 직접 지정해줘야겠죠.

\begin{document}
\fvset{fontsize=\footnotesize}
\listofsources
\newpage
\begin{Source}{Hello World!}
\begin{Verbatim}
#include <stdio.h>

int main()
{
printf("Hello, World!");
}
\end{Verbatim}
\end{Source}
\end{document}


\listofsources로 사용된 소스코드의 목록을 자동으로 만들수 있습니다.

fvset에 numbers=left를 추가하면 아래와 같이 라인넘버도 출력할 수 있습니다.

: