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?

More than 3 years have passed since last update.

VSCode+TeXでレポートを書くときのファイル構成とスニペット

Last updated at Posted at 2021-11-30

VSCode+TeXでレポートを書くときのファイル構成とスニペット

目次

  1. PC環境
  2. TeX環境
  3. ファイル構成
  4. ファイルを分けたあとのまとめ方
  5. 各texファイルのテンプレート
  6. スニペット

PC環境

環境
PC M1 Macbook Air
OS Big Sur

TeX環境

TeX環境を構築するときに参考にした記事があることを思い出して,探してたら見つかった.
PCを汚さずにTeXがかけるところが素晴らしい.

ファイル構成

ユーザスニペットの説明をする前にTeXを作る上で1つのファイルに書いていくと
行数が長くなりすぎるので,読み込むパッケージなどの初期設定と
セクション(節)ごとにファイルを分ける.
セクションとは次のことを示す.

\section{}

次にファイル構成の例を示す,section○.texはセクションの数だけファイルを作成する.
また,タイトルを付けたり,参考文献を載せるときも専用のファイルを作成する.
こうすることにより,書いた内容の修正をするのに,どこにあるのか遡らずに見ることができる.
文量が多くなれば,section3-1.texなどのようにして更に分ければよいだけ.

report/
├─ main.tex
├─ section1.tex
├─ section2.tex
├─ ⋮
├─ sectionN.tex
├─ title.tex
├─ ref.tex

ファイルを分けたあとのまとめ方

上述でファイルを分けたがどうやって読み込むのかと,気になると思うが,
これは簡単に解決できる.

main.texには,次のように読み込みたいファイルを順に記述する.

\input{section1.tex}
\input{section2.tex}\input{sectionN.tex}

他のtexファイルの最初に次の1行を記述する.
詳しいことはわからないが,文によれば,「元となるファイルはmain.texですよ.」ということである.
VSCodeで拡張機能の「TeX Workshop」を有効にしていれば,この1行が赤くなるので,それさえ覚えていれば,書いてなくてもないことに気づきやすい.

% !TEX root = main.tex

また,次の記事に書いたplistingなどのstyファイルを使いたいときは同じディレクトリ内に入れておけば良い.

report/
├─ main.tex
├─ section1.tex
├─ section2.tex
├─ ⋮
├─ sectionN.tex
├─ title.tex
├─ ref.tex
├─ plistings.sty

texファイルのテンプレート

次に実際に自分がユーザスニペットに登録しているmain.texの中身を示す.
各コマンドの説明はコメントを書いてあるので省略する.
また,今回掲載するテンプレートはVSCode風にソースコードは出力しないものである.

main.tex
\documentclass[a4j,uplatex,dvipdfmx,11pt]{jsarticle}    % 文章クラスとオプション設定
%%%%%%%%%% スタイル %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{comment}    % 複数行コメント

 % 余白の調整
\usepackage[top=30truemm,bottom=30truemm,left=20truemm,right=20truemm]{geometry}

% 数式関連
\usepackage{amsmath,amssymb,nccmath}    % 数式
\usepackage{bm}         % ベクトルの太文字
\newcommand{\alignref}[1]{\textbf{式(\ref{#1})}}    % 数式参照

% 画像関連
\usepackage{graphicx} % 画像
\renewcommand{\figurename}{\textbf{Fig.}}    % 画像キャプション
\newcommand{\figurenameref}[1]{\textbf{Fig.\ref{#1}}}   % 画像の参照を定義

\usepackage{here}% figureの位置調整
% 表設定
\usepackage{multirow}   % 表の行の結合
\usepackage{longtable}  % ページをまたぐ長い表
\renewcommand{\tablename}{\textbf{Table.}}   % 表キャプション
\newcommand{\tablenameref}[1]{\textbf{Table.\ref{#1}}}  % 表の参照を定義

% ソースコード
\usepackage{listings,plistings}
\renewcommand{\lstlistingname}{\textbf{Source Code.}}    % ソースコードキャプション
\newcommand{\lstlistingnameref}[1]{\textbf{Source Code.\ref{#1}}}   % ソースコード参照

\lstset{
    language=C++,       % 言語設定
    frame=shadowbox,    % 枠設定
    breaklines=true,    % 行が長くなった場合自動改行
    breakindent=12pt,   % 自動改行時のインデント
    columns=fixed,      % 文字の間隔を統一
    basewidth=0.5em,    % 文字の横のサイズを小さく
    numbers=left,       % 行数の位置
    numberstyle={\scriptsize},  % 行数のフォント
    stepnumber=1,       % 行数の増間
    numbersep=1zw,      % 行数の余白
    xrightmargin=0zw,   % 左の余白
    xleftmargin=2zw,    % 右の余白
    framexleftmargin=18pt,  
    keepspaces=true,    % スペースを省略せず保持
    lineskip=-0.2ex,    % 枠線の途切れ防止
    tabsize = 4,        % タブ数
    showstringspaces=false,  %文字列中の半角スペースを表示させない
    basicstyle     ={\small\ttfamily},             % 基礎の文字のフォント設定
    identifierstyle={\small},           % 変数名などのフォント設定
    keywordstyle={\small\bfseries},     % 予約語などのフォント設定
}
%%%%%%%%%% 本文 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\input{title.tex}

\tableofcontents
\newpage

\input{section1.tex}
\input{section2.tex}
\input{section3.tex}
\input{section4.tex}
\input{section5.tex}
\input{section6.tex}
\input{section7.tex}

%%%%%%%%%% 参考文献 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\input{ref.tex}

\end{document}

次にsection○.texを示す.

section.tex
% !TEX root = main.tex

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{セクション}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

次にtitle.texを示す.

title.tex
% !TEX root = main.tex

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\title{タイトル}
\author{氏名\\thanks{所属}}
\date{年月日}
\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

次にref.texを示す.

ref.tex
% !TEX root = main.tex

%%%%%%%%%% 参考文献 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{thebibliography}{99}
    \bibitem{}資料名
\end{thebibliography}

スニペット

登録しているスニペットを次に示す.
ぜひコピーして使ってください.

latex.json
{
	"tex": {
		"prefix": "main",
		"body": [
            "\\documentclass[a4j,uplatex,dvipdfmx,11pt]{jsarticle}    % 文章クラスとオプション設定",
            "%%%%%%%%%% スタイル %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
            "",
            "\\usepackage{comment}    % 複数行コメント",
            "",
            "% 余白の調整",
            "\\usepackage[top=30truemm,bottom=30truemm,left=20truemm,right=20truemm]{geometry}",
            "",
            "% 数式関連",
            "\\usepackage{amsmath,amssymb,nccmath}    % 数式",
            "\\usepackage{bm}         % ベクトルの太文字",
            "\\newcommand{\\alignref}[1]{\\textbf{式(\\ref{#1})}}    % 数式参照",
            "",
            "% 画像関連",
            "\\usepackage{graphicx} % 画像",
            "\\renewcommand{\\figurename}{\\textbf{Fig.}}    % 画像キャプション",
            "\\newcommand{\\figurenameref}[1]{\\textbf{Fig.\\ref{#1}}}   % 画像の参照を定義",
            "",
            "\\usepackage{here}% figureの位置調整",
            "% 表設定",
            "\\usepackage{multirow}   % 表の行の結合",
            "\\usepackage{longtable}  % ページをまたぐ長い表",
            "\\renewcommand{\\tablename}{\\textbf{Table.}}   % 表キャプション",
            "\\newcommand{\\tablenameref}[1]{\\textbf{Table.\\ref{#1}}}  % 表の参照を定義",
            "",
            "% ソースコード",
            "\\usepackage{listings,plistings}",
            "\\renewcommand{\\lstlistingname}{\\textbf{Source Code.}}    % ソースコードキャプション",
            "\\newcommand{\\lstlistingnameref}[1]{\\textbf{Source Code.\\ref{#1}}}   % ソースコード参照",
            "",
            "\\lstset{",
            "\tlanguage=C++,    % 言語設定",
			"\tframe=shadowbox, % 枠設定",
            "\tbreaklines=true,    % 行が長くなった場合自動改行",
            "\tbreakindent=12pt,   % 自動改行時のインデント",
            "\tcolumns=fixed,      % 文字の間隔を統一",
            "\tbasewidth=0.5em,    % 文字の横のサイズを小さく",
            "\tnumbers=left,       % 行数の位置",
            "\tnumberstyle={\\scriptsize},  % 行数のフォント",
            "\tstepnumber=1,       % 行数の増間",
            "\tnumbersep=1zw,      % 行数の余白",
            "\txrightmargin=0zw,   % 左の余白",
            "\txleftmargin=2zw,    % 右の余白",
            "\tframexleftmargin=18pt,   ",
            "\tkeepspaces=true,    % スペースを省略せず保持",
            "\tlineskip=-0.2ex,    % 枠線の途切れ防止",
            "\ttabsize = 4,        % タブ数",
            "\tshowstringspaces=false,  %文字列中の半角スペースを表示させない",
            "\tbasicstyle     ={\\small\ttfamily},             % 基礎の文字のフォント設定",
            "\tidentifierstyle={\\small},           % 変数名などのフォント設定",
            "\tkeywordstyle={\\small\bfseries},     % キーワードのフォント設定",
            "\tcommentstyle={\\textit},             % コメントのフォント設定",           
			"\tndkeywordstyle={\\small},            % 他のキーワードのフォント設定",
			"\tstringstyle={\\small\\ttfamily},     % 文字列のフォント設定",
            "}",
            "%%%%%%%%%% 本文 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
            "",
            "\\begin{document}",
            "",
            "\\input{title.tex}",
            "",
            "\\setcounter{tocdepth}{3}",
            "\\tableofcontents",
            "\\newpage",
            "",
            "\\input{section1.tex}",
            "\\input{section2.tex}",
            "\\input{section3.tex}",
            "\\input{section4.tex}",
            "\\input{section5.tex}",
            "\\input{section6.tex}",
            "\\input{section7.tex}",
            "",
            "%%%%%%%%%% 参考文献 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
            "",
            "\\input{ref.tex}",
            "",
            "\\end{document}",
		]
	},
    "section": {
		"prefix": "section",
		"body": [
			"% !TEX root = main.tex",
			"",
			"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
			"\\section{セクション}",
			"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
			""
		]
	},
    "title":{
		"prefix": "title",
		"body": [
			"% !TEX root = main.tex",
			"",
			"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
			"",
			"\\title{タイトル}",
			"\\author{氏名\\thanks{所属}}",
			"\\date{年月日}",
			"\\maketitle",
			"",
			"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
		]
	},
	"ref": {
		"prefix": "ref",
		"body": [
			"% !TEX root = main.tex",
			"",
			"%%%%%%%%%% 参考文献 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
			"\\begin{thebibliography}{99}",
			"\t\\bibitem{ラベル}資料名", 
			"\\end{thebibliography}"
		]
	},
}

これらをスニペットに登録するだけで,ファイルを新規作成して登録している文字列を入力するだけで
これらが自動入力されれば,今まで作ってきたものからコピーする必要もなく,いつでも簡単にTeXでレポートが書ける.

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?