LoginSignup
11
6

More than 5 years have passed since last update.

Pandocを使ってMarkdownからLatexによるPDF生成をする

Last updated at Posted at 2017-10-25

Latexを使うとフリーで綺麗な組版ができる.しかし,レポートのような作成頻度が高く,フォーマットが固定されている文章を毎回書くのは面倒だ.Pandocはmarkdownとlatexの相互変換をサポートしてくれる.なおかつlatexのコンパイル環境が整って入れば直接PDFを生成してくれる.

PandocがサポートしているMarkdown記法はドキュメントで説明されているが,よく使われそうな記述を網羅したサンプルがあれば便利かもしれない.そこでレポートのサンプルを作ってみた:

https://github.com/kzmssk/pandoc_report

以下はこのレポジトリの中身の説明.

Markdownの元ファイルは以下のようになっているとしよう:

report.md

---
title: Title of report
author: Tanuki Kitsune
date: 2017.1.1
...

# First section

This is an example report content.

## How to include images

Including png image:

![Example of png image\label{fig:png_sample}](images/example.png){ width=10cm }

Including pdf image:

![Example of pdf image\label{fig:pdf_sample}](images/example.pdf){ width=10cm }

You can use tex reference like Figure \ref{fig:png_sample} and \ref{fig:pdf_sample}

## Citation

You can cite an article like [@Krizhevsky2012]

## First sub-section

You can put a list of items using itemize:

* Monday
* Tuesday
* Wednesday
* Thursday
* Friday
* Saturday
* Sunday

Or, numbering:

1. Totato
2. Carrot
3. Onion

## Other expressions

* Italic*
* _Italic2_
* **bold**
* __bold2__


## Quotation

> Quotation

## URL

URL: [www.waseda.jp](https://www.waseda.jp)

## Table

Table:

|  A  |  B  |  C  |
| --- | --- | --- |
|  22  |  99  |  128  |

## Math expressions

Formula:

$$
\frac{\partial^2 p}{\partial t^2} = c^2 \nabla^2 p
$$

\begin{eqnarray}
  2x_1 + x_2 & = & 5 \\
    2x_2 & = & 2
\end{eqnarray}

## Source code

\```test.py
t = np.arange(0, np.pi*4)
plt.plot(t, np.sin(t))
\```
You can put source code

# References

注)souce codeの部分はエスケープがうまくできなかったのでバックスラッシュが入っているが,本来は不要

一番上にあるのはYAMLデータブロックと呼ばれるもので,Latexに変換される場合にはtitle, author, dateに割り当てられるようだ.markdownと言いつつも図・数式や相互参照についてはLatex表記が使える.このファイルをPandocを使って変換するコマンドを毎回打つのは面倒なのでGNU Makeにまかせることにする:

SOURCE = report.md
TARGET = report.pdf

all:
    pandoc $(SOURCE) \
        -o output/$(TARGET) \
        -H style/preamble.tex \
        -V fontsize=12pt \
        -V papersize=a4paper \
        -V documentclass:article \
        --bibliography mybib.bib \
        --csl style/ieee-with-url.csl \
        -N

こうすればmakeとすればoutputディレクトリの下にPDFファイルが生成される.この生成はLatexを通していて,フォーマットはPandocのコマンドオプションの他にヘッダーファイルとしてTexファイルを指定できる:

preable.tex
\usepackage{amsmath,amssymb}
\usepackage{bm}
\usepackage{setspace}
\usepackage[margin=3.0cm]{geometry}
\usepackage{color}
\usepackage{url}
\usepackage{here}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{fancyvrb}
\usepackage{graphicx}

参考文献に関してはbibtexが使える.CSLファイルで文献の表示スタイルを指定できる.

11
6
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
11
6