koTeX 과 listings 패키지를 이용하여 (한글 문자 포함된) 프로그램 소스를 문서에 표현하기
아래의 소스는 koTeX 2012 이상이면 컴파일되는 TeX 소스이다.
TeXworks 에서 XeLaTeX 으로 컴파일해도 되고, pdfLaTeX 으로 컴파일해도 된다.
한글이 정상적으로 출력되게 하려면,
\begin{lstlisting}[frame=single,escapeinside=~~]
처럼 옵션 escapeinside=~~ 을 주고 ~아무개~ 하면 아무개가 출력된다.
만일 옵션 escapeinside=^$ 을 주었다면 ^아무개$ 해야 아무개가 출력된다.
* 파일명: ex-listings=03.tex
\usepackage{kotex}
\usepackage[T1]{fontenc}
\usepackage{listings} % Include the listings-package
\usepackage{color}
\usepackage{tikz}
\begin{document}
\lstset{ % Set your language (you can change the language for each code-block optionally)
language=Pascal,
tabsize=2,
extendedchars=true,
texcl=true,
stringstyle=\color{black!50}\ttfamily, basicstyle=\small\sffamily,
numbers=left,
numberstyle=\tiny,
frame=tb,
columns=fullflexible,
showstringspaces=false
}
\begin{lstlisting}[frame=single,escapeinside=~~] % Start your code-block
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write('Case insensitive ');
Write('Pascal keywords ~한글~');
\end{lstlisting}
\lstset{
language=C,
tabsize=2,
extendedchars=true,
texcl=true,
stringstyle=\color{black!50}\ttfamily, basicstyle=\small\sffamily,
numbers=left,
numberstyle=\tiny,
frame=tb,
columns=fullflexible,
showstringspaces=false
inputencoding=utf8
}
\vspace{1cm}
\begin{lstlisting}[escapeinside=~~]
#include <stdio.h>
#define N 10
/* Block ~블록~
* comment ~커멘트~ */
int main()
{
int i;
// Line comment.
puts("Hello world! ~안녕?~");
for (i = 0; i < N; i++)
{
puts("LaTeX is also great for programmers!");
}
return 0;
}
\end{lstlisting}
\end{document}
출력된 PDF 파일: