k0b0's record.

Computer Engineering, Arts and Books

Latexにソースコードを載せる

Latexにソースコードを載せる

 Latexにソースコードを載せる必要があったので、そのサンプルコードを忘れないようにメモしておく。

サンプルコード

\documentclass[a4j]{jarticle}
\usepackage{listings}

% ソースコードを表示するための設定
\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
}

\begin{document}

% Hello worldのソースコード
\begin{lstlisting}[caption=Hello world!,label=fig:hoge]
#include<stdio.h>
int main(){
   printf("Hello world!");
}
\end{lstlisting}

\end{document}

コンパイル結果

f:id:k0b0:20190418211848p:plain