0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LaTeXでソースコードを掲載する方法

Posted at

はじめに

LaTeXでソースコードをきれいに整形して掲載するには、listingsパッケージを使用する。最終的には、次のような出力が得られる。

プリアンブルに記述する内容

\usepackage{listings,jvlisting}

\lstset{
  basicstyle={\ttfamily},
  identifierstyle={\small},
  commentstyle={\smallitshape},
  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
}

listingsはソースコードを整形して表示するための基本パッケージである。jvlistingを併用すると、日本語コメントを含むコードも扱える。この設定はやや長いが、主に見た目を整えるための指定であり、行番号や余白、フォントサイズなどを細かく調整している。

本文に記述する内容

\begin{lstlisting}[caption=サンプルプログラム,label=lst:sample]
#include <stdio.h>

int main(void) {
    printf("Hello, world!\n");
    return 0;
}
\end{lstlisting}

本文では、lstlisting環境を使ってソースコードを囲む。captionでタイトルを付け、labelで識別名を設定すると、図表のように扱える。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?