0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Tex】論文やレポートをTexで書くとき,各章をファイルごとに分ける方法【初期設定】【環境構築】

Posted at

目次

問題

レポートで,以下のようにTexコードを表紙から参考文献までまとめて一つのファイルにまとめて書くことを避けたい.

※改善したい悪い例
\documentclass[a4paper,12pt]{ltjsarticle}
\usepackage{fontspec}
\setmainfont{Times New Roman}[ItalicFont={Times New Roman Italic}, BoldFont={Times New Roman Bold}, BoldItalicFont={Times New Roman Bold Italic}]
\usepackage{geometry}
% マージン等を設定
\geometry{
    a4paper,
    margin=25mm,
    textwidth=\dimexpr\paperwidth-50mm\relax,
    textheight=37\baselineskip,
    lines=37
}
\usepackage{luatexja}
\makeatletter
\renewcommand{\presectionname}{第}
\renewcommand{\postsectionname}{章}
\clearpage
\@startsection{section}{1}{\z@}%
{\Cvs\@plus.5\Cdp\@minus.2\Cdp}
{.5\Cvs\@plus.3\Cdp}
{\normalfont\LARGE\headfont\raggedright}}
\makeatother

\begin{document}
    \begin{titlepage}
        タイトル
    \end{titlepage}
    \begin{abstract}
        \pagenumbering{roman}
        アブストラクト
    \end{abstract}
    \tableofcontents % 目次
    \clearpage
    \begin{section}{まえがき}
        \pagenumbering{arabic}
        \setcounter{page}{1}
        
    \end{section}
    \begin{section}{理論}
        \begin{subsection}{理論1}
            
        \end{subsection}
    \end{section}
    \begin{section}{実験器具}
        
    \end{section}
    \begin{section}{実験}
        
    \end{section}
    \begin{section}{考察}
        
    \end{section}
    \begin{section}{今後の展望}
        
    \end{section}
    \begin{section}*{謝辞}
        \addcontentsline{toc}{section}{謝辞} % 目次に追加
    \end{section}
    \begin{thebibliography}{1}
        \addcontentsline{toc}{section}{参考文献}
        
    \end{thebibliography}
\end{document}

章ごとにファイルを分けることのメリット

・ファイル管理が楽
・可読性の向上,文章全体の構造の明確化
・再利用性が生まれる
・コンパイル時の負荷を軽減

解決方法:章ごとのファイルの分け方

ファイル構成

ファイル構成は以下のようになる.

thesis/
├─ main.tex
├─ title.tex
├─ introduction.tex
├─ theory.tex
├─ ⋮
├─ section.tex
├─ ref.tex

ここからさらに分けたい場合は,「theory-1.tex」「theory-2.tex」のように,細かく分けることもできる.

main.tex

\documentclass[a4paper,12pt]{ltjsarticle} % 文章クラスとオプション

%%%%%%%%%% スタイル %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% フォント設定
\usepackage{fontspec} % フォント設定用パッケージ
\setmainfont{Times New Roman}[ItalicFont={Times New Roman Italic}, BoldFont={Times New Roman Bold}, BoldItalicFont={Times New Roman Bold Italic}] % フォントをtimes new romanに設定
\usepackage[utf8]{inputenc} % UTF-8文字エンコーディング


% 余白,マージンの調整
\usepackage[top=25truemm,bottom=25truemm,left=25truemm,right=25truemm]{geometry}

% 1行あたり70文字分の幅を設定
\setlength{\textwidth}{35em} % 1emは約半角1文字分

% 1ページあたり37行分の高さを設定
\setlength{\textheight}{37\baselineskip}
\addtolength{\textheight}{\topskip}

% 数式関連
\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},     % 予約語などのフォント設定
}

% セクションを「第n章」と表示する設定
\renewcommand{\presectionname}{}
\renewcommand{\postsectionname}{}

% ハイパーリンクの設定
\usepackage{hyperref} 
\hypersetup{
    hidelinks, % ハイパーリンクの色や枠線を隠す
    colorlinks=false, % リンクの文字に色をつける
    pdfborder={0 0 0} % リンクを囲む枠の大きさ
}

% URLを表示するためのパッケージ
\usepackage{url}

% PDFを挿入するパッケージ
\usepackage{pdfpages} 

% 参考文献を表示するための設定
\usepackage[backend=biber,style=ieee,sorting=none]{biblatex} % BibLaTeX設定
\setlength{\bibitemsep}{0.5\baselineskip} % 項目間に0.5行分のスペースを追加
\renewcommand*{\newunitpunct}{,\addspace} % 区切り文字をコンマに設定
\addbibresource{ref.bib} % 参考文献bibファイルを指定

%%%%%%%%%% 本文 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\thispagestyle{empty} % このページのみページ番号を消す
\input{title.tex}
\newpage

\input{abstract.tex}
\pagenumbering{roman} % ページ番号をローマ数字に
\newpage

\tableofcontents % 目次
\newpage

\input{introduction.tex}
\pagenumbering{arabic} % ページ番号をローマ数字に
\setcounter{page}{1} % ページ番号を1から始める
\newpage

\input{section1.tex}
\newpage

% 参考文献セクション
\addcontentsline{toc}{section}{参考文献} % 目次に参考文献を表示させる
\printbibliography[title=参考文献] % 参考文献リストを出力



\end{document}

※以下のリンクから引用させていただいた部分がございます.

title.tex

% !TEX root = main.tex

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

% 表紙としてPDFを挿入する場合
% \includepdf[pages=1]{title.pdf}

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

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

abstract.tex

% !TEX root = main.tex

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\renewcommand{\abstractname}{\LARGE\bfseries 要約} % アブストラクトのタイトル
\begin{abstract}

\end{abstract}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

introduction.tex, section1.tex

introduction.tex

% !TEX root = main.tex

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{まえがき}

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

section1.tex

% !TEX root = main.tex

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{}
\subsection{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

中身の構造は一緒

ref.bib

% 注意:参考文献はciteコマンドで文中で使用しない限り,pdfには出力されない

%% 書き方 %%
% 文献
% article{cite:タグ,
    author  = {著者,Webページの題名 },
    title   = {タイトル},                   % 勝手に""つく
    journal = {掲載されたジャーナル名},
    volume  = {ジャーナルや書籍の巻数},
    number  = {ジャーナルの号数},           % Vol.は勝手につく
    pages   = {ページ範囲}, (例:12--34) % pp.は勝手につく
    year    = {発行年},                    % 勝手に()はつく
    month   = {発行月},
    note    = {補足情報} (例:特別号など)I
}

% Webサイト
% misc{cite:タグ,
  author       = {著者},
  title        = {Webサイトのタイトル},
  howpublished = {Webサイト名, 入手先<\url{URL}>},
  year         = {(参照 YYYY-MM-DD)}
}

@article{cite:,
  author  = {},
  title   = {},
  journal = {},
  volume  = {},
  number  = {},
  pages   = {},
  year    = {},
  month   = {}
}

@misc{cite:,
  author       = {},
  title        = {},
  howpublished = {, 入手先<\url{}>},
  note         = {(参照 --)}
}

ここまでのファイルを作成したら,ビルドできるはず!

スニペットに登録

この設定をいつでもすぐ出せるように,スニペットに登録しておきましょう.

latex.jsonの中身

{
    "LaTeX Snippet": {
        "prefix": "main",
        "body": [
        "\\documentclass[a4paper,12pt]{ltjsarticle} % 文章クラスとオプション",
        "",
        "%%%%%%%%%% スタイル %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
        "",
        "\\usepackage{comment} % 複数行コメント",
        "",
        "% フォント設定",
        "\\usepackage{fontspec} % フォント設定用パッケージ",
        "\\setmainfont{Times New Roman}[ItalicFont={Times New Roman Italic}, BoldFont={Times New Roman Bold}, BoldItalicFont={Times New Roman Bold Italic}] % フォントをtimes new romanに設定",
        "\\usepackage[utf8]{inputenc} % UTF-8文字エンコーディング",
        "",
        "% 余白,マージンの調整",
        "\\usepackage[top=25truemm,bottom=25truemm,left=25truemm,right=25truemm]{geometry}",
        "",
        "% 1行あたり70文字分の幅を設定",
        "\\setlength{\\textwidth}{35em} % 1emは約半角1文字分",
        "",
        "% 1ページあたり37行分の高さを設定",
        "\\setlength{\\textheight}{37\\baselineskip}",
        "\\addtolength{\\textheight}{\\topskip}",
        "",
        "% 数式関連",
        "\\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}, % 予約語などのフォント設定",
        "}",
        "",
        "% セクションを「第n章」と表示する設定",
        "\\renewcommand{\\presectionname}{第}",
        "\\renewcommand{\\postsectionname}{章}",
        "",
        "% ハイパーリンクの設定",
        "\\usepackage{hyperref}",
        "\\hypersetup{",
        "    hidelinks, % ハイパーリンクの色や枠線を隠す",
        "    colorlinks=false, % リンクの文字に色をつける",
        "    pdfborder={0 0 0} % リンクを囲む枠の大きさ",
        "}",
        "",
        "% URLを表示するためのパッケージ",
        "\\usepackage{url}",
        "",
        "% PDFを挿入するパッケージ",
        "\\usepackage{pdfpages}",
        "",
        "% 参考文献を表示するための設定",
        "\\usepackage[backend=biber,style=ieee,sorting=none]{biblatex} % BibLaTeX設定",
        "\\setlength{\\bibitemsep}{0.5\\baselineskip} % 項目間に0.5行分のスペースを追加",
        "\\renewcommand*{\\newunitpunct}{,\\addspace} % 区切り文字をコンマに設定",
        "\\addbibresource{ref.bib} % 参考文献bibファイルを指定",
        "",
        "%%%%%%%%%% 本文 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
        "",
        "\\begin{document}",
        "",
        "\\thispagestyle{empty} % このページのみページ番号を消す",
        "\\input{title.tex}",
        "\\newpage",
        "",
        "\\input{abstract.tex}",
        "\\pagenumbering{roman} % ページ番号をローマ数字に",
        "\\newpage",
        "",
        "\\tableofcontents % 目次",
        "\\newpage",
        "",
        "\\input{introduction.tex}",
        "\\pagenumbering{arabic} % ページ番号をローマ数字に",
        "\\setcounter{page}{1} % ページ番号を1から始める",
        "\\newpage",
        "",
        "\\input{section1.tex}",
        "\\newpage",
        "",
        "% 参考文献セクション",
        "\\addcontentsline{toc}{section}{参考文献} % 目次に参考文献を表示させる",
        "\\printbibliography[title=参考文献] % 参考文献リストを出力",
        "",
        "\\end{document}"
        ],
        "description": "main.tex スニペット"
    },
    "Title Snippet": {
    "prefix": "title",
    "body": [
        "% !TEX root = main.tex",
        "",
        "% 表紙としてPDFを挿入",
        "% \\includepdf[pages=1]{title.pdf}",
        "",
        "\\title{タイトル}",
        "\\author{氏名\\\\thanks{所属}}",
        "\\date{年月日}",
        "\\maketitle",
        "",
    ],
    "description": "title.tex スニペット"
    },
    "Introdution Snippet": {
        "prefix": "introduction",
        "body": [
            "% !TEX root = main.tex",
            "",
            "\\section{まえがき}",
        ],
        "description": "introduction.tex スニペット"
        },
    "Section Snippet": {
    "prefix": "section",
    "body": [
        "% !TEX root = main.tex",
        "",
        "\\section{}",
        "\\subsection{}",
    ],
    "description": "section1.tex スニペット"
    },
    "Abstract Snippet": {
    "prefix": "abstract",
    "body": [
        "% !TEX root = main.tex",
        "",
        "\\renewcommand{\\abstractname}{\\LARGE\\bfseries 要約} % アブストラクトのタイトル",
        "\\begin{abstract}",
        "",
        "\\end{abstract}",
    ],
    "description": "abstract.tex スニペット"
    },
    "Reference Snippet": {
    "prefix": "ref",
    "body": [
        "% 注意:参考文献はciteコマンドで文中で使用しない限り,pdfには出力されない",
        "",
        "%% 書き方 %%",
        "% 文献",
        "% article{cite:タグ,",
        "%     author = {著者,Webページの題名 },",
        "%     title = {タイトル}, % 勝手に\\\"\\\"つく",
        "%     journal = {掲載されたジャーナル名},",
        "%     volume = {ジャーナルや書籍の巻数},",
        "%     number = {ジャーナルの号数}, % Vol.は勝手につく",
        "%     pages = {ページ範囲}, (例:12--34) % pp.は勝手につく",
        "%     year = {発行年}, % 勝手に()はつく",
        "%     month = {発行月},",
        "%     note = {補足情報} (例:特別号など)",
        "% }",
        "",
        "% Webサイト",
        "% misc{cite:タグ,",
        "%     author = {著者},",
        "%     title = {Webサイトのタイトル},",
        "%     howpublished = {Webサイト名, 入手先<\\url{URL}>},",
        "%     year = {(参照 YYYY-MM-DD)}",
        "% }",
        "",
        "@article{cite:,",
        "author = {},",
        "title = {},",
        "journal = {},",
        "volume = {},",
        "number = {},",
        "pages = {},",
        "year = {},",
        "month = {}",
        "}",
        "",
        "@misc{cite:,",
        "author = {},",
        "title = {},",
        "howpublished = {, 入手先<\\url{}>},",
        "note = {(参照 --)}",
        "}"
    ],
    "description": "ref.bib スニペット"
    },
    "ArticleBib Snippet": {
    "prefix": "article",
    "body": [
        "@article{cite:,",
        "author = {},",
        "title = {},",
        "journal = {},",
        "volume = {},",
        "number = {},",
        "pages = {},",
        "year = {},",
        "month = {}",
        "}",
    ],
    "description": "article スニペット"
    },
    "MiscBib Snippet": {
    "prefix": "misc",
    "body": [
        "@misc{cite:,",
        "author = {},",
        "title = {},",
        "howpublished = {, 入手先<\\url{}>},",
        "note = {(参照 ----)}",
        "}"
    ],
    "description": "misc スニペット"
    }
}

setting.jsonを設定

setting.jsonに以下の内容を追記することで,bibファイルがlatexファイルとして扱われ,latexのスニペットがbibファイル内でも使えるようになります.

これでも問題なくビルドでき,参考文献も表示できるため,心配しなくて大丈夫です!

    "files.associations": {
        "*.bib": "latex"
    }

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?