LoginSignup
2
0

More than 1 year has passed since last update.

TeXのlistingsでVSCode風にソースコードを出力してみた

Last updated at Posted at 2021-11-26

環境

PC,OS

次にPCとOS環境を示す.

  • PC
    • M1 MacBook Air
  • OS
    • Big Sur

TeXのバージョン

Paperist/texlive-ja:latestをDockerでインストール.

TeX実行環境について

後日,別途記事で投稿予定.

VSCode風に表示

次にVSCode風に表示した一例を示す.
オプションの設定については,コメントに示す.
色はVSCodeからカラーコードを読み取り,0~1に変換した.

main.tex
\documentclass[a4j,uplatex,dvipdfmx]{jsarticle}
...
...(省略)
...
% ソースコード
\usepackage{listings,plistings}
\usepackage{color}  % 色
\newcommand{\lstlistingnameref}[1]{\textbf{Source Code.\ref{#1}}}   % ソースコード参照
\renewcommand{\lstlistingname}{\textbf{Source Code.}}    % ソースコードキャプション
\lstset{
    language=Scilab,    % 言語設定
    frame=single,       % 枠設定
    breaklines=true,    % 行が長くなった場合自動改行
    breakindent=10pt,   % 自動改行時のインデント
    columns=fixed,      % 文字の間隔を統一
    basewidth=0.5em,    % 文字の横のサイズを小さく
    numbers=left,       % 行数の位置
    numberstyle={\scriptsize \color{black}},  % 行数のフォント
    stepnumber=1,       % 行数の増間
    numbersep=1zw,      % 行数の余白
    xrightmargin=0zw,   % 左の余白
    xleftmargin=2zw,    % 右の余白
    keepspaces=true,    % スペースを省略せず保持
    lineskip=-0.2ex,    % 枠線の途切れ防止
    tabsize = 4,        % タブ数
    showstringspaces=false,  %文字列中の半角スペースを表示させない
    %%%%% style & color %%%%%
    backgroundcolor={\color[gray]{0.1}},   %背景色
    basicstyle={\small\ttfamily \color{white}}, % 基礎の文字のフォントを設定
    identifierstyle={\small\ttfamily \color[rgb]{0.611, 0.862, 0.996}},% 変数名のフォントを設定
    commentstyle={\small \color[rgb]{0.415, 0.600, 0.333}},        % コメントのフォントを設定
    morekeywords=[1]{  % 予約語の追加
        read_csv,
        strtod,
        \%inf,
        \%pi,
        cov,
        cell,
        cat,
    },
    morekeywords=[2]{  % 関数名の登録
        bayes_train,
        bayes_valid,
        get_class_range,
        init_data,
    }, 
    keywordstyle=[1]{\small\bfseries \color[rgb]{0.768, 0.521, 0.749}}, % 予約語のフォントを設定
    keywordstyle=[2]{\small\bfseries \color[rgb]{0.862, 0.862, 0.666}}, % 関数名のフォントを設定
    stringstyle={\small\ttfamily \color[rgb]{0.8, 0.5, 0.3}},   % 文字列
    %%%%% style %%%%%
}

あとは,次のようにプログラムファイルを読み込むことで色付けされる.

sample.tex
\lstinputlisting[caption=2次元正規分布に基づいたベイズ識別器, label=code_bayes_func]{code/bayes_func.sce}

出力結果

スクリーンショット 2021-11-26 11.17.23.png

スクリーンショット 2021-11-26 11.17.34.png

参考にした記事

plistings

日本語対応にするために,plistingsをmain.texと同じディレクトリに移動させる.

文字を等間隔に

次の記事を参考にした.

TeXにソースコードを等幅フォントで"ちゃんと"載せる.

枠線の途切れ

次の記事を参考にした.

listingsで枠線が途切れる問題

2
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
2
0